1
|
1 #!/usr/bin/env python
|
|
2 '''This runs a COMMAND on a remote host using SSH.
|
|
3 At the prompts enter password.
|
|
4 '''
|
|
5 import pexpect
|
|
6 import getpass
|
|
7
|
|
8 import getopt
|
|
9 import sys
|
|
10 import os
|
|
11 import re
|
|
12
|
|
13 def ssh_command (user, host, password, command):
|
|
14 """This runs a command on the remote host. This returns a
|
|
15 pexpect.spawn object. This handles the case when you try
|
|
16 to connect to a new host and ssh asks you if you want to
|
|
17 accept the public key fingerprint and continue connecting.
|
|
18 """
|
|
19 ssh_newkey = 'Are you sure you want to continue connecting.*'
|
|
20
|
|
21 child = pexpect.spawn('ssh -l %s %s'%(user, host),
|
|
22 timeout=30)
|
|
23 i = child.expect([pexpect.TIMEOUT, ssh_newkey, '.*ssword:.*'])
|
|
24 while i:
|
|
25 if i == 1: # SSH does not have the public key. Just accept it.
|
|
26 child.sendline ('yes')
|
|
27 i = child.expect([pexpect.TIMEOUT, ssh_newkey, '.*ssword:.*'])
|
|
28 elif i == 2:
|
|
29 # input password
|
|
30 child.sendline(password)
|
|
31 child.sendline(command)
|
|
32 return child
|
|
33 else:
|
|
34 print child.before, child.after
|
|
35
|
|
36 print 'ERROR!'
|
|
37 print 'SSH could not login. Here is what SSH said:'
|
|
38 print child.before, child.after
|
|
39 return None
|
|
40
|
|
41 def gethostlist(hostlistfile):
|
|
42
|
|
43 # get host name list from file
|
|
44 hostlist = re.split('\s+', open(hostlistfile).read())
|
|
45
|
|
46 if hostlist[-1] == '':
|
|
47 hostlist.pop()
|
|
48
|
|
49 if hostlist[0] == '':
|
|
50 hostlist.pop(0)
|
|
51
|
|
52 dellist = []
|
|
53 for h in hostlist:
|
|
54 if h[0] == '#':
|
|
55 print "remove",h
|
|
56 dellist.append(h)
|
|
57
|
|
58 for d in dellist:
|
|
59 hostlist.remove(d)
|
|
60
|
|
61 return hostlist
|
|
62
|
|
63
|
|
64 def main(user, hostlist, command, waitprc=False):
|
|
65 # get password
|
|
66 password = getpass.getpass('Password: ')
|
|
67
|
|
68 childlist = []
|
|
69
|
|
70 if not waitprc:
|
|
71 command = command + ' &'
|
|
72
|
|
73 for host in hostlist:
|
|
74 child = ssh_command(user, host, password, command)
|
|
75 if child is not None:
|
|
76 print "".join([user, '@', host, ' : ', command])
|
|
77 childlist.append(child)
|
|
78 if waitprc:
|
|
79 print child.expect(pexpect.EOF)
|
|
80 print child.before
|
|
81
|
|
82 # end main
|
|
83
|
|
84
|
|
85 def usage(program_name):
|
|
86 print "Usage : %s [-w] [-h hostname] [-l username] [-f hostlist] command" % program_name
|
|
87 sys.exit(1)
|
|
88
|
|
89
|
|
90 if __name__ == "__main__":
|
|
91 if (len(sys.argv) < 2) :
|
|
92 usage(sys.argv)
|
|
93
|
|
94 try:
|
|
95 opts, args = getopt.getopt(sys.argv[1:], "h:l:f:w")
|
|
96 except getopt.GetoptError:
|
|
97 usage(sys.argv[0])
|
|
98
|
|
99 # default
|
|
100 user = os.environ['USER']
|
|
101 hostlist = ["localhost"]
|
|
102 waitprc = False
|
|
103
|
|
104 for o, a in opts:
|
|
105 if o == '-l':
|
|
106 user = a
|
|
107 elif o == '-w':
|
|
108 waitprc = True # show children's output
|
|
109 elif o == '-f':
|
|
110 hostlist = gethostlist(a)
|
|
111 elif o == '-h':
|
|
112 hostlist = [a]
|
|
113 else:
|
|
114 pass
|
|
115
|
|
116 # execute command on remote host
|
|
117 command = args[0]
|
|
118
|
|
119 main(user, hostlist, command, waitprc)
|