Created
November 1, 2017 20:20
-
-
Save royvandam/a14eebe0afaa7571c31cdd263587f596 to your computer and use it in GitHub Desktop.
Python RSH client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
class RemoteShell: | |
def __init__(self, hostname, port=514, username='root', encoding='ascii'): | |
self.hostname = hostname | |
self.port = port | |
self.username = username | |
self.encoding = encoding | |
self.rsh = ['/usr/bin/rsh', '-p', str(port), '%s@%s' % (username, hostname) ] | |
def exec_command(self, command, get_exit_code=True): | |
if get_exit_code: | |
command += '; echo $?' | |
p = subprocess.Popen(self.rsh + [ command ], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
stdout, stderr = p.communicate() | |
stdout = stdout.decode(self.encoding).split('\n')[:-1] | |
stderr = stderr.decode(self.encoding).split('\n')[:-1] | |
if not get_exit_code or p.returncode != 0: | |
return stdout, stderr, p.returncode | |
exit_code = int(stdout.pop()) | |
return stdout, stderr, exit_code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment