设为首页 收藏本站
查看: 1141|回复: 0

[经验分享] python paramiko ssh sftp

[复制链接]

尚未签到

发表于 2018-8-7 07:57:55 | 显示全部楼层 |阅读模式
  Working with paramiko
  

  SSHClient is the main>  Here’s a simple example:
  1
  import paramiko
  2
  ssh = paramiko.SSHClient()
Another way is to use an SSH key:  1
  import paramiko
  2
  import os
  3
  privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
  4
  mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
  5
  ssh.connect('192.168.1.2', username = 'vinod', pkey = mykey)
  注意:(这里的key,用的是RSA的key,我们在用ssh-keygen -t rsa来指定它,才可以在这里用,否则将会报无法识别的RSA KEY。而且如果你的RSA Key有密码的话,你还需要
  mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile,password='12345678')
  不过,我们可以用publickey来登录的。
  解法如下:
  serverHost = "127.0.0.1"
  serverPort = 22
  userName = "root"
  keyFile = "~/.ssh/badboy"
  known_host = "~/.ssh/known_hosts"
  channel = paramiko.SSHClient();
  channel.load_system_host_keys( known_host )
  channel.connect( serverHost, serverPort,username = userName, key_filename = keyFile )
  )
  Running Simple Commands
  Lets run some simple commands on a remote machine.
  1
  import paramiko
2
  ssh = paramiko.SSHClient()
3
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) <=这样的话,就会报paramiko.SSHException: Unknown server
4
  ssh.connect('beastie', username='vinod', password='secret')
5
  stdin, stdout, stderr = ssh.exec_command('df -h')
6
  print stdout.readlines()
7
  ssh.close()
  “paramiko.AutoAddPolicy()” which will auto-accept unknown keys.
  
  Using sudo in running commands:
  01
  import paramiko
  02
  03
  cmd    = &quot;sudo /etc/rc.d/apache2 restart&quot;
  04
  05
  ssh    = paramiko.SSHClient()
  06
  ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  07
  ssh.connect('beastie', username='vinod', password='secret')
  08
  stdin, stdout, stderr = ssh.exec_command(cmd)
  09
  stdin.write('secret\n')
  10
  stdin.flush()
  11
  print stdout.readlines()
  12
  ssh.close()
  在这个例子中,无法运行,也无法解释,希望志同道合的朋友能给个解释!
  Secure File Transfer Using SFTPClient
  SFTPClient is used to open an sftp session across an open ssh Transport and do remote file operations.
  An SSH Transport attaches to a stream (usually a socket), negotiates an encrypted session, authenticates, and then creates stream tunnels, called Channels, across the session. Multiple channels can be multiplexed across a single session (and often are, in the case of port forwardings).
  以下是用密码认证功能登录的
#!/usr/bin/env python
import paramiko
  socks=('127.0.0.1',22)
  testssh=paramiko.Transport(socks)
  testssh.connect(username='root',password='000000')
  sftptest=paramiko.SFTPClient.from_transport(testssh)
  remotepath=&quot;/tmp/a.log&quot;
  localpath=&quot;/tmp/c.log&quot;
  sftptest.put(remotepath,localpath)
sftptest.close()
testssh.close()
  
  以下是用DSA认证登录的(PubkeyAuthentication)
  #!/usr/bin/env python
  import paramiko
  
  serverHost = &quot;192.168.1.172&quot;
  serverPort = 22
  userName = &quot;root&quot;
  keyFile = &quot;/root/.ssh/zhuzhengjun&quot;
  known_host = &quot;/root/.ssh/known_hosts&quot;
  channel = paramiko.SSHClient();
  #host_keys = channel.load_system_host_keys(known_host)
  channel.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  channel.connect(serverHost, serverPort,username=userName, key_filename=keyFile )
  testssh=paramiko.Transport((serverHost,serverPort))
  mykey = paramiko.DSSKey.from_private_key_file(keyFile,password='xyxyxy')
  testssh.connect(username=userName,pkey=mykey)
  sftptest=paramiko.SFTPClient.from_transport(testssh)
  filepath='/tmp/e.log'
  localpath='/tmp/a.log'
  sftptest.put(localpath,filepath)
  sftptest.close()
  testssh.close()
  
  以下是用RSA Key认证登录的
  #!/usr/bin/evn python
  
  import os
  import paramiko
  
  host='127.0.0.1'
  port=22
  testssh=paramiko.Transport((host,port))
  privatekeyfile = os.path.expanduser('~/.ssh/badboy')
  mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile,password='000000')
  username = 'root'
  testssh.connect(username=username, pkey=mykey)
  sftptest=paramiko.SFTPClient.from_transport(testssh)
  filepath='/tmp/e.log'
  localpath='/tmp/a.log'
  sftptest.put(localpath,filepath)
sftptest.close()
testssh.close()

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-547873-1-1.html 上篇帖子: python读写excel 下篇帖子: python闭包简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表