SoFunction
Updated on 2024-11-12

python operation ssh realize server log download method

This article example describes the python operation ssh to realize the server log download method. Shared for your reference. Specific implementation methods are as follows:

#coding:utf-8
"""
  Example of ssh operation Implemented server log download
  2012-08-24
  yywolf
"""
import paramiko
import time
hostname="????"
port=22
username="app"
password="????"
if __name__=="__main__":
#  .log_to_file('')
  s = ()
  s.load_system_host_keys()
  s.set_missing_host_key_policy(()) 
  (hostname,port,username,password,timeout=4)
  stdin,stdout,stderr = s.exec_command("sh ~/log/check")
  print ()
  ()
  #sftp
  t = ((hostname,port))
  (username=username,password=password)
  sftp = .from_transport(t)
  files = ("/home/app/log/")
  for f in files:
    print f
  filetime = ('%Y-%m-%d',(()))
  #The file to be downloaded and the name of the downloaded file.
  ("/home/app/log/","C:\\Users\\Administrator\\Desktop\\")   
  ("/home/app/log/"+filetime+".","C:\Users\Administrator\Desktop\\"+filetime+".")
  #RASkey
  pkey_file = "E:\\yy\\tools\\key\\"
  key = .from_private_key_file(pkey_file)
  s = ()
  s.load_system_host_keys()
  s.set_missing_host_key_policy(()) 
  (hostname,port,username,pkey=key)
  stdin, stdout, stderr = s.exec_command("ls -l /home/app/log")
  print ()
  ()
  raw_input()

I hope that what I have described in this article will help you in your Python programming.