diff options
author | Shwetha-H-Panduranga <shwetha@gluster.com> | 2011-12-07 11:56:48 +0530 |
---|---|---|
committer | Shwetha-H-Panduranga <shwetha@gluster.com> | 2011-12-07 11:56:48 +0530 |
commit | 4399cf4d161526300244d8cf090a461c792ab82e (patch) | |
tree | 7ca5ad70b0429148c63ab77667e8e54acaf5b11e /libs/connect | |
parent | 77c7f324610224e808d8940aec7e6dbf19b790a5 (diff) |
Renaming Directories
Diffstat (limited to 'libs/connect')
-rwxr-xr-x | libs/connect/ssh.py | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/libs/connect/ssh.py b/libs/connect/ssh.py new file mode 100755 index 0000000..a9952bb --- /dev/null +++ b/libs/connect/ssh.py @@ -0,0 +1,114 @@ +""" + sshConnection Class for connecting and performing operation on + remote server using SSH Protocol. +""" +import paramiko +import pdb +import time + +class SshConnection(): + + def __init__(self): + self._connection = None + self._connection = paramiko.SSHClient() + + def connect(self, host, user=None, password=None): + """ + Objective: + SSH to Server "host" as User "user" + + Parameter: + host: Server IP Address + user: Login Username + password: Login password + + Return: + Success: 0 + Failure: 1 + """ + self._connection.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + + if user == None: + user = "root" + + if password == None: + password = "syst3m" + + try: + self._connection.connect(host, username=user, password=password) + + except paramiko.BadHostKeyException as result: + print( + "BadHostKeyException: Unable to Connect to Server: '" + host + + "' as User: '" + user + "'") + return 1 + + except paramiko.AuthenticationException: + print("AuthenticationException: Unable to Authenticate " + + user + "@" + host) + return 1 + + except paramiko.SSHException: + print("SSHException: Unknown server " + host) + return 1 + + return 0 + + def close(self): + """ + Objective: + Close SSH Connections + """ + self._connection.close() + return + + def executecommand(self, command, commandInput=None): + """ + Objective: + Execute Command "comamnd" + + Parameters: + command: command to execute + + Return: + Success: 0 + Failure: 1 + """ + output = {} + output["exitstatus"] = None + output["stdoutdata"] = None + output["stderrdata"] = None + exit_status_ready_flag = True + + try: + transport = self._connection.get_transport() + channel = transport.open_session() + channel.exec_command(command) + # Adding sleep to get the correct exit_status. + time.sleep(5) + exit_status_ready_flag = channel.exit_status_ready() + if not exit_status_ready_flag: + stdin = channel.makefile("wb") + + if commandInput: + stdin.write(commandInput) + else: + print "This command requirs Command Input \ + after executing comamnd for command completion" + stdin.write("\n") + return output + + stdout = channel.makefile("rb") + stderr = channel.makefile_stderr("rb") + exit_status = channel.recv_exit_status() + + output["exitstatus"] = exit_status + output["stdoutdata"] = stdout.readlines() + output["stderrdata"] = stderr.readlines() + + except paramiko.SSHException: + print("Unable to Execute Command: " + command) + + return output + + |