This article mainly introduces how to download server logs through ssh in python. It involves the skills related to establishing an ssh connection in Python and downloading server logs, for more information about how to download server logs using python ssh, see the following example. Share it with you for your reference. The specific implementation method is as follows:
# Coding: UTF-8 "ssh operation example implements server log download 2012-08-24 yywolf" import paramikoimport timehostname = "???? "Port = 22 username =" app "password = "???? "If _ name __= =" _ main _ ": # paramiko. util. log. log_to_file ('paramiko. log ') s = paramiko. SSHClient () s. load_system_host_keys () s. set_missing_host_key_policy (paramiko. autoAddPolicy () s. connect (hostname, port, username, password, timeout = 4) stdin, stdout, stderr = s.exe c_command ("sh ~ /Log/check ") print stdout. read () s. close () # sftp t = paramiko. transport (hostname, port) t. connect (username = username, password = password) sftp = paramiko. SFTPClient. from_transport (t) files = sftp. listdir ("/home/app/log/") for f in files: print f filetime = time. strftime ('% Y-% m-% d', time. localtime (time. time () # The file to be downloaded and the downloaded file name sftp. get ("/home/app/log/server.txt", "C: \ Users \ Administrator \ Desktop \ server.txt") sftp. get ("/home/app/log/" + filetime + ".log.zip", "C: \ Users \ Administrator \ Desktop \" + filetime + ".log.zip ") # RASkey pkey_file = "E: \ yy \ tools \ key \ rsa.txt" key = paramiko. RSAKey. from_private_key_file (pkey_file) s = paramiko. SSHClient () s. load_system_host_keys () s. set_missing_host_key_policy (paramiko. autoAddPolicy () s. connect (hostname, port, username, pkey = key) stdin, stdout, stderr = s.exe c_command ("ls-l/home/app/log") print stdout. read () s. close () raw_input ()
I hope this article will help you with Python programming.