-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpssh.py
64 lines (56 loc) · 1.53 KB
/
pssh.py
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
#coding:utf-8
import pexpect
import getpass
import time
import sys
def ssh_cmd(ip, user, passwd, cmds):
'''cmds is a command list'''
prompt = '[$#>\]]'
ssh = pexpect.spawn('ssh %s@%s ' % (user, ip))
try:
i = ssh.expect(['continue connecting (yes/no)?','[pP]assword:'],timeout=3)
if i == 0 :
ssh.sendline('yes')
ssh.expect('[pP]assword:')
ssh.sendline(passwd)
elif i == 1:
ssh.sendline(passwd)
except pexpect.TIMEOUT:
print 'connected timeout!'
ssh.close()
except pexpect.EOF:
ssh.close()
vendor = ssh.expect(['Nexus', 'Hangzhou H3C'],timeout=2)
if vendor == 0:
pass
#print 'NX-OS'
elif vendor == 1:
pass
#print 'H3C'
ssh.expect(prompt,timeout=2)
for cmd in cmds:
ssh.sendline(cmd)
ssh.expect(prompt,timeout=5)
time.sleep(1)
print '--------%s : %s--------' % (ip,cmd)
print ssh.before.lstrip(cmd)
print ''
ssh.close()
def usage():
print '\n\tUsage : python %s username\n' % sys.argv[0]
sys.exit(0)
if __name__ == '__main__':
if len(sys.argv) < 2:
usage()
user = sys.argv[1]
passwd = getpass.getpass()
hosts = []
cmds = []
with open('hosts.ip','r') as hostfile:
for i in hostfile:
hosts.append(i.strip())
with open('cmds.txt','r') as commands:
for i in commands:
cmds.append(i.strip())
for host in hosts:
ssh_cmd(host,user,passwd,cmds)