# Coding: UTF-8 """ The ssh operation example implements server log download. 2012-08-24 Yywolf """ Import paramiko Import time Hostname = "???? " 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 () |