我试图从Python程序中调用ssh
,但似乎忽略了这些参数。
这是Python程序:
#!/usr/bin/python
from subprocess import Popen, PIPE, call
vm_name = 'vmName with-space'
vm_host = 'user@192.168.21.230'
def ssh_prefix_list(host=None):
if host:
# return ["ssh", "-v", "-v", "-v", host]
return ["scripts/ssh_wrapper", "-v", "-v", "-v", host]
else:
return []
def start(vm_name, vm_host=None): # vm_host defaults to None
print "vm_host = ", vm_host
vbm_args = ssh_prefix_list(vm_host) + ["VBoxManage", "startvm", vm_name]
print vbm_args
return call(vbm_args, shell=True)
start(vm_name, vm_host)
包装器打印参数的数量,它们的值,并调用ssh。
#!/bin/bash
echo Number of arguments: $#
echo ssh arguments: "$@"
ssh "$@"
这是输出。
$ scripts/vm_test.py
vm_host = stephen@192.168.21.230
['scripts/ssh_wrapper', '-v', '-v', '-v', 'stephen@192.168.21.230', 'VBoxManage', 'startvm', 'atp-systest Clone']
Number of arguments: 0
ssh arguments:
usage: ssh [-1246AaCfgKkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-i identity_file] [-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-w local_tun[:remote_tun]] [user@]hostname [command]
这是在Python 2.5上。
答案 0 :(得分:0)
将call
与shell=True
一起使用时,需要传递单个字符串,而不是字符串数组。所以:
call("scripts/ssh_wrapper -v -v -v "+host+" VBoxManage startvm "+vmname)