forked from tbird20d/ttc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathssh_exec
More file actions
executable file
·115 lines (101 loc) · 3.29 KB
/
ssh_exec
File metadata and controls
executable file
·115 lines (101 loc) · 3.29 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/python
#
# ssh_exec - run a command on another machine via ssh
#
# This command requires the python module pexpect.py
# which is not in the default python distribution.
# You can get it from: http://pexpect.sourceforge.net/
#
# Copyright 2006 Sony Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation. The GNU General Public
# License is available online at: http://www.gnu.org/copyleft/gpl.html
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Author: Tim Bird <tim.bird (at) am.sony.com>
import sys
import pexpect
# These are defaults
# Normally, get host, user, password and command from command line
host = "osk2"
user = "root"
password = ""
cmd = "dmesg"
def usage():
print """Usage: ssh_exec <options>
Options:
-t <host> Specify the host to ssh to
-u <user> Specify the user name to login with
-p <password> Specify the password to use to login
-c <command> Specify the command to execute on the remote machine
-v Be verbose
"""
sys.exit(0)
def vprint(msg):
if verbose:
print msg
def dprint(msg):
if debug:
print msg
def rprint(result):
dprint("result='%s'" % result)
# ssh_command() was taken directly from the sshls.py example in
# pexpect-2.1
def ssh_command (user, host, password, command):
"""This runs a command on the remote host. This returns a
pexpect.spawn object. This handles the case when you try
to connect to a new host and ssh asks you if you want to
accept the public key fingerprint and continue connecting.
"""
ssh_newkey = 'Are you sure you want to continue connecting'
child = pexpect.spawn('ssh -o "StrictHostKeyChecking no" -l %s %s %s'%(user, host, command))
i = child.expect([pexpect.TIMEOUT, ssh_newkey, 'password: '])
if i == 0: # Timeout
print 'ERROR!'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
return None
if i == 1: # SSH does not have the public key. Just accept it.
child.sendline ('yes')
child.expect ('password: ')
i = child.expect([pexpect.TIMEOUT, 'password: '])
if i == 0: # Timeout
print 'ERROR!'
print 'SSH could not login. Here is what SSH said:'
print child.before, child.after
return None
child.sendline(password)
return child
verbose = 0
debug = 0
for arg in sys.argv:
if arg=="-h":
usage()
if arg=="-t":
host = sys.argv[sys.argv.index(arg)+1]
if arg=="-u":
user = sys.argv[sys.argv.index(arg)+1]
if arg=="-p":
password = sys.argv[sys.argv.index(arg)+1]
if arg=="-v":
verbose = 1
if arg=="-c":
cmd = sys.argv[sys.argv.index(arg)+1]
if arg=="--debug":
debug = 1
if not cmd:
print "No command specified."
usage()
vprint('Executing "%s" as user %s on host %s' % (cmd, user, host))
child = ssh_command (user, host, password, cmd)
child.expect(pexpect.EOF)
output = child.before
# remove an explicable leading blank line
output = '\n'.join(output.split('\n')[1:])
print output