Created
October 1, 2015 19:02
-
-
Save dnorton/ad9804f79dcac7804772 to your computer and use it in GitHub Desktop.
a paramiko class for use in a "with" block
This file contains hidden or 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
__author__ = 'usunoda' | |
try: | |
import paramiko, getpass | |
except ImportError: | |
pass | |
class Remote(object): | |
""" | |
Container class for SSH functionality. Needs to be used in a with statement. | |
""" | |
def __init__(self): | |
pass | |
ssh_client = None | |
user, password = None, None | |
def __enter__(self): | |
if self._has_paramiko(): | |
self.ssh_client = paramiko.SSHClient() | |
self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
return self | |
def __exit__(self, exc_type, exc_val, exc_tb): | |
try: | |
if self.ssh_client is not None: | |
self.ssh_client.close() | |
except: | |
print "received an exception closing the ssh connection." | |
finally: | |
self.ssh_client = None | |
def connect(self, server, username=None, password=None): | |
if not self._has_paramiko(): | |
return | |
if username is None and password is None \ | |
and self.user is None and self.password is None: | |
self._get_auth() | |
else: | |
self.user = username | |
self.password = password | |
try: | |
self.ssh_client.connect(server, username=self.user, password=self.password) | |
except paramiko.AuthenticationException: | |
print "you entered an incorrect username or password. Please try again" | |
self._get_auth() | |
try: | |
self.ssh_client.connect(server, username=self.user, password=self.password) | |
except: | |
print "you entered an incorrect username or password a second time. Exiting" | |
sys.exit(1) | |
def execute(self, command, sudo=False): | |
if not self._has_paramiko(): | |
return None, None, None | |
stdin, stdout, stderr = self.ssh_client.exec_command(command) | |
if sudo: | |
stdin.write(self.password + '\n') | |
stdin.flush() | |
return stdin, stdout, stderr | |
def _get_auth(self): | |
print "Enter your ssh credentials" | |
self.user = raw_input("Enter user name: ") | |
self.password = getpass.getpass("Enter your password: ") | |
def _has_paramiko(self): | |
return 'paramiko' in sys.modules.keys() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment