Skip to content

Commit e2e2dc1

Browse files
committed
first
1 parent 23d0702 commit e2e2dc1

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

remote_cmd.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/python
2+
#coding:utf-8
3+
import paramiko
4+
import sys
5+
import datetime
6+
7+
if len(sys.argv) < 2 :
8+
print "\033[41mPlease input iplist and cmd,like 'python remote_cmd.py iplist ls'\033[0m"
9+
sys.exit()
10+
iplist=sys.argv[1]
11+
cmd=sys.argv[2]
12+
now=datetime.datetime.now()
13+
file=open(iplist)
14+
15+
for l in file.readlines():
16+
if len(l)==1 or l.startswith('#'):
17+
continue
18+
# print l,
19+
f=l.split()
20+
#print f
21+
hostip=f[0]
22+
username=f[1]
23+
password=f[2]
24+
port=f[3]
25+
print "\033[42m---------------------------------%s---------------------------\033[0m" %hostip
26+
try:
27+
#paramiko.util.log_to_file('ssh.log')
28+
s=paramiko.SSHClient()
29+
s.load_system_host_keys()
30+
s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
31+
s.connect(hostname=hostip,port=port,username=username, password=password)
32+
stdin,stdout,stderr=s.exec_command(cmd)
33+
print stdout.read()
34+
s.close()
35+
except Exception,ex:
36+
print "\n",hostip,":\t",ex,"\n"
37+
ssh_errors=open("ssh_errors.log","a")
38+
ssh_errors.write("%s\t%s:\t%s\n"%(now,hostip,ex))
39+
ssh_errors.close()
40+
pass
41+
file.close()

remote_file.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/python
2+
import paramiko
3+
import sys
4+
import datetime
5+
6+
7+
if len(sys.argv) < 2 :
8+
print '\033[41mPlease input iplist and command,like "python remote_cmd.py iplist upload_filename"\033[0m'
9+
sys.exit()
10+
11+
iplist=sys.argv[1]
12+
local_dir=sys.argv[2]
13+
remote_dir='/tmp/'+local_dir
14+
now=datetime.datetime.now()
15+
16+
file=open(iplist,'r')
17+
18+
if __name__ == "__main__" :
19+
for l in file.readlines():
20+
if len(l)== 1 or l.startswith('#'):
21+
continue
22+
f=l.split()
23+
hostip=(f[0])
24+
username=(f[1])
25+
password=(f[2])
26+
port=int(f[3])
27+
# print type(port)
28+
# paramiko.util.log_to_file('sftp.log')
29+
try:
30+
t=paramiko.Transport((hostip,port))
31+
t.connect(username=username,password=password)
32+
sftp=paramiko.SFTPClient.from_transport(t)
33+
sftp.put(local_dir,remote_dir)
34+
print "Upload file \033[31m%s\033[0m to \033[32m%s\033[0m:\033[33m%s\033[0m : %s" %(local_dir,hostip,remote_dir,now)
35+
sftp.close()
36+
t.close()
37+
except Exception,ex:
38+
print "\n",hostip,":\t",ex,"\n"
39+
ssh_errors=open("ssh_errors.log","a")
40+
ssh_errors.write("%s\t%s:\t%s\n"%(now,hostip,ex))
41+
ssh_errors.close()
42+
pass
43+
44+
file.close()

0 commit comments

Comments
 (0)