Shell 结构无法在windows上执行远程命令
我正在使用fabric在远程Windows7系统上运行一些命令。我所做的是:Shell 结构无法在windows上执行远程命令,shell,fabric,msys,Shell,Fabric,Msys,我正在使用fabric在远程Windows7系统上运行一些命令。我所做的是: env.hosts=['192.168.91.235'] env.user='test' env.password='123456' def test_windows(): run("ifconfig",shell=False) pass 它适用于“ipconfig”,并输出远程系统的网络信息,因此我确信ssh连接是正常的。但它不适用于我尝试过的任何其他命令,如“cd”、“echo hello”。错
env.hosts=['192.168.91.235']
env.user='test'
env.password='123456'
def test_windows():
run("ifconfig",shell=False)
pass
它适用于“ipconfig”,并输出远程系统的网络信息,因此我确信ssh连接是正常的。但它不适用于我尝试过的任何其他命令,如“cd”、“echo hello”。错误是:
out:无法在远程系统上执行命令或shell:无法执行进程
我想远程运行windows shell脚本,那么我应该这样做吗
另外,如果我通过putty连接窗口,“cd”“echo hello”命令有效
[更新]
我意识到fabric正在使用env.shell解释我传递的命令,现在我的问题是:是否可以将windows shell指定给fabric env.shell?怎么做
[再次更新]
我将“cmd.exe”分配给env.shell,但它在执行“cmd.exe”时被卡住了。经过几次尝试后,env.shell=“cmd.exe/c”
最终起作用。现在我可以通过结构远程执行dir
,echo
[最终解决后更新:
我不确定我的解决方案是否有缺陷,到目前为止进展顺利
我的解决方案是在windows+fabric上使用msys,在linux+FreeShd上使用windows作为sshserver
windows上的msys提供了一个“bash”,正如Andrew Walker在下面提到的,fabric需要“bash”,并在其上表现出色。尽管在上面的[再次更新]中,fabric也可以与cmd.exe/c
共存,并在其上执行windows命令
要将msysbash/shell分配给fabric,用户应该具体告诉env如何找到bash
env.shell='cmd.exe /c c:/msys/1.0/bin/sh.exe -l -c'
cmd.exe/c
tell fabric以下字符串应作为cmd.exe中的“命令”执行,/c
在cmd.exe
之后表示windows cmd contenxt中的命令,类似于cmd.exe/c“命令”
然后,cmd.exe执行c:/msys/1.0/bin/sh-l-c
,fabric执行msys shell。我不知道-l
有什么作用,msys外壳在没有-l
的bin文件夹中找不到fakelinuxcommand.exe,因此我得出结论认为-l
有助于改善环境-c
与cmd.exe/c
中的/c
类似,表示以下字符串作为c:/msys/1.0/bin/sh
的命令,因此传递给ssh客户端的以下内容作为msys shell中的命令执行
一个完整的例子来说明:
env.password='123456'
env.user='test'
env.hosts=['test@192.168.91.238']
env.shell='cmd.exe /c c:/msys/1.0/bin/sh.exe -l -c'
def run_shell_command(command):
return run(command,pty=False)
函数run_shell_命令中的参数command
将与env.shell连接,并由msys shell执行
[我的结论]
我不认为没有“bash-like”伪终端,fabric就不能工作。在[再次更新]中,linux中的fabric可以在远程windows的cmd.exe中执行命令。在一些只存在远程WinODW的场景中,这就足够了。msys提供了一个LinuxBash,允许我从本地linux在远程windows和远程linux上执行相同的shell脚本 Fabric实际上希望远程计算机上有一个bash(或至少类似bash的)shell
让结构在远程windows计算机上运行命令的最简单方法是安装适当的shell。例如,的安装默认包括这样一个外壳。我同意火星猫的结论,即“结构可以在没有“bash-like”外壳的情况下工作” 我已经在我的Windows7机器上安装了FreeShd软件(作为服务运行),并且我已经为“sistemas”用户授予了访问Windowsshell的权限 然后,可以使用以下结构代码远程调用该shell上的命令(如systeminfo): 请特别注意
shell=False
参数,这是使其正常工作的关键
如果我们运行它,我们得到的是:
[sistemas@cliente01 ~]$ fab test_win
[sistemas@cliente03] Executing task 'test_win'
[sistemas@cliente03] run: systeminfo
[sistemas@cliente03] Login password for 'sistemas':
[sistemas@cliente03] out:
[sistemas@cliente03] out: Host Name: RP_CLIENTE03
[sistemas@cliente03] out: OS Name: Microsoft Windows 7 Professional
[sistemas@cliente03] out: OS Version: 6.1.7601 Service Pack 1 Build 7601
[sistemas@cliente03] out: OS Manufacturer: Microsoft Corporation
[sistemas@cliente03] out: OS Configuration: Standalone Workstation
...
[sistemas@cliente03] out:
Done.
Disconnecting from cliente03... done.
我真的很感激你的回答,虽然cygwin并不能完全满足我们的需要。我已经在问题描述中更新了让cmd.exe在上面工作的方法。谢谢你的支持。这个答案应该是正确的,因为它解决了我的问题和OP。
[sistemas@cliente01 ~]$ fab test_win
[sistemas@cliente03] Executing task 'test_win'
[sistemas@cliente03] run: systeminfo
[sistemas@cliente03] Login password for 'sistemas':
[sistemas@cliente03] out:
[sistemas@cliente03] out: Host Name: RP_CLIENTE03
[sistemas@cliente03] out: OS Name: Microsoft Windows 7 Professional
[sistemas@cliente03] out: OS Version: 6.1.7601 Service Pack 1 Build 7601
[sistemas@cliente03] out: OS Manufacturer: Microsoft Corporation
[sistemas@cliente03] out: OS Configuration: Standalone Workstation
...
[sistemas@cliente03] out:
Done.
Disconnecting from cliente03... done.