1 #!/usr/bin/env python
2 #coding:utf8
3 #####################################################
4 #Author: wangganyu188 wangganyu188@gmail.com
5 #Last modified: 2014-09-05
6 #Filename: sys_ops.py
7 #####################################################
8
9
10
11 from fabric.api importenv,run,put,get12 from os importpath13 from re importfindall14 from sys importargv15 from fabric.context_managers importhide16 from time importsleep17
18
19
20 USER='root'
21 HOST,IP_LIST=[],[]22 PORT='22'
23 timeout = 1
24 CMD,getSRC,getDST,putSRC,putDST = '','','','',''
25
26 for i in range(1,len(argv)+1):27 #print i
28 if argv[i-1] == '-h' or len(argv) == 1:29 print """
30 USAGE:31 -c [cmd] The command you want the host(s) to run32 -f [file] The file content multiple ip address you want to connect33 -P [put] The local file that you want to upload to the remote host(s)34 -G [get] The remote file that you want to download to the local host35 -h [help] Print this help screen36 """
37
38
39 if argv[i-1]=='-f':40 if path.isfile('%s'%(argv[i])) ==True:41 file_list = open('%s'%(argv[i]),'r').readlines()42 for line infile_list:43 #print line
44 HOSTIP =line.split()[0]45 HOSTPW = line.split()[1]46 #print HOSTIP,'\n',HOSTPW
47 IP_LIST.append(HOSTIP)48 env.password ='%s'%HOSTPW49 #print IP_LIST,'\n',env.password
50 if argv[i-1] == '-c':51 CMD =argv[i]52
53 if argv[i-1] == '-P':54 p = src = argv[i].split(',')55 putSRC =p[0]56 putDST = p[1]57
58
59 if argv[i-1] == '-G':60 g = src = argv[i].split(',')61 getSRC =g[0]62 getDST = g[1]63
64 else:65 IP_PORT =[]66 if len(IP_LIST) !=0:67 for ip inIP_LIST:68 IP_PORT.append(ip + ':' +PORT)69
70
71
72 if CMD !='':73 defcommand():74 with hide('running'):75 run("%s"%CMD)76
77 for ipport inIP_PORT:78 env.host_string =ipport79 print "Execute Command : \033[1;33;40m %s\033[0m at Host : \033[1;33;40m %s \033[0m" %(CMD,ipport.split(':')[0])80 print "***************************************************************"
81 command()82 print '***************************************************************'
83
84 if putSRC and putDST != '':85 defPUTupload():86 with hide('running'):87 put("%s"%(putSRC),"%s"%(putDST))88 for ipport inIP_PORT:89 env.host_string =ipport90 print "PUT local file:\033[1;33;40m %s \033[0m to remote HOST:\033[1;33;40m %s\033[0m : \033[1;33;40m %s\033[0m" %(putSRC,ipport.split(':')[0],putDST)91 print "*****************************************************************"
92 PUTupload()93 print "*****************************************************************"
94
95
96 if getSRC and getDST != '':97 defGETdown():98 with hide('running'):99 get("%s"%(getSRC),"%s"%(getDST))100 for ipport inIP_PORT:101 env.host_string =ipport102 print "GET remote file:\033[1;33;40m %s \033[0m from host :\033[1;33;40m %s\033[0m to local \033[1;33;40m %s\033[0m" %(getSRC,ipport.split(':')[0],getDST)103 print "*****************************************************************"
104 GETdown()105 print "*****************************************************************"