python paramiko ssh

python paramiko ssh,python,ssh,paramiko,Python,Ssh,Paramiko,我是python新手。我编写了一个脚本来连接到主机并执行一个命令 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=pw) print 'running remote command' stdin, stdout, stderr = ssh.exec_command(command) std

我是python新手。我编写了一个脚本来连接到主机并执行一个命令

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, username=user, password=pw)

print 'running remote command'

stdin, stdout, stderr = ssh.exec_command(command)
stdin.close()

for line in stdout.read().splitlines():
    print '%s$: %s' % (host, line)
    if outfile != None:
        f_outfile.write("%s\n" %line)

for line in stderr.read().splitlines():
    print '%s$: %s' % (host, line + "\n")
    if outfile != None:
        f_outfile.write("%s\n" %line)

ssh.close()

if outfile != None:
    f_outfile.close()

print 'connection to %s closed' %host

except:
   e = sys.exc_info()[1]
   print '%s' %e

当远程命令不需要tty时,工作正常。我找到了一个invoke_shell示例。我对这个解决方案不满意,因为如果服务器有一个不是在我的脚本->无限循环中指定的提示,或者脚本中指定的提示是返回文本->中的字符串,则不会收到所有数据。是否有更好的解决方案,比如在我的脚本中发送stdout和stderr?

有大量的paramiko API文档,您可以在以下位置找到:

我使用以下方法在受密码保护的客户端上执行命令:

import paramiko

nbytes = 4096
hostname = 'hostname'
port = 22
username = 'username' 
password = 'password'
command = 'ls'

client = paramiko.Transport((hostname, port))
client.connect(username=username, password=password)

stdout_data = []
stderr_data = []
session = client.open_channel(kind='session')
session.exec_command(command)
while True:
    if session.recv_ready():
        stdout_data.append(session.recv(nbytes))
    if session.recv_stderr_ready():
        stderr_data.append(session.recv_stderr(nbytes))
    if session.exit_status_ready():
        break

print 'exit status: ', session.recv_exit_status()
print ''.join(stdout_data)
print ''.join(stderr_data)

session.close()
client.close()

被接受的答案有问题,它有时(随机)从服务器带来一个裁剪响应。我不知道为什么,我没有调查被接受答案的错误原因,因为该代码对我非常有效:

import paramiko

ip='server ip'
port=22
username='username'
password='password'

cmd='some useful command' 

ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)

stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)

stdin,stdout,stderr=ssh.exec_command('some really useful command')
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)

@ThePracticalOne的代码非常适合显示用法,但有一点除外: Somtimes输出将不完整。(
session.recv\u ready()
if session.recv\u ready():
while
session.recv\u stderr\u ready()
进入下一个循环之前,退出\u status\u ready()

因此,我的想法是在数据准备退出会话时检索数据

while True:
if session.exit_status_ready():
while True:
    while True:
        print "try to recv stdout..."
        ret = session.recv(nbytes)
        if len(ret) == 0:
            break
        stdout_data.append(ret)

    while True:
        print "try to recv stderr..."
        ret = session.recv_stderr(nbytes)
        if len(ret) == 0:
            break
        stderr_data.append(ret)
    break

不应该使用
data\u block
而应该使用
session.recv(4096)
session.recv\u stderr(4096)
例如(
data\u block
是从哪里来的?)。你是对的,data\u block如你所述(recv/recv\u stderr),但我意外地删除了这些行。截至2015年5月,Paramiko文档截至2015年12月,Paramiko文档(版本1.16)在这里:这段代码中有一些错误:它可以工作,但有时响应没有完全剪裁。我也看到了这一点,它就像一个带有/exit_status_ready()和recv_ready()的竞赛条件,以防有人希望使用上述方法捕获退出代码-stdout.channel.recv_exit_status()有一个例外:密钥不能用于签名参考:参考:
###### Use paramiko to connect to LINUX platform############
import paramiko

ip='server ip'
port=22
username='username'
password='password'
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)

--------Connection Established-----------------------------

######To run shell commands on remote connection###########
import paramiko

ip='server ip'
port=22
username='username'
password='password'
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)


stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp) # Output