Python operation SSH

From , 2 Years ago, written in Python, viewed 232 times.
URL https://pastebin.vip/view/e992111e
  1. #coding:utf-8
  2. """
  3.    ssh操作例子 实现了服务器日志下载
  4.    2012-08-24
  5.    yywolf
  6. """
  7. import paramiko
  8. import time
  9.  
  10. hostname="????"
  11. port=22
  12. username="app"
  13. password="????"
  14. if __name__=="__main__":
  15. #    paramiko.util.log.log_to_file('paramiko.log')
  16.     s = paramiko.SSHClient()
  17.     s.load_system_host_keys()
  18.     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  19.     s.connect(hostname,port,username,password,timeout=4)
  20.     stdin,stdout,stderr = s.exec_command("sh ~/log/check")
  21.     print stdout.read()
  22.     s.close()
  23.    
  24.    
  25.     #sftp
  26.     t = paramiko.Transport((hostname,port))
  27.     t.connect(username=username,password=password)
  28.     sftp = paramiko.SFTPClient.from_transport(t)
  29.     files = sftp.listdir("/home/app/log/")
  30.     for f in files:
  31.         print f
  32.    
  33.     filetime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
  34.     #需要下载的文件 和下载后的文件名
  35.     sftp.get("/home/app/log/server.txt","C:\\Users\\Administrator\\Desktop\\server.txt")    
  36.     sftp.get("/home/app/log/"+filetime+".log.zip","C:\Users\Administrator\Desktop\\"+filetime+".log.zip")
  37.    
  38.     #RASkey
  39.     pkey_file = "E:\\yy\\tools\\key\\rsa.txt"
  40.     key = paramiko.RSAKey.from_private_key_file(pkey_file)
  41.     s = paramiko.SSHClient()
  42.     s.load_system_host_keys()
  43.     s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  44.     s.connect(hostname,port,username,pkey=key)
  45.     stdin, stdout, stderr = s.exec_command("ls -l /home/app/log")
  46.     print stdout.read()
  47.     s.close()
  48.  
  49.     raw_input()
  50. #//python/5635

Reply to "Python operation SSH"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website