简单执行命令可用以下方式:
connection = paramiko.SSHClient()
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
connection.connect(hostIP, 22, 'd', 'd')
command = 'xxxx'
stdin, stdout, stderr = connection.exec_command(command)
respmsg = stdout.read()
但是如果执行多条命令,则会报错paramiko.ssh_exception.SSHException: Unable to open channel.
命令执行完后,channel关闭,所以报错。(http://docs.paramiko.org/en/1.15/api/channel.html)可使用如下方式。
connection = paramiko.SSHClient()
connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
chan = connection.invoke_shell()
resp = chan.recv(9999)
commands = [xxxxxxxxxxx]
for command in commands:
chan.send(command + '\r\n')
time.sleep(1)
resp = chan.recv(999999)
print resp