Execute commands in batches, upload and download scripts, and execute commands to download files.
Preface:
In a medium-sized company, nearly 2000 servers in each province in China do not have an O & M platform. I am also drunk. I log on to the machine one by one every day, and the management machine is really tricky, after my work, I used the paramiko module to write a batch command to upload and download a file script. The code I wrote was so bad that I was crying, but at least I could use it together.
The environment is python2.6-2.7, in which a ssh_login.txt file is generated under the current directory. It is a file for the user to save the login username, password, and host name. The format is as follows:
Host Name, port, user name, authentication method: Public/Private Key Authentication or password authentication, password, # each line ends with a comma, do not allow blank lines,
172.1619.25, 22, root, password, xxxxx, 172.1619.26, 22, root, password, xxxxx,
The script source code is as follows:
Python script name [-c command]-m put/down-l local_file_path-r remote_file_path
-C is used to execute commands in batches. the upload/download function is not used at this time.
-M specifies the mode, whether to download or upload
-L specifies the local file path
-R is the remote file path and must be an absolute path. You need to write the file name.
-H is used to capture help information.
For example, I want to execute commands in batches -- python xxx. py-c "uptime; df-hT"
For example, I want to upload files in batches -- python xxx. py-m put-l/tmp/123.txt-r/tmp/123.txt
For example, I want to download files in batches now -- python xxx. py-m down-l/tmp/123.txt-r/tmp/123.txt
Note that if the download mode is used, the downloaded file format is as follows: file name _ Host ip Address
If an error occurs during running, an error. log is generated in the current directory, which records error messages such as host login failure or connection failure.
By default, 10 processes are started, and the indentation alignment may be messed up when the copy is pasted. If you have any questions, you can contact my email: 18500777133@sina.cn.
#/Usr/bin/env pythonimport paramikoimport sysimport osimport getoptimport multiprocessingimport timeimport socketpkey_file = '/root /. ssh/id_rsa 'if OS .path.exists('ssh_login.txt ') is not True: print' \ 033 [31mNo such the ssh_login.txt \ 033 [0m' sys. exit () def ssh_connect (cmd, line, host): s = paramiko. SSHClient () s. set_missing_host_key_policy (paramiko. autoAddPolicy () s. load_system_host_keys () # paramiko.com Mon. logging. basicConfig (level = paramiko. common. DEBUG) Output # paramiko DEBUG port = int (line. split (',') [1]) user = line. split (',') [2] auth_method = line. split (',') [3] if auth_method = 'Password': password = line. split (',') [4] s. connect (host, port, user, password, timeout = 3) else: key = paramiko. RSAKey. from_private_key_file (pkey_file) s. connect (host, port, user, key, timeout = 3) stdin, stdout, stderr = s.exe c_command (cmd) res Ult = stdout. read (), stderr. read () print '\ 033 [32 m =' * 25 + "the result from ", host + "\ 033 [32 m = \ 033 [0 m" * 25 for a in result: print a s. close () def sftp_put (line, host, lfile, rfile): port = int (line. split (',') [1]) user = line. split (',') [2] auth_method = line. split (',') [3] if auth_method = 'Password': try: passwd = line. split (',') [4] t = paramiko. transport (host, port) t. connect (username = user, password = passwd) sftp = par Amiko. SFTPClient. from_transport (t) sftp. put (lfile, rfile) print "\ 033 [32 m I put % s to % s now, please waite a moment ..... \ 033 [0m \ n "% (lfile, host) print sftp. stat (rfile) t. close (); handle T (IOError): print host, "\ 033 [31 m -- Must be input remote file absolute path !! \ 033 [0 m"
Else:
Try: key = paramiko. RSAKey. from_private_key_file (pkey_file) t = paramiko. transport (host, port) t. connect (username = user, pkey = key) sftp = paramiko. SFTPClient. from_transport (t) sftp. put (lfile, rfile) print "\ 033 [32 m I put % s to % s now, please waite a moment ..... \ 033 [0m \ n "% (lfile, host) print sftp. stat (rfile) t. close (); handle T (IOError): print host, "\ 033 [31 m -- Must be input remote file absolute path !! \ 033 [0 m "def sftp_down (line, host, rfile, lfile): port = int (line. split (',') [1]) user = line. split (',') [2] auth_method = line. split (',') [3] if auth_method = 'Password': try: passwd = line. split (',') [4] t = paramiko. transport (host, port) t. connect (username = user, password = passwd) sftp = paramiko. SFTPClient. from_transport (t) sftp. get (rfile, lfile) OS. rename (lfile, lfile + "_" + host) print "\ 033 [32 m I down % s from % s now, Please waite a moment ..... \ 033 [0 m "% (rfile, host) print sftp. stat (rfile) t. close (); handle T (IOError): print host, "\ 033 [31 m -- Must be input remote file absolute path !! \ 033 [0 m "else: try: key = paramiko. RSAKey. from_private_key_file (pkey_file) t = paramiko. transport (host, port) t. connect (username = user, pkey = key) sftp = paramiko. SFTPClient. from_transport (t) sftp. get (rfile, lfile) OS. rename (lfile, lfile + '_' + host) print "\ 033 [32 m I down % s from % s now, please waite a moment ..... \ 033 [0 m "% (rfile, host) print sftp. stat (rfile) t. close (); handle T (IOError): print host, "\ 033 [31 m -- Must be input remote file absolute path !! \ 033 [0 m "# main_programp = multiprocessing. pool (processes = 10) opts, args = getopt. getopt (sys. argv [1:], "c: m: h: r: l:") opts = dict (opts) cmd = opts. get ('-C') # capture-c this parameter pd = opts. get ('-m') # capture-m, the following is the same as l_file = opts. get ('-l') r_file = opts. get ('-R') help = opts. get ('-H') if help is not None: print "\ 033 [35 mUseag: % s [-c command]-m put/down-l local_file_path-r remote_file_path \ 033 [0 m "% sys. argv [0] result = [] if cmd is not Non E: login_info1_open('ssh_login.txt ', 'R') err_log = open ('error. log', 'A') err_log.write ('=' * 30 + 'this is last result' +' = '* 30 +' \ n') for line in login_info: try: host = line. split (',') [0] result. append (p. apply_async (ssh_connect, (cmd, line, host,) handle T BaseException, e: print "\ 033 [46 mError Info from % s \ 033 [0 m:" % host, e, '\ n \ n' err_log.write (str (time. ctime () + "| Error Info from" + str (host) + ':' + str (e) + '\ N') Continue p. close () p. join () print "\ 033 [36m \ t \ tProgram Run Out !! \ 033 [0 m "err_log.flush () err_log.close () login_info.close () else: if pd = 'put': if l_file and r_file is not None: Comment ', 'R ') err_log = open ('error. log', 'A') err_log.write ('=' * 30 + 'this is last result' +' = '* 30 +' \ n') for line in login_info: try: host = line. split (',') [0] result. append (p. apply_async (sftp_put, (line, host, l_file, r_file,) handle T BaseException, e: print "\ 033 [46 mError Info from % s \ 033 [0 m: "% host, e, '\ n \ n' err_log.write (str (time. ctime () + "| Error Info from" + str (host) + ':' + str (e) + '\ n ') continue print "\ 033 [36m \ t \ tProgram Run Out !! \ 033 [0 m "p. close () p. join () err_log.flush () err_log.close () login_info.close () else: print "\ 033 [36 m-l local_file_path-r remote_file_path \ 033 [0 m" elif pd = 'drop': if l_file and r_file is not None: login_info1_open('ssh_login.txt ', 'R') err_log = open ('error. log', 'A') err_log.write ('=' * 30 + 'this is last result' +' = '* 30 +' \ n') for line in login_info: try: host = line. split (',') [0] result. append (p. apply_asyn C (sftp_down, (line, host, l_file, r_file,) handle T BaseException, e: print "\ 033 [46 mError Info from % s \ 033 [0 m: "% host, e, '\ n \ n' err_log.write (str (time. ctime () + "| Error Info from" + str (host) + ':' + str (e) + '\ n') continue p. close () p. join () print "\ 033 [36m \ t \ tProgram Run Out !! \ 033 [0 m "err_log.flush () err_log.close () login_info.close () else: print" \ 033 [36 m-l local_file_path-r remote_file_path \ 033 [0 m "else: print "\ 033 [31 m The Mode Is Put Or Down !! \ 033 [0 m"