Linux 从 *nix 在远程 Windows 机器上执行 exe 或 bat 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7036300/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing exe or bat file on remote windows machine from *nix
提问by lAH2iV
I am trying to execute a bat file on remote windows machine on cloud from my Linux. The bat files starts selenium server and then my selenium tests are run. I am not able to start selenium RC server on that machine. I tried with Telnet but the problem with it is when telnet session is closed the RC server port is also closed. As my code my code has to start the server so I tried with ANT telnet task and also executed shell script of telnet in both ways the port was closed.
我正在尝试从我的 Linux 在云上的远程 Windows 机器上执行一个 bat 文件。bat 文件启动 selenium 服务器,然后运行我的 selenium 测试。我无法在该机器上启动 selenium RC 服务器。我尝试使用 Telnet,但问题是当 telnet 会话关闭时,RC 服务器端口也关闭。作为我的代码,我的代码必须启动服务器,因此我尝试使用 ANT telnet 任务,并且还以关闭端口的两种方式执行了 telnet 的 shell 脚本。
I read about Open SSH, psexec for linux and cygwin. But i am not getting how to use these and will they will solve my problem.
我阅读了 Open SSH、psexec for linux 和 cygwin。但我不知道如何使用这些,它们会解决我的问题。
I have tried to start a service which will start the server but in this method i am not getting browser visible all tests are running in background as my script takes screen shot browser visibility is must.
我试图启动一个将启动服务器的服务,但在这种方法中,我没有让浏览器可见,所有测试都在后台运行,因为我的脚本需要屏幕截图浏览器可见性。
Now my Question is what to use and which will be preferable for my job. and what ever i choose should be executed by code it may be by shell, ant or php.
现在我的问题是使用什么以及哪种更适合我的工作。我所选择的应该由代码执行,它可能是由 shell、ant 或 php 执行的。
Thanks in advance.
提前致谢。
采纳答案by Lee Netherton
I prefer to use cygwin and use SSH to then log in to the windows machine to execute commands. Be aware that, by default, cygwin doesn't have OpenSSH installed.
我更喜欢使用cygwin,然后使用SSH登录windows机器执行命令。请注意,默认情况下,cygwin 没有安装 OpenSSH。
Once you have SSH working on the windows machine you can run a command on it from the Linux machine like this:
在 Windows 机器上运行 SSH 后,您可以从 Linux 机器上运行命令,如下所示:
ssh user@windowsmachine 'mycommand.exe'
You can also set up ssh authenticationkeys so that you don't need to enter a password each time.
您还可以设置ssh 身份验证密钥,这样您就无需每次都输入密码。
回答by xuedi
Not a very secure way, but if you have a running webserver you can use PHP or ASP to trigger a system command. Just hide thgat script under www.myserver.com/02124309c9867a7616972f52a55db1b4.php or something. And make sure the command are fixed written in the code, not open via parameter ...
不是一种非常安全的方式,但如果您有一个正在运行的网络服务器,您可以使用 PHP 或 ASP 来触发系统命令。只需将 thgat 脚本隐藏在 www.myserver.com/02124309c9867a7616972f52a55db1b4.php 或其他内容下即可。并确保命令已固定写入代码中,而不是通过参数打开...
回答by David W.
Let's go through the various options you mentioned:
让我们来看看你提到的各种选项:
- psexec: This is pretty much a PC only thing. Plus, you must make sure that newer Windows machines can get through the UAC that are setup by default. UAC is the thing you see all the time on Vista and Windows 7 when you try to do something that requires administrator's privileges. You can try something called winexewhich is a Linux program that can do the psexecprotocol, but I've had problems getting it to work.
- OpenSSH: There are two main flavors of SSH, and Open SSH is the one used by the vast majority of sites. SSH has several advantages over other methods:
- SSH is secure: Your network traffic is encrypted.
- SSH can be password independent: You can setup SSH to use private/public keys. This way, you don't even have to know the password on the remote server. This makes it more secure since you don't have passwords being stored on various systems. And, in many Windows sites, passwords have to be changed every month or so or the account is locked.
- SSH can do more than just execute remote commands: There are two sub-protocols on SSH called SCP and SFTP. These allow you to transfer files between two machines. Since they work over SSH, you get all of the advantages of SSHincluding encrypted packets, and public/private key protection.
- SSH is well implemented in the Unix World: You'll find SSH clients built into Ant, Maven, and other build tools. Programs like CVS, Subversion, and Git can work over SSH connections too. Unfortunately, the Windows World operates in a different space time dimension. To use SSH on a Windows system requires third party software like Cygwin.
- Cygwin: Cygwin is sort of an odd beast. It's a layer on top of Windows that allows many of the Unix/GNU libraries to work over Windows. It was originally developed to allow Unix developers to run their software on
WindowsDOS systems. However, Cygwin now contains a complete Unix like system including tools such as Perl and Python, BASH shell, and many utilities such as an SSH server. Since Cygwin is open source, you can download it for free and run SSH server. Unfortunately, I've had problems with Cygwin's SSH server. Another issue: If you're running programs remotely, you probably want to run them in a Windows environment and not the Cygwin environment.
- psexec:这几乎是仅适用于 PC 的东西。此外,您必须确保较新的 Windows 计算机可以通过默认设置的 UAC。当您尝试执行需要管理员权限的操作时,您在 Vista 和 Windows 7 上总是会看到 UAC。你可以尝试一个叫做winexe 的东西,它是一个可以执行psexec协议的 Linux 程序,但我在让它工作时遇到了问题。
- OpenSSH:SSH 有两种主要风格,Open SSH 是绝大多数站点使用的一种。与其他方法相比,SSH 有几个优点:
- SSH 是安全的:您的网络流量是加密的。
- SSH 可以独立于密码:您可以设置 SSH 以使用私钥/公钥。这样,您甚至不必知道远程服务器上的密码。这使得它更安全,因为您没有将密码存储在各种系统上。而且,在许多 Windows 站点中,必须每个月左右更改密码,否则帐户将被锁定。
- SSH 可以做的不仅仅是执行远程命令:SSH 上有两个子协议,称为 SCP 和 SFTP。这些允许您在两台机器之间传输文件。由于他们在工作的SSH,你得到所有的优点SSH包括加密的数据包,公钥/私钥保护。
- SSH 在 Unix 世界中得到了很好的实现:您会发现 SSH 客户端内置于Ant、Maven和其他构建工具中。CVS、Subversion 和 Git 等程序也可以通过 SSH 连接工作。不幸的是,Windows World 在不同的时空维度上运行。在 Windows 系统上使用 SSH 需要像 Cygwin 这样的第三方软件。
- Cygwin:Cygwin 有点奇怪。它是 Windows 之上的一个层,允许许多 Unix/GNU 库在 Windows 上工作。它最初的开发目的是允许 Unix 开发人员在
WindowsDOS 系统上运行他们的软件。然而,Cygwin 现在包含一个完整的类 Unix 系统,包括 Perl 和 Python、BASH shell 和许多实用程序(例如 SSH 服务器)等工具。由于 Cygwin 是开源的,您可以免费下载并运行 SSH 服务器。不幸的是,我在 Cygwin 的 SSH 服务器上遇到了问题。另一个问题:如果您远程运行程序,您可能希望在 Windows 环境而不是 Cygwin 环境中运行它们。
I recommend that you look at WinSSHDfrom Bitvise. It's an OpenSSH implementation of the SSH Server, but it's not open source. It's about $100 per license and you need a license on each server. However, it's a robust implementation and has all of the features SSH has to offer.
我建议你看一下WinSSHD从Bitvise。它是 SSH 服务器的 OpenSSH 实现,但它不是开源的。每个许可证大约 100 美元,您需要在每台服务器上都有一个许可证。但是,它是一个健壮的实现,并且具有 SSH 必须提供的所有功能。
You can look at CoSSHwhich is a package of Cygwin utilities and OpenSSH server. This is free and all open source, but if you want an easy way of setting it up, you have to pay for the Advanced Administrator Console. You don't need the Advanced Administrator Console since you can use Cygwin to set everything up, and it comes with a basic console to help.
您可以查看CoSSH,它是 Cygwin 实用程序和 OpenSSH 服务器的包。这是免费且全部开源的,但如果您想要一种简单的设置方式,则必须为高级管理员控制台付费。您不需要高级管理员控制台,因为您可以使用 Cygwin 来设置所有内容,并且它带有一个基本控制台来提供帮助。
回答by LAZA
I've succeeded to run remote command on W2K3 via EXPECTon Debian Buster. Here is the script of mine:
我已经成功地通过Debian Buster上的EXPECT在W2K3 上运行远程命令。这是我的脚本:
#!/usr/bin/expect
#
# execute the script in the following manner:
#
# <script> <vindoze> <user> <password> <command>
#
#
set timeout 200
set hostname [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]
spawn telnet $hostname
expect "login:"
send "$username\r"
expect "password:"
send "$password\r"
expect "C:*"
send "dir c:\tasks\logs \r"
# send $command
expect "C:*"
send "exit\r\r\r"
Bear in mind that you need to enable TELNET service of the Win machine and also the user which you are authenticated with must be member of TelnetClients built-in Win group. Or as most of the Win LazyMins do - authenticate with Admin user ;)
请记住,您需要启用 Win 机器的 TELNET 服务,并且您进行身份验证的用户必须是 TelnetClients 内置 Win 组的成员。或者像大多数 Win LazyMins 一样 - 使用管理员用户进行身份验证;)
I use similar "expect" script for automated collecting & backup configuration of CLI enabled network devices like Allied Telesyn, Cisco, Planet etc.
我使用类似的“expect”脚本来自动收集和备份支持 CLI 的网络设备,如 Allied Telesyn、Cisco、Planet 等。
Cheers, LAZA
干杯,拉萨