def __get_privatekey_file(self): """Returns user private key""" pkey_file = None rsa_key_file = local_expanduser('~%s/.ssh/id_rsa' \ % self.username) dsa_key_file = local_expanduser('~%s/.ssh/id_dsa' \ % self.username) if local_pathexists(rsa_key_file): pkey_file = rsa_key_file elif local_pathexists(dsa_key_file): pkey_file = dsa_key_file return pkey_file
def __init__(self, host, port=22, username=None, password=None, pkey=None): """ Creates a new SSH transport object which can be used in starting a session with remote server. The authentication is done based on password or private_key. Arguments: host (str) - host name or ip port (int) - (optional) defaults to 22 username (str) - (optional) defaults to command execution username password (str) - (optional) defaults to private_key pkey (str) - (optional) private key defaults to ~/.ssh/id_rsa key Actions: * Creates SSH transport object * Authenticates remotes based on username and private_key|password """ self.host = host self.port = port self.username = username or local_environ['LOGNAME'] self.password = password self.pkey = None self.transport = None self.sftp_live = False self.sftp = None # Set to info level LOG.setLevel(20) if pkey: pkey_file = local_expanduser(pkey) else: pkey_file = self.__get_privatekey_file() if not password and not pkey_file: raise RemoteDispatcherException(\ "You have not specified a password or key.") if pkey_file: self.__load_private_key(pkey_file) self.__establish_session()