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
|
import re
import subprocess
SSH = '/usr/bin/ssh'
class GenericSSHError(Exception): pass
class NetappCommandError(Exception): pass
class NetappMissingParam(Exception): pass
class NetappNotImplementedError(Exception): pass
def ssh(user, host, cmdargs, input=None):
cmdline = [SSH, "-o forwardagent=no", "%s@%s" % (user, host)]
cmdline.extend(cmdargs)
cmd = subprocess.Popen(cmdline,
executable=SSH,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=False)
(out, err) = cmd.communicate(input)
if cmd.wait() != 0:
raise GenericSSHError, err
else:
return out + err
def param_check(args, required):
for r in required:
if not args.has_key(r):
raise NetappMissingParam, r
def check_output(regex, output):
#strip newlines
output = output.replace('\n', ' ')
if re.search(regex, output):
return True
else:
raise NetappCommandError, output
|