1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
| #!/usr/bin/python
import paramiko
import os
import sys
import time
import multiprocessing
import datetime
ip_list = []
def Execmd(host,cmd):
time.sleep(2)
hostname=host
username="ckl"
ssh=paramiko.SSHClient()
ssh.load_system_host_keys()
privatekey = os.path.expanduser('/home/chenkangle/key/ckl.pem')
key = paramiko.RSAKey.from_private_key_file(privatekey)
ssh.connect(hostname=hostname,username=username,pkey = key)
stdin,stdout,stderr=ssh.exec_command(cmd)
print stdout.read()
ssh.close()
def Upfile(host_ip,local_path,remote_path):
time.sleep(2)
privatekey = os.path.expanduser('/home/ckl/.ssh/id_rsa')
key=paramiko.RSAKey.from_private_key_file(privatekey)
scp = paramiko.Transport((host_ip, 22))
scp.connect(username='ckl', pkey=key)
sftp = paramiko.SFTPClient.from_transport(scp)
src = local_path
des = remote_path
file=os.path.basename(src)
print '='*50
print 'Start to upload file %s ' % datetime.datetime.now()
print 'Upload file:',file
sftp.put(local_path,remote_path)
scp.close()
print '\033[1;32m Upload file success %s \033[0m' % datetime.datetime.now()
print '='*50
def Downfile(host_ip,remote_path,local_path):
privatekey = os.path.expanduser('/home/chenkangle/key/ckl.pem')
key=paramiko.RSAKey.from_private_key_file(privatekey)
scp = paramiko.Transport((host_ip, 22))
scp.connect(username='ckl', pkey=key)
sftp = paramiko.SFTPClient.from_transport(scp)
src = remote_path
file=os.path.basename(src)
des = local_path
print '='*50
print 'Start to download file %s ' % datetime.datetime.now()
print 'Download file:',file
sftp.get(src,des)
scp.close()
print '\033[1;32m download file success %s \033[0m' % datetime.datetime.now()
print '='*50
def all_ip():
f = file('2.txt','r')
c = f.readlines()
for x in c:
ip = x.split('\n')[0]
ip_list.append(ip)
f.close()
if __name__=='__main__':
all_ip()
pool = multiprocessing.Pool(processes=30)
threads = []
print "Begin......"
for i in ip_list:
#pool.apply_async(Execmd,(i,))
pool.apply_async(Upfile,(i,'/home/chenkangle/php_0420.tar.gz','/tmp/soft/php_0420.tar.gz'))
pool.close()
pool.join()
for res in threads:
print res.get()
print "process done."
|