是否有Linux命令或脚本可以在linux上编写并运行以调用Windows DOS命令提示符?如果是这样,怎么样?

时间:2022-05-01 01:10:09

I would like to automate an existing process that involves executing programs on a Linux server which would then send output file(s) to be edited by excel. The resulting .csv files must be run on a Windows DOS command prompt. If I would like to automate this process, one of the things that I would like to know is if it's possible for me to write and run a script on a Linux server to call a Windows DOS command prompt. Any suggestions?

我想自动执行一个现有的过程,该过程涉及在Linux服务器上执行程序,然后发送输出文件由excel编辑。生成的.csv文件必须在Windows DOS命令提示符下运行。如果我想自动化这个过程,我想知道的一件事是,我是否可以在Linux服务器上编写和运行脚本来调用Windows DOS命令提示符。有什么建议?

2 个解决方案

#1


1  

If you install Cygwin (https://www.cygwin.com/), it provides an SSH server for Windows. Once installed and configured, you can access it like any other SSH server. In the Cygwin SSH server, you can execute both Cygwin compiled programs and Windows native programs (Windows native programs will show the GUI on the users desktop, even though you have forwarded the X). To execute a DOS command prompt, just type "cmd.exe". If you want the DOS command prompt to execute a bat file and exit, you can do it like this:

如果安装Cygwin(https://www.cygwin.com/),它将为Windows提供SSH服务器。安装和配置后,您可以像访问任何其他SSH服务器一样访问它。在Cygwin SSH服务器中,您可以执行Cygwin编译的程序和Windows本机程序(即使您转发了X,Windows本机程序也会在用户桌面上显示GUI)。要执行DOS命令提示符,只需键入“cmd.exe”。如果您希望DOS命令提示符执行bat文件并退出,您可以这样做:

cmd.exe /C '"C:\Path\to\file 1.bat"' (The single quotes are due to as the name has spaces, the cmd parameters needs the double quotes, and to avoid Bash to take them out, we have to put them between single quotes).

cmd.exe / C'“C:\ Path \ to \ file 1.bat”'(单引号是由于名称有空格,cmd参数需要双引号,并且为了避免Bash将它们取出,我们必须把它们放在单引号之间)。

Note: If you want a quick Cygwin installation, you can try this package: "http://www.gage.es/glab-download". This installation is from an open source program (gLAB), which already has an automatic Cygwin installation embedded. Just erase the gLAB later, and you will have a Cygwin with everything you need.

注意:如果您想快速安装Cygwin,可以试试这个软件包:“http://www.gage.es/glab-download”。此安装来自开源程序(gLAB),该程序已嵌入了自动Cygwin安装。稍后擦除gLAB,你将拥有一个Cygwin,你需要的一切。

#2


0  

Option 1

It is a gaping security hole since it allows password-less connection to your Windows box from anywhere, but if you are only doing this at home inside a private network, it may be ok.

这是一个巨大的安全漏洞,因为它允许从任何地方无密码连接到您的Windows机箱,但如果您只在家中私人网络中这样做,它可能没问题。

Basically, you would install the tiny program called netcat (also known as nc) on your Windows box. It is already installed on most Linux boxes.

基本上,您将在Windows框中安装名为netcat(也称为nc)的小程序。它已经安装在大多数Linux机器上。

Then on your Windows box, in a Command Prompt you tell netcat to listen on port 10000 (or any other one you choose) and run cmd.exe to process whatever it receives on that port:

然后在Windows框中,在命令提示符中告诉netcat侦听端口10000(或您选择的任何其他端口)并运行cmd.exe来处理它在该端口上收到的任何内容:

nc -l -p 10000 -v -e cmd.exe

On your Linux box, you connect to port 10000 of your Windows box and then the commands you type in your Linux box are processed by cmd.exe on your Windows box. So, assuming your Windows box has the IP address 192.168.0.10:

在Linux机器上,连接到Windows框的端口10000,然后在Windows框中由cmd.exe处理在Linux框中键入的命令。因此,假设您的Windows框具有IP地址192.168.0.10:

nc 192.168.0.10 10000

process file.xls

Of course, installing an ssh server is more secure, but this approach has the advantage that it requires very little setup at all.

当然,安装ssh服务器更安全,但这种方法的优点是它只需要很少的设置。

Option 2

Another option might be to share a directory between the Linux and Windows machine and have the Windows machine regularly (every 10 seconds maybe) check the directory for new files and if it sees any process them and then move them to a different directory called, say Processed so that they don't get processed again on the next iteration. So there would be a BATCH script running on Windows that just did a loop:

另一种选择可能是在Linux和Windows机器之间共享一个目录,并定期安装Windows机器(可能每10秒)检查一下目录中的新文件,如果它看到任何进程,然后将它们移动到另一个名为的目录,比如说已处理,以便在下一次迭代时不会再次处理它们。因此,在Windows上运行的BATCH脚本只会执行一个循环:

@echo off
:top
echo Checking for files...
FOR /F ["options"] %%parameter IN (*.CSV) DO (
   process file
   move file to processed subdirectory
)
sleep 3
goto top

See ss64 website for syntax of FOR loop.

有关FOR循环的语法,请参见ss64网站。

#1


1  

If you install Cygwin (https://www.cygwin.com/), it provides an SSH server for Windows. Once installed and configured, you can access it like any other SSH server. In the Cygwin SSH server, you can execute both Cygwin compiled programs and Windows native programs (Windows native programs will show the GUI on the users desktop, even though you have forwarded the X). To execute a DOS command prompt, just type "cmd.exe". If you want the DOS command prompt to execute a bat file and exit, you can do it like this:

如果安装Cygwin(https://www.cygwin.com/),它将为Windows提供SSH服务器。安装和配置后,您可以像访问任何其他SSH服务器一样访问它。在Cygwin SSH服务器中,您可以执行Cygwin编译的程序和Windows本机程序(即使您转发了X,Windows本机程序也会在用户桌面上显示GUI)。要执行DOS命令提示符,只需键入“cmd.exe”。如果您希望DOS命令提示符执行bat文件并退出,您可以这样做:

cmd.exe /C '"C:\Path\to\file 1.bat"' (The single quotes are due to as the name has spaces, the cmd parameters needs the double quotes, and to avoid Bash to take them out, we have to put them between single quotes).

cmd.exe / C'“C:\ Path \ to \ file 1.bat”'(单引号是由于名称有空格,cmd参数需要双引号,并且为了避免Bash将它们取出,我们必须把它们放在单引号之间)。

Note: If you want a quick Cygwin installation, you can try this package: "http://www.gage.es/glab-download". This installation is from an open source program (gLAB), which already has an automatic Cygwin installation embedded. Just erase the gLAB later, and you will have a Cygwin with everything you need.

注意:如果您想快速安装Cygwin,可以试试这个软件包:“http://www.gage.es/glab-download”。此安装来自开源程序(gLAB),该程序已嵌入了自动Cygwin安装。稍后擦除gLAB,你将拥有一个Cygwin,你需要的一切。

#2


0  

Option 1

It is a gaping security hole since it allows password-less connection to your Windows box from anywhere, but if you are only doing this at home inside a private network, it may be ok.

这是一个巨大的安全漏洞,因为它允许从任何地方无密码连接到您的Windows机箱,但如果您只在家中私人网络中这样做,它可能没问题。

Basically, you would install the tiny program called netcat (also known as nc) on your Windows box. It is already installed on most Linux boxes.

基本上,您将在Windows框中安装名为netcat(也称为nc)的小程序。它已经安装在大多数Linux机器上。

Then on your Windows box, in a Command Prompt you tell netcat to listen on port 10000 (or any other one you choose) and run cmd.exe to process whatever it receives on that port:

然后在Windows框中,在命令提示符中告诉netcat侦听端口10000(或您选择的任何其他端口)并运行cmd.exe来处理它在该端口上收到的任何内容:

nc -l -p 10000 -v -e cmd.exe

On your Linux box, you connect to port 10000 of your Windows box and then the commands you type in your Linux box are processed by cmd.exe on your Windows box. So, assuming your Windows box has the IP address 192.168.0.10:

在Linux机器上,连接到Windows框的端口10000,然后在Windows框中由cmd.exe处理在Linux框中键入的命令。因此,假设您的Windows框具有IP地址192.168.0.10:

nc 192.168.0.10 10000

process file.xls

Of course, installing an ssh server is more secure, but this approach has the advantage that it requires very little setup at all.

当然,安装ssh服务器更安全,但这种方法的优点是它只需要很少的设置。

Option 2

Another option might be to share a directory between the Linux and Windows machine and have the Windows machine regularly (every 10 seconds maybe) check the directory for new files and if it sees any process them and then move them to a different directory called, say Processed so that they don't get processed again on the next iteration. So there would be a BATCH script running on Windows that just did a loop:

另一种选择可能是在Linux和Windows机器之间共享一个目录,并定期安装Windows机器(可能每10秒)检查一下目录中的新文件,如果它看到任何进程,然后将它们移动到另一个名为的目录,比如说已处理,以便在下一次迭代时不会再次处理它们。因此,在Windows上运行的BATCH脚本只会执行一个循环:

@echo off
:top
echo Checking for files...
FOR /F ["options"] %%parameter IN (*.CSV) DO (
   process file
   move file to processed subdirectory
)
sleep 3
goto top

See ss64 website for syntax of FOR loop.

有关FOR循环的语法,请参见ss64网站。