结构无法在Windows上执行远程命令 [英] fabric cannot execute remote command on windows

查看:166
本文介绍了结构无法在Windows上执行远程命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fabric在远程Windows 7系统上运行一些命令.而我所做的就像:

I'm using fabric to run some command on a remote windows 7 system. and what i did was like:

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".错误是:

And it works for "ipconfig" and outputs the network infors of remote system, so i'm sure the ssh connection is OK. But it does not work for any other command i tried, like "cd", "echo hello". The error was :

out:无法在远程系统上执行命令或shell:无法执行进程.

out: Unable to execute command or shell on remote system: Failed to Execute process.

我想远程运行Windows Shell脚本,那我该怎么办?

And i want to run a windows shell script remotely, so should i do?

P.S.如果我通过腻子连接窗户,则命令"cd","echo hello"有效.

P.S. the commands "cd" "echo hello" works if i connect the windows via putty.

[更新]

我意识到Fabric正在使用env.shell来解释我传递的命令,现在我的问题是:是否可以为fabric env.shell指定Windows Shell?以及如何?

I realize that fabric is using env.shell to interpret the command i passed, now my question is: can is specify a windows shell to fabric env.shell? and how?

[再次更新]

我正在将"cmd.exe"分配给env.shell,并且卡在执行"cmd.exe"的过程中.经过几次尝试,env.shell="cmd.exe /c"最终可以工作.现在我可以通过结构远程执行direcho.

I was assigning "cmd.exe" to env.shell and it was stuck at executing "cmd.exe". After a few attempts env.shell="cmd.exe /c" eventually works. now i can execute dir, echo remotely via fabric.

[最终解决后更新:]

我不确定我的解决方案是否有缺陷,它可以顺利进行到现在.

I'm not sure if my solution is flawed, it goes well till now.

我的解决方案是在Windows上使用msys +在linux上使用fabric +在Windows上使用freesshd作为sshserver.

My solution is using msys on windows + fabric on linux + freesshd on windows as sshserver.

msys提供了重击",正如下面的安德鲁·沃克(Andrew Walker)所说,fabric期望重击"并在其上表现出色.尽管在上面的[再次更新]中,结构也可以与cmd.exe /c一起使用并在其上执行Windows命令.

msys on windows provides a "bash", as Andrew Walker mentioned below, fabric expects "bash" and performs excellently on it. although in [update again] above, fabric can also live with cmd.exe /c and execute windows commands on it.

要将msys bash/shell分配给结构,用户应特别告知env如何找到bash

To assign msys bash/shell to fabric, user should tell env specifically how to find the bash

env.shell='cmd.exe /c c:/msys/1.0/bin/sh.exe -l -c'

cmd.exe /c告诉fabric以下字符串应在cmd.exe中作为命令"执行,/ccmd.exe表示Windows cmd contenxt中的命令之后,就像cmd.exe /c "command"

cmd.exe /c tell fabric the following string should be execute as a "command" in cmd.exe, /c after cmd.exe indicates a command in windows cmd contenxt, it's like cmd.exe /c "command"

然后c:/msys/1.0/bin/sh -l -c由cmd.exe执行,并且fabric正在执行msys shell.我不确定-l的作用,没有-l的msys shell无法在bin文件夹中找到fakelinuxcommand.exe,因此我得出的结论是-l对环境有所帮助. -ccmd.exe /c中的/c相似,表示以下字符串作为c:/msys/1.0/bin/sh的命令,因此传递给ssh客户端的以下内容在msys shell中作为命令执行.

then c:/msys/1.0/bin/sh -l -c is executed by cmd.exe and fabric is executing msys shell. I'm not sure what does -l do, the msys shell cannot find fakelinuxcommand.exe in bin folder without -l, so i made the conclusion that -l helps with the environment. -c is similar with /c in cmd.exe /c, indicating the following string as a command of c:/msys/1.0/bin/sh, so the following stuff passed to ssh client is executed as a command in 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中的参数command将与env.shell连接,并由msys shell执行.

the argument command in function run_shell_command will be concatenate with env.shell and executed by msys shell.

[我的结论]

我不认为没有'bash like'伪终端,结构就无法工作.在[再次更新]中,Linux中的结构可以在远程Windows的cmd.exe中执行命令.在仅存在远程窗口的某些情况下,这就足够了. msys提供了一个Linux bash,可让我在本地Windows的远程Windows和远程Linux上执行相同的Shell脚本.

I don't think fabric cannot work without a 'bash like' pseudo terminal. In [update again], fabric in linux can execute command in a remote windows's cmd.exe. And that is enough in some scenarios where only remote winodws are present. msys provides a linux bash to allow me execute same shell scripts on both remote windows and remote linux from a local linux.

推荐答案

Fabric确实期望远程计算机上有一个bash(或至少类似bash的外壳)外壳.

Fabric really is expecting a bash (or at least bash-like) shell on the remote machine.

让结构在远程Windows机器上运行命令的最简单方法是安装适当的外壳.例如,默认情况下, cygwin 的安装中包括这样的shell.

The easiest way to get fabric to run commands on a remote windows machine is to install an appropriate shell. For example, an install of cygwin includes such a shell by default.

这篇关于结构无法在Windows上执行远程命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆