23def main(user_host, cmd, fail_errorlevel):
24 ssh_cmd = ['ssh', '-oConnectTimeout=15', '-oBatchMode=yes', user_host,
25 '(' + cmd + ') & if not errorlevel %s echo %s' % (
26 fail_errorlevel, SENTINEL)]
27
28 saw_sentinel = False
29 print(
'Original command:\n%s\nFull command:\n%s' % (
30 cmd, ' '.join([repr(s) for s in ssh_cmd])), file=sys.stderr)
31 proc = subprocess.Popen(ssh_cmd, stdout=subprocess.PIPE)
32 for line in iter(proc.stdout.readline, ''):
33 stripped = line.strip()
34 if stripped == SENTINEL:
35 saw_sentinel = True
36 else:
38 proc.wait()
39 sys.stdout.flush()
40 if proc.returncode != 0:
41 sys.exit(proc.returncode)
42 if not saw_sentinel:
43 sys.exit(1)
44 sys.exit(0)
45
46