Batch operation of remote host scripts (command execution, file upload and download) Written in Python 12:53:44
Tags: Servers
Script
SSH
Python
Centralized management of original works, reprinting allowed, please be sure to mark the article in hyperlink form during Reprinting
Original source, author information, and this statement. Otherwise, legal liability will be held. Http://mayulin.blog.51cto.com/1628315/555459
I recently learned Python and used the fabric module to write a script for batch server operations. I would like to share it with you. If you have any shortcomings, please let me know.
Preparations:
Install Python 2.6.5:
Yum-y install Readline *
Tar xf Python-2.6.5.tar.bz2
CD Python-2.6.5
Remove the comments before "Readline. C-lreadline-ltermcap" in the modules/setup. Dist file in the directory.
Compile and install:
./Configure -- enable-shared
Make-J8 & make install
Install setuptools
Tar xf setuptools-0.6c11.tar.gz
CD setuptools-0.6c11
Python setup. py install
Install Fabric
During installation, the software will automatically find and install the dependent installation package from the Internet
Tar xf fabric-0.9rc2.tar.gz
CD goosemo-fabric-1eacbf2
Python setup. py install
######################################## ####################################
Pyssh script:
#! /Usr/bin/ENV Python #-*-Coding: UTF-8 -*-######################################## ############# # Author: mayulin-mayulin@cy2009.com # Last modified: # Filename: Pssh. py ######################################## ############# From fabric. API import ENV, run, put, get From OS import path From re import findall From sys import argv From fabric. context_managers import hide From time import sleep User = 'root' Host, ip_list = [], [] Port = '22' Pri_key, password, CMD, usrc, udst, DSRC, ddst = '','','','','','','' Timeout = 1 For I in range (1, Len (argv) + 1 ): If argv [I-1] = '-H' or Len (argv) = 1: Print """ Usage: -U [user] use this argument to specify the user, default is 'root' -H [host] The host that you want to connect -F the file content multiple IP address you want to connect -P [port] the ssh port, default is 22 -P [PWD | file] You can specify password or a priviate key file to connect the host -C [command] The command you want the host (s) to run -U [SRC, DST] the local file that you want to upload to the remote host (s) -D [SRC, DST] the remote file that you want to download to the local host -T [timeout] the program running timeout, default is 1 (s) -H print this help screen """ If argv [I-1] = '-U ': User = argv [I] Env. User = '% s' % (User) Else: Env. User = '% s' % (User) If argv [I-1] = '-H ': Arg = findall ('(\ D + \. \ D + \. \ D + \. \ D + | \ s + \. {3, 4}) ', argv [I]) For J in Arg: If type (j). _ name __! = 'Nonetype ': Host. append (j) Else: Print 'the hostip input error' If argv [I-1] = '-P ': Port = argv [I] If argv [I-1] = '-F ': If path. isfile ('% s' % (argv [I]) = true: Ip_list = open ('% s' % (argv [I]), 'R'). readlines () If argv [I-1] = '-P ': If path. isfile (argv [I]) = true: Pri_key = argv [I] Env. key_filename = '% s' % (pri_key) Else: Password = argv [I] Env. Password = '% s' % (password) If argv [I-1] = '-C ': Cmd = argv [I] If argv [I-1] = '-t ': Timeout = argv [I] SLP = 'sleep % s' % (timeout) If argv [I-1] = '-U ': X = src = \ '# \' "/> usrc = x [0] Udst = x [1] If argv [I-1] = '-d ': Y = src = \ '# \' "/> DSRC = Y [0] Ddst = Y [1] Else: Ip_port = [] If Len (ip_list )! = 0: For k in ip_list: Ip_port.append (K. Strip () + ':' + port) If Len (host )! = 0: For k in Host: Ip_port.append (K. Strip () + ':' + port) If cmd! = '': Def command (): With hide ('running '): Run ("% s; % s" % (CMD, SLP )) For IP in ip_port: Env. host_string = IP Print "Execute Command: \" % s \ "at host: % s" % (CMD, IP. Split (':') [0]) Print "-------------------------------------------------" Command () Print "-------------------------------------------------"
If usrc and udst! = '': Def upload (): With hide ('running '): Put ("% s" % (usrc), "% s" % (udst )) For IP in ip_port: Env. host_string = IP Print "upload local file: \" % s \ "to host: % s \" % s \ "" % (usrc, IP. split (':') [0], udst) Print "-------------------------------------------------" Upload () Print "-------------------------------------------------"
If DSRC and ddst! = '': Def download (): With hide ('running '): Get ("% s" % (DSRC), "% s" % (ddst )) For IP in ip_port: Env. host_string = IP Print "Download Remote File: \" % s \ "from host: % s to Local \" % s \ "" % (DSRC, IP. split (':') [0], ddst) Print "-------------------------------------------------" Download () Print "-------------------------------------------------" |
######################################## ######################################## ###########
Script Execution result:
Show Help:
Run the shell command on multiple remote hosts:
Download files from the remote host to the local directory:
Upload local files to multiple remote hosts
This article is from dream ~ Starting from here "blog, be sure to keep this source http://mayulin.blog.51cto.com/1628315/555459