我的服务器正在运行带有 freeSSHd 的 Windows 7 以允许 ssh 连接。我正在尝试使用 paramiko 在该服务器上远程执行程序 MyProgram.exe。这是完成其工作的代码的相关部分。尽管如此,它仍然需要改进。
import paramiko
import time
cmds = 'xcopy file1 backup\\file1 & xcopy file2 backup\\file2 & MyProgram.exe file1 file2'
final_cmds = 'cmd.exe /c "' + final_cmds + '"'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ipaddr = socket.gethostbyname(hostname)
ssh.connect(ipaddr, port=port, username=username, password=password)
transport = ssh.get_transport()
channel = transport.open_session()
channel.setblocking(1)
channel.settimeout(None)
channel.set_combine_stderr(1)
channel.exec_command(final_cmds)
while True:
try:
if channel.exit_status_ready():
if channel.recv_ready():
output = channel.recv(1024)
break
except:
print 'Exception'
break
time.sleep(2)
ssh.close()
上面代码的问题是它在服务器端打开了一个新的 cmd 窗口。我想在后台运行 cmd.exe 或者至少最小化但是如果我改变了行
final_cmds = 'cmd.exe /c "' + final_cmds + '"'
进入
final_cmds = 'start /b cmd.exe /c "' + final_cmds + '"'
或者
final_cmds = 'start /min cmd.exe /c "' + final_cmds + '"'
甚至
final_cmds = 'start cmd.exe /c "' + final_cmds + '"'
我收到一个错误:“无法在远程系统上执行命令或 shell:无法执行进程。” 有任何想法吗?
谢谢