大佬教程收集整理的这篇文章主要介绍了python paramiko ssh,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您可以在以下网址找到大量的paramiko api文档:http://docs.paramiko.org/en/stable/index.HTML
我使用以下方法在受密码保护的客户端上执行命令:
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()
我是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时,可以正常工作。我找到了一个与Paramiko的invoke_sHell示例嵌套SSH会话。我对这种解决方案不满意,因为如果服务器上的提示未在我的脚本中指定->无限循环,或者脚本中指定的提示是返回文本中的字符串->不会接收到所有数据。有没有更好的解决方案,也许将stdout和stderr像我的脚本一样发送回去?
以上是大佬教程为你收集整理的python paramiko ssh全部内容,希望文章能够帮你解决python paramiko ssh所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。