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
|
#!/usr/bin/python
import argparse
import os
import paramiko
import re
import sys
status_re="^status=\d$"
parser = argparse.ArgumentParser(description='Run Azure tests')
parser.add_argument('--user', default="ubutest",
help="Username of the user")
parser.add_argument('--passwd',
help="Password of the user")
parser.add_argument('--host', required=True,
help="Hostname to test")
parser.add_argument('--key', default=None,
help="Key to use for authentication")
parser.add_argument('--file', required=True,
help="File to execute on on remote note")
parser.add_argument('--port', default=22,
help="Which SSH port to use")
opts = parser.parse_args()
if not opts.passwd and not opts.key:
raise Exception("Must define either --passwd or --key")
if opts.key and not os.path.exists(opts.key):
raise IOError("SSH Key does not exist")
if not os.path.exists(opts.file):
raise IOError("File %s does not exists" % opts.file)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(opts.host, username=opts.user, password=opts.passwd,
key_filename=opts.key, port=opts.port)
cmd = None
with open(opts.file, 'r') as f:
cmd = f.read()
if not cmd:
raise Exception("Nothing to run on remote host")
stdin, stdout, stderr = ssh.exec_command(cmd)
out = stdout.read()
err = stderr.read()
ssh.close()
print "Standard out: \n%s" % out
print "Standard err: \n%s" % err
# Extract the status cods
status_codes = []
status_codes.extend(re.findall("status=\d", out))
status_codes.extend(re.findall("status=\d", err))
if len(status_codes) > 0:
try:
last_code = int((status_codes[-1]).split('=')[-1])
print "Reported status code is %s" % last_code
sys.exit(last_code)
except Exception as e:
print "Errored on parsing"
pass
sys.exit(-1)
|