def __init__(self, hostname='localhost', username=None, password=None, config_path='~/.ssh/config', port=None, verbose=False): if not hostname: raise ValueError('Missing hostname') self.sftp = None self.ssh = None ssh_config_path = path(config_path).expand() if not username: config = SSHConfig() if ssh_config_path.exists(): config.parse(ssh_config_path.open()) if config.lookup(hostname): host_config = config.lookup(hostname) username = host_config['user'] else: print 'Unknown host ', hostname else: print 'config file path wrong' self.verbose = verbose if not username: username = getpass.getuser() if self.verbose: print 'Connection info: ', username, hostname, ssh_config_path #self.ssh.set_missing_host_key_policy( # paramiko.AutoAddPolicy()) self.hostname = hostname self.username = username self.password = password self.connect()
def read_ssh_config(self, hostname): config_file = os.path.join(os.getenv('HOME'), '.ssh/config') config = SSHConfig() config.parse(open(config_file, 'r')) self.ssh_config = config.lookup(hostname) if len(self.ssh_config.keys()) < 2: print("Hostname no found in .ssh/config")
def read_openssh_config(host, config_file=None): """Parses user's OpenSSH config for per hostname configuration for hostname, user, port and private key values :param host: Hostname to lookup in config """ _ssh_config_file = config_file if config_file else \ os.path.sep.join([os.path.expanduser('~'), '.ssh', 'config']) # Load ~/.ssh/config if it exists to pick up username # and host address if set if not os.path.isfile(_ssh_config_file): return ssh_config = SSHConfig() ssh_config.parse(open(_ssh_config_file)) host_config = ssh_config.lookup(host) host = (host_config['hostname'] if 'hostname' in host_config else host) user = host_config['user'] if 'user' in host_config else None port = int(host_config['port']) if 'port' in host_config else 22 pkey = None # Try configured keys, pick first one that loads if 'identityfile' in host_config: for file_name in host_config['identityfile']: pkey = load_private_key(file_name) if pkey: break return host, user, port, pkey
def __init__(self, hostname): """ Initialise and connect to SSH. """ super(GerritSSHClient, self).__init__() self.load_system_host_keys() self.remote_version = None configfile = expanduser("~/.ssh/config") if not isfile(configfile): raise GerritError("ssh config file '%s' does not exist" % configfile) config = SSHConfig() config.parse(open(configfile)) data = config.lookup(hostname) if not data: raise GerritError("No ssh config for host %s" % hostname) if not 'hostname' in data or not 'port' in data or not 'user' in data: raise GerritError("Missing configuration data in %s" % configfile) key_filename = None if 'identityfile' in data: key_filename = abspath(expanduser(data['identityfile'])) if not isfile(key_filename): raise GerritError("Identity file '%s' does not exist" % key_filename) try: port = int(data['port']) except ValueError: raise GerritError("Invalid port: %s" % data['port']) try: self.connect(hostname=data['hostname'], port=port, username=data['user'], key_filename=key_filename) except socket.error as e: raise GerritError("Failed to connect to server: %s" % e)
def readsshconfig(self): config = os.path.expanduser('~/.ssh/config') if not os.path.exists(config): return f = open(config,'r') sshconfig = SSHConfig() sshconfig.parse(f) f.close() host = self.host try: host,port = host.split(':') except: port = None opt = sshconfig.lookup(host) if port is None: port = opt.get('port') host = opt.get('hostname', host) if port: host = "%s:%s" % (host,port) self.host=host if not self.identityfile: self.identityfile = opt.get('identityfile', None) if self.identityfile: self.identityfile = os.path.expanduser(self.identityfile).strip() if not self.user: self.user=opt.get('user','root')
def start_procs(serial, hosts, starter_func, wait=0.0): config = SSHConfig() config.parse(open(os.path.expanduser('~/.ssh/config'))) try: wait = float(wait) except ValueError: pass processes = [] for host in hosts: process = starter_func(host, config.lookup(host)) process.start() if serial or wait > 0.0: process.join() if isinstance(wait, Number): sleep(wait) processes.append(process) while multiprocessing.active_children(): try: sleep(0.3) except KeyboardInterrupt: for p in processes: p.stop() break return processes
def main(): USAGE = "usage: %prog [options] host1:path1 host2:path2" parser = OptionParser(usage=USAGE) parser.add_option("-F", "--config-file", action="store", dest="config_file", default="%s/.ssh/config" % os.environ['HOME'], help="SSH config file (default: ~/.ssh/config)",) parser.add_option("--scp-options", action="store", dest="scp_options", default="", help="string of options (in quotes) passed directy to the scp command",) (options, args) = parser.parse_args() host1, path1 = args[0].split(':', 1) host2, path2 = args[1].split(':', 1) # ssh config file config = SSHConfig() config.parse(open(options.config_file)) o = config.lookup(host2) # copy keyfile keyfile_remote = '/tmp/%s' % os.path.basename(o['identityfile']) run('scp %s %s:%s' % (o['identityfile'], host1, keyfile_remote)) # copy actual file ssh_options = ' -o'.join(['='.join([k, v]) for k, v in o.iteritems() if k != 'hostname' and k != 'identityfile']) if ssh_options: ssh_options = '-o' + ssh_options run('ssh %s scp %s -i %s -oStrictHostKeyChecking=no %s %s %s:%s' % ( host1, options.scp_options, keyfile_remote, ssh_options, path1, o['hostname'], path2))
def _configure(self): """ Configure the ssh parameters from the config file. """ configfile = expanduser("~/.ssh/config") if not isfile(configfile): raise GerritError("ssh config file '%s' does not exist" % configfile) config = SSHConfig() config.parse(open(configfile)) data = config.lookup(self.hostname) if not data: raise GerritError("No ssh config for host %s" % self.hostname) if 'hostname' not in data or 'port' not in data or 'user' not in data: raise GerritError("Missing configuration data in %s" % configfile) self.hostname = data['hostname'] self.username = data['user'] if 'identityfile' in data: key_filename = abspath(expanduser(data['identityfile'][0])) if not isfile(key_filename): raise GerritError("Identity file '%s' does not exist" % key_filename) self.key_filename = key_filename try: self.port = int(data['port']) except ValueError: raise GerritError("Invalid port: %s" % data['port']) if 'proxycommand' in data: self.proxy = ProxyCommand(data['proxycommand'])
def lookup(configfile): config = SSHConfig() config.parse(open(configfile)) res = config.lookup(login) if res.get('user'): res['username'] = res['user'] del res['user'] return res
def _configure(self): """ Configure the ssh parameters from the config file. The Port and username are extracted from .ssh/config, unless overridden by arguments to the constructor. If username and or port are provided to `__init__`, they will override the values found in the configuration file. :raise: SSHException under the following conditions: * No configuration file is found * It does not contain an entry for the Host. * It references a keyfile which does not exist * The port number is non-numeric or negative * Values for port and username can not be determined """ configfile = expanduser("~/.ssh/config") if not isfile(configfile): raise SSHException("ssh config file '%s' does not exist" % configfile) config = SSHConfig() config.parse(open(configfile)) data = config.lookup(self.hostname) if not data: raise SSHException("No ssh config for host %s" % self.hostname) self.hostname = data.get('hostname', None) self.proxy_cmd = data.get('proxycommand', None) if not self.username: self.username = data.get('user', None) if self.key_filename: self.key_filename = abspath(expanduser(self.key_filename)) elif 'identityfile' in data: self.key_filename = abspath(expanduser(data['identityfile'][0])) if self.key_filename and not isfile(self.key_filename): raise SSHException("Identity file '%s' does not exist" % self.key_filename) if self.port is None: try: self.port = int(data.get('port', '29418')) except ValueError: raise SSHException("Invalid port: %s" % data['port']) config_data = (self.hostname, self.port, self.username) if not all(config_data): raise SSHException("Missing configuration data in %s" % configfile)
def scp(self, filename, remote_path): config = SSHConfig() config.parse(open(os.path.expanduser('~/.ssh/config'))) o = config.lookup('geodata') ssh_client = SSHClient() ssh_client.load_system_host_keys() ssh_client.connect(o['hostname'], username=o['user']) scp = SCPClient(ssh_client.get_transport()) scp.put(filename, remote_path=remote_path)
def __init__(self, host): # store the host self.host = host # read the ssh config file config = SSHConfig() fname = os.path.join(os.environ['HOME'], '.ssh', 'config') config.parse(open(fname)) self.config = config.lookup(host)
def connect(self, host): # ssh config file config = SSHConfig() config.parse(open('%s/.ssh/config' % os.environ['HOME'])) o = config.lookup(host) # ssh client self.ssh_client = ssh = SSHClient() ssh.load_system_host_keys() ssh.connect(o['hostname'], username=o['user'], key_filename=o['identityfile']) self.sftp_client = ssh.open_sftp()
def config(self): sshconfig = SSHConfig() try: sshconfig.parse(open(SSH_CONFIG)) except IOError: sys.stderr.write("Warning: SSH config file location invalid.\n") conf = sshconfig.lookup(self.server) if 'port' in conf: conf['port'] = int(conf['port']) return conf
def config(self): sshconfig = SSHConfig() try: sshconfig.parse(open(SSH_CONFIG)) except IOError: print "your app needs to have a valid " \ "ssh config file location in settings.py" sys.exit(1) conf = sshconfig.lookup(self.server) if 'port' in conf: conf['port'] = int(conf['port']) return conf
def read_ssh_config( self, hostname, cnf_file = '~/.ssh/config' ): """ Read an ssh config file and store it in this class @param cnf_file: The configuration file for ssh @type cnf_file: str @raise MissingConfigurationFile: If the file does not exist """ cnf_file = os.path.expanduser( cnf_file ) if os.path.exists( cnf_file ): with open( cnf_file ) as fh: ssh_config = SSHConfig() ssh_config.parse( fh ) self._ssh_config = ssh_config.lookup( hostname ) else: raise MissingConfigurationFile( cnf_file )
def get_a_ssh_config(box_name): """Gives back a map of all the machine's ssh configurations""" output = subprocess.check_output(["vagrant", "ssh-config", box_name]) config = SSHConfig() config.parse(StringIO(output)) host_config = config.lookup(box_name) # man 5 ssh_config: # > It is possible to have multiple identity files ... # > all these identities will be tried in sequence. for id in host_config['identityfile']: if os.path.isfile(id): host_config['identityfile'] = id return dict((v, host_config[k]) for k, v in _ssh_to_ansible)
def start_procs(serial, hosts, starter_func): config = SSHConfig() config.parse(open(os.path.expanduser('~/.ssh/config'))) processes = [] for host in hosts: process = starter_func(host, config.lookup(host)) process.start() if serial: process.join() processes.append(process) while multiprocessing.active_children(): try: sleep(0.3) except KeyboardInterrupt: for p in processes: p.stop() break return processes
def connect(self): ssh = SSHClient() config = SSHConfig() with open(os.path.expanduser("~/.ssh/config")) as _file: config.parse(_file) host_config = config.lookup(self.host) ssh.set_missing_host_key_policy(AutoAddPolicy()) ssh.load_system_host_keys() ssh.connect( self.host, username=self.user, password=self.password, key_filename=host_config["identityfile"][0], allow_agent=False, timeout=30, ) transport = ssh.get_transport() channel = transport.open_session() channel.setblocking(1) return ssh
def get_a_ssh_config(box): # call `vagrant ssh-config` with box id local.cwd.chdir(box[4]) vagrant_cmd = local["vagrant"] ssh_config_cmd = vagrant_cmd["ssh-config"] output = ssh_config_cmd(box[1]) output = output[output.find("Host "):] config = SSHConfig() config.parse(StringIO(output)) host_config = config.lookup(box[1]) host_config['python_interpreter'] = "PATH=/home/core/bin:$PATH python" # man 5 ssh_config: # > It is possible to have multiple identity files ... # > all these identities will be tried in sequence. for identity_file in host_config['identityfile']: if os.path.isfile(identity_file): host_config['identityfile'] = identity_file return {v: host_config[h] for h, v in _ssh_to_ansible}
def get_service_status(hostname, servicename): ssh_client = SSHClient() ssh_client.set_missing_host_key_policy(AutoAddPolicy()) ssh_config = SSHConfig() config_file = path.expanduser("~/.ssh/config") if path.exists(config_file): config_file_opened = open(config_file) ssh_config.parse(config_file_opened) server_config = ssh_config.lookup(hostname) ssh_client.connect(hostname=server_config['hostname'], username=server_config['user'], port=server_config['port'], key_filename=server_config['identityfile']) stdin, stdout, stderr = ssh_client.exec_command( "sudo service {} status | grep 'Active'".format(servicename)) stdout_file = stdout.read() status_line = stdout_file.decode() status = status_line.split()[1] ssh_client.close() return status
def __remote_file(self, host, remote_file, log_type): def __remote_progress(filename, size, sent): stdout.write("%s\'s : %.2f%% \r" % (filename, float(sent)/float(size)*100) ) config = SSHConfig() config.parse(open('{}/.ssh/config'.format(HOME_PATH))) ssh = SSHClient() ssh.load_system_host_keys() user_config = config.lookup(host) ssh.connect(host, username=user_config['username'], port=user_config['port']) scp = SCPClient(ssh.get_transport(), sanitize=lambda x: x, progress=__remote_progress) try: file_dest = "{}/input/{}.log".format(OUTPUT_FILTERS_PATH, log_type) scp.get(remote_file, file_dest) Status.show('File {}.log has been successfuly created'.format(log_type), True) scp.close() return file_dest except: Status.show('Something went wrong creating {}.log'.format(log_type), False)
def read_openssh_config(_host, config_file=None): """Parses user's OpenSSH config for per hostname configuration for hostname, user, port and private key values :param _host: Hostname to lookup in config""" _ssh_config_file = config_file if config_file else os.path.sep.join([os.path.expanduser("~"), ".ssh", "config"]) # Load ~/.ssh/config if it exists to pick up username # and host address if set if not os.path.isfile(_ssh_config_file): return ssh_config = SSHConfig() ssh_config.parse(open(_ssh_config_file)) host_config = ssh_config.lookup(_host) host = host_config["hostname"] if "hostname" in host_config else _host user = host_config["user"] if "user" in host_config else None port = int(host_config["port"]) if "port" in host_config else 22 pkey = None # Try configured keys, pick first one that loads if "identityfile" in host_config: for file_name in host_config["identityfile"]: pkey = load_private_key(file_name) if pkey: break return host, user, port, pkey
def getSSHConnection(hostName="dc1-c-rsp-bastion-01.responsys.net"): config = SSHConfig() user = getpass.getuser() # config.parse(open('C:/Users/' + user +'/.ssh/config')) config.parse(open('C:\\cygwin64\\home\\jaganpat\\.ssh\\config.dat')) host=config.lookup(hostName) # setup SSH client client = SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #Check for proxy settings try: print(host ['proxycommand']) # proxy = paramiko.ProxyCommand(host['proxycommand']) proxy = paramiko.ProxyCommand("ssh -o StrictHostKeyChecking=no jumphostIP nc targethostIP 22") except: proxy = None #Setup the SSH connection try: passwd = "ocna@123" + str(input("Please enter password : "******"Connection to ", hostName, " is successful") return client
def from_string(config_str): """Construct RemoteAccountSSHConfig object from a string that looks like Host the-host Hostname the-hostname Port 22 User ubuntu IdentityFile /path/to/key """ config = SSHConfig() config.parse(config_str.split("\n")) hostnames = config.get_hostnames() if '*' in hostnames: hostnames.remove('*') assert len(hostnames) == 1, "Expected hostnames to have single entry: %s" % hostnames host = hostnames.pop() config_dict = config.lookup(host) if config_dict.get("identityfile") is not None: # paramiko.SSHConfig parses this in as a list, but we only want a single string config_dict["identityfile"] = config_dict["identityfile"][0] return RemoteAccountSSHConfig(host, **config_dict)
def archive(self, files): print("scp-ing: ", files) ssh = SSHClient() ssh.load_system_host_keys() # locate hostname in config file if not found, try connecting directly. hostname = os.environ['CRONOHUB_SCP_HOST'] # TODO: This won't work on windows... ssh_config_file = os.path.expanduser("~/.ssh/config") if os.path.exists(ssh_config_file): config = SSHConfig() with open(ssh_config_file) as f: config.parse(f) settings = config.lookup(hostname) if settings['hostname'] != hostname: ssh.connect(settings['hostname'], username=settings['user'], key_filename=settings['identityfile'][0], port=settings['port']) else: ssh.connect(hostname) scp = SCPClient(ssh.get_transport()) for file in files: scp.put(file[0])
def ssh_config(hostname): config = SSHConfig() config.parse(open('{}/.ssh/config'.format(HOME_PATH))) return config.lookup(hostname)
def start_tunnel(database, use_ssh_config=False): if not database: return from paramiko import AutoAddPolicy, SSHClient, SSHConfig db = settings.DATABASES[database] if db.has_key(SSH_CLIENT_KEY): # Tunnel is already running return if not db.has_key('REMOTE_HOST'): raise ValueError('REMOTE_HOST not specified for ' + database) if not db.has_key('TUNNEL_HOST'): raise ValueError('TUNNEL_HOST not specified for ' + database) kwargs = {} hostname = db['TUNNEL_HOST'] # Setup the kwargs if db.has_key('TUNNEL_USER'): kwargs['username'] = db['TUNNEL_USER'] if db.has_key('TUNNEL_PASSWORD'): kwargs['password'] = db['TUNNEL_PASSWORD'] if db.has_key('TUNNEL_IDENTITY'): kwargs['key_filename'] = db['TUNNEL_IDENTITY'] if db.has_key('TUNNEL_PORT'): kwargs['port'] = int(db['TUNNEL_PORT']) if use_ssh_config: try: with open(os.path.expanduser('~/.ssh/config')) as f: sshConfig = SSHConfig() sshConfig.parse(f) config = sshConfig.lookup(db['TUNNEL_HOST']) hostname = config['hostname'] # Use username and port if missing if not kwargs.has_key('username') and config.has_key('user'): kwargs['username'] = config['user'] if not kwargs.has_key('port') and config.has_key('port'): kwargs['port'] = int(config['port']) # Add identityfile (a list) if config.has_key('identityfile'): if kwargs.has_key('key_filename'): if type(kwargs['key_filename']) is list: kwargs['key_filename'] += config['identityfile'] else: kwargs['key_filename'] = [ kwargs['key_filename'] ] + config['identityfile'] else: kwargs['key_filename'] = config['identityfile'] except: pass # Fix the identity files if kwargs.has_key('key_filename'): if type(kwargs['key_filename']) is list: for i in range(len(kwargs['key_filename'])): if kwargs['key_filename'][i].startswith('~'): kwargs['key_filename'][i] = os.path.expanduser( kwargs['key_filename'][i]) elif kwargs['key_filename'].startswith('~'): kwargs['key_filename'] = os.path.expanduser(kwargs['key_filename']) # Setup the client client = SSHClient() client.load_system_host_keys() client.set_missing_host_key_policy(AutoAddPolicy()) client.connect(hostname, **kwargs) # Setup the port forwarding server class __SubPortForwardingServerHandler(__PortForwardingServerHandler): chain_host = db['REMOTE_HOST'] chain_port = int(db['PORT']) ssh_transport = client.get_transport() server = __PortForwardingServer(('', int(db['PORT'])), __SubPortForwardingServerHandler) # Save a reference to the client and port forwarding server db[SSH_TUNNEL_KEY] = server db[SSH_CLIENT_KEY] = client # Start port forwarding server on another thread thread.start_new_thread(__start_tunnel, (server, ))
def ssh(host, forward_agent=False, sudoable=False, max_attempts=1, max_timeout=5, ssh_password=None): """Manages a SSH connection to the desired host. Will leverage your ssh config at ~/.ssh/config if available :param host: the server to connect to :type host: str :param forward_agent: forward the local agents :type forward_agent: bool :param sudoable: allow sudo commands :type sudoable: bool :param max_attempts: the maximum attempts to connect to the desired host :type max_attempts: int :param max_timeout: the maximum timeout in seconds to sleep between attempts :type max_timeout: int :param ssh_password: SSH password to use if needed :type ssh_password: str :returns a SSH connection to the desired host :rtype: Connection :raises MaxConnectionAttemptsError: Exceeded the maximum attempts to establish the SSH connection. """ with closing(SSHClient()) as client: client.set_missing_host_key_policy(AutoAddPolicy()) cfg = { "hostname": host, "timeout": max_timeout, } if ssh_password: cfg['password'] = ssh_password ssh_config = SSHConfig() user_config_file = os.path.expanduser("~/.ssh/config") if os.path.exists(user_config_file): with open(user_config_file) as f: ssh_config.parse(f) host_config = ssh_config.lookup(host) if "user" in host_config: cfg["username"] = host_config["user"] if "proxycommand" in host_config: cfg["sock"] = ProxyCommand(host_config["proxycommand"]) if "identityfile" in host_config: cfg['key_filename'] = host_config['identityfile'] if "port" in host_config: cfg["port"] = int(host_config["port"]) attempts = 0 while attempts < max_attempts: try: attempts += 1 client.connect(**cfg) break except socket.error as e: if attempts < max_attempts: print("SSH to host {0} failed, retrying...".format(host)) time.sleep(max_timeout) else: print("SSH Exception: {0}".format(e)) else: raise MaxConnectionAttemptsError( "Exceeded max attempts to connect to host {0} after {1} retries" .format(host, max_attempts)) yield Connection(client, forward_agent, sudoable)
from paramiko.client import SSHClient from paramiko import SSHConfig SSH_CONFIG = "<insert path to ssh config>" SSH_HOST = "example" config = SSHConfig() config_file = open(SSH_CONFIG) config.parse(config_file) dev_config = config.lookup(SSH_HOST) client = SSHClient() client.load_system_host_keys() HOST = dev_config['hostname'] client.connect(HOST, port=int(dev_config['port']), username=dev_config['user'], password=dev_config['password']) client.close()
def ssh_config(ssh_config_filename, name): config = SSHConfig() with open(ssh_config_filename) as stream: config.parse(stream) return config.lookup(name)
class IronportSSH(): def __init__(self, host=None): if host is None: raise (ValueError) self.ssh_config = SSHConfig() self.ssh_config.parse(open(SSH_CONFIG_PATH)) self.ssh_host = self.ssh_config.lookup(host) self.ssh_client = SSHClient() self.ssh_client.load_system_host_keys() self.ssh_client.connect(self.ssh_host['hostname'], username=self.ssh_host['user'], key_filename=self.ssh_host['identityfile']) def ssh_run(self, cmd=None, commit=False): if cmd is None: return (None) self.ssh_client = SSHClient() self.ssh_client.load_system_host_keys() self.ssh_client.connect(self.ssh_host['hostname'], username=self.ssh_host['user'], key_filename=self.ssh_host['identityfile']) if commit: cmd = "{} ; commit spamtrapauto".format(cmd) stdin, stdout, stderr = self.ssh_client.exec_command(cmd, get_pty=True) t_out, t_err = ([], []) for i, line in enumerate(stdout): line = line.rstrip() t_out.append(line) for i, line in enumerate(stderr): line = line.rstrip() t_err.append(line) return (t_out, t_err) def get_dictionary_contents(self, dictname=None): if dictname is None: raise (ValueError) cmd = 'dictionaryconfig print {}'.format(dictname) out, err = self.ssh_run(cmd=cmd) if len(err) > 0: pprint(err) return (out) def add_to_dictionary(self, dictname=None, what=None, verbose=False): if dictname is None: raise (ValueError) if what is None: raise (ValueError) cmd = 'dictionaryconfig edit {} new {}'.format(dictname, what) hn = self.ssh_host['hostname'] if verbose: print('[INFO:IronPort:{}] Adding {} to {}'.format( hn, what, dictname)) out, err = self.ssh_run(cmd=cmd, commit=True) if len(err) > 0: pprint(err) return (out)
with open(file_ssh) as f: ssh_config.parse(f) _local_repo = eval(cfg['_local_repo']) cfg['servers']['local']['repo_project'] = _local_repo cfg['servers']['local']['root'] = os.path.split(_local_repo)[0] cfg['servers']['local']['dir_project'] = cfg['servers']['local'][ 'repo_project'] + '/' + cfg['project'] cfg['servers']['local'][ 'path_apps'] = cfg['servers']['local']['repo_project'] + cfg['dir_apps'] cfg['servers']['local'][ 'backup_dir'] = cfg['servers']['local']['repo_project'] + '/backup' # save ssh config to cfbg dict for server in cfg['servers']: host = ssh_config.lookup(server) cfg['servers'][server]['user'] = host['user'] cfg['servers'][server]['hostname'] = host['hostname'] cfg['servers'][server]['port'] = host['port'] cfg['servers'][server][ 'source'] = server + ':' + cfg['servers'][server]['repo_project'] cfg['servers'][server][ 'target'] = server + ':' + cfg['servers'][server]['root'] def _create_conda_env(): """Create a conda env""" run('conda create -y -n %s' % cfg['conda_env']) def _setup(_server):
__author__ = 'monangi.kumar' #!/usr/bin/env python import os import sys from contextlib import closing from paramiko import SSHConfig, SSHClient # specify hostname to connect to and the remote/local paths #hostname, remote_dirname, destdir = sys.argv[1:] # load parameters to setup ssh connection config = SSHConfig() with open(os.path.expanduser('~/.ssh/config')) as config_file: config.parse(config_file) d = config.lookup('10.0.0.241') ''' # connect with closing(SSHClient()) as ssh: ssh.load_system_host_keys() #NOTE: no AutoAddPolicy() ssh.connect(d['10.0.0.241'], username=d.get('root')) with closing(ssh.open_sftp()) as sftp: # cd into remote directory sftp.chdir(remote_dirname) # cd to local destination directory os.chdir(destdir) # download all files in it to destdir directory for filename in sftp.listdir(): sftp.get(filename, filename) '''
import os from paramiko import SSHClient, SSHConfig # ssh config file config = SSHConfig() ssh_config = os.path.join(os.path.expanduser('~'), '.ssh/config') print ssh_config config.parse(file(ssh_config)) o = config.lookup('zA') # ssh client pwd1 = 'uhbnmj^&*' ssh_client = SSHClient() ssh_client.load_system_host_keys() ssh_client.connect(o['hostname'], username=o['user'], password=pwd1) # run a command print "\nRun a command" cmd = 'ps aux' stdin, stdout, stderr = ssh_client.exec_command(cmd) for i, line in enumerate(stdout): line = line.strip() print "%d: %s" % (i, line) if i >= 9: break # open a remote file print "\nOpen a remote file" sftp_client = ssh_client.open_sftp() sftp_file = sftp_client.open('/var/log/mail.log')
def ssh_paramiko_connect_to(display_desc): #plain socket attributes: dtype = display_desc["type"] host = display_desc["host"] port = display_desc.get("ssh-port", 22) #ssh and command attributes: username = display_desc.get("username") or get_username() if "proxy_host" in display_desc: display_desc.setdefault("proxy_username", get_username()) password = display_desc.get("password") target = ssh_target_string(display_desc) remote_xpra = display_desc["remote_xpra"] proxy_command = display_desc["proxy_command"] #ie: "_proxy_start" socket_dir = display_desc.get("socket_dir") display_as_args = display_desc["display_as_args"] #ie: "--start=xterm :10" socket_info = { "host": host, "port": port, } with nogssapi_context(): from paramiko import SSHConfig, ProxyCommand ssh_config = SSHConfig() user_config_file = os.path.expanduser("~/.ssh/config") sock = None host_config = None if os.path.exists(user_config_file): with open(user_config_file) as f: ssh_config.parse(f) host_config = ssh_config.lookup(host) if host_config: host = host_config.get("hostname", host) username = host_config.get("username", username) port = host_config.get("port", port) proxycommand = host_config.get("proxycommand") if proxycommand: sock = ProxyCommand(proxycommand) from xpra.child_reaper import getChildReaper cmd = getattr(sock, "cmd", []) getChildReaper().add_process(sock.process, "paramiko-ssh-client", cmd, True, True) log("found proxycommand='%s' for host '%s'", proxycommand, host) from paramiko.client import SSHClient ssh_client = SSHClient() ssh_client.load_system_host_keys() ssh_client.connect(host, port, sock=sock) transport = ssh_client.get_transport() do_ssh_paramiko_connect_to( transport, host, username, password, host_config or ssh_config.lookup("*")) chan = paramiko_run_remote_xpra(transport, proxy_command, remote_xpra, socket_dir, display_as_args) peername = (host, port) conn = SSHProxyCommandConnection(chan, peername, target, socket_info) conn.timeout = SOCKET_TIMEOUT conn.start_stderr_reader() conn.process = (sock.process, "ssh", cmd) from xpra.net import bytestreams from paramiko.ssh_exception import ProxyCommandFailure bytestreams.CLOSED_EXCEPTIONS = tuple( list(bytestreams.CLOSED_EXCEPTIONS) + [ProxyCommandFailure]) return conn from xpra.scripts.main import socket_connect from paramiko.transport import Transport from paramiko import SSHException if "proxy_host" in display_desc: proxy_host = display_desc["proxy_host"] proxy_port = display_desc.get("proxy_port", 22) proxy_username = display_desc.get("proxy_username", username) proxy_password = display_desc.get("proxy_password", password) sock = socket_connect(dtype, proxy_host, proxy_port) middle_transport = Transport(sock) middle_transport.use_compression(False) try: middle_transport.start_client() except SSHException as e: log("start_client()", exc_info=True) raise InitException("SSH negotiation failed: %s" % e) proxy_host_config = ssh_config.lookup(host) do_ssh_paramiko_connect_to( middle_transport, proxy_host, proxy_username, proxy_password, proxy_host_config or ssh_config.lookup("*")) log("Opening proxy channel") chan_to_middle = middle_transport.open_channel( "direct-tcpip", (host, port), ('localhost', 0)) transport = Transport(chan_to_middle) transport.use_compression(False) try: transport.start_client() except SSHException as e: log("start_client()", exc_info=True) raise InitException("SSH negotiation failed: %s" % e) do_ssh_paramiko_connect_to(transport, host, username, password, host_config or ssh_config.lookup("*")) chan = paramiko_run_remote_xpra(transport, proxy_command, remote_xpra, socket_dir, display_as_args) peername = (host, port) conn = SSHProxyCommandConnection(chan, peername, target, socket_info) conn.timeout = SOCKET_TIMEOUT conn.start_stderr_reader() return conn #plain TCP connection to the server, #we open it then give the socket to paramiko: sock = socket_connect(dtype, host, port) sockname = sock.getsockname() peername = sock.getpeername() log("paramiko socket_connect: sockname=%s, peername=%s", sockname, peername) transport = Transport(sock) transport.use_compression(False) try: transport.start_client() except SSHException as e: log("start_client()", exc_info=True) raise InitException("SSH negotiation failed: %s" % e) do_ssh_paramiko_connect_to(transport, host, username, password, host_config or ssh_config.lookup("*")) chan = paramiko_run_remote_xpra(transport, proxy_command, remote_xpra, socket_dir, display_as_args) conn = SSHSocketConnection(chan, sock, sockname, peername, target, socket_info) conn.timeout = SOCKET_TIMEOUT conn.start_stderr_reader() return conn
def ssh_paramiko_connect_to(display_desc): #plain socket attributes: dtype = display_desc["type"] host = display_desc["host"] port = display_desc.get("ssh-port", 22) #ssh and command attributes: username = display_desc.get("username") or get_username() if "proxy_host" in display_desc: display_desc.setdefault("proxy_username", get_username()) password = display_desc.get("password") remote_xpra = display_desc["remote_xpra"] proxy_command = display_desc["proxy_command"] #ie: "_proxy_start" socket_dir = display_desc.get("socket_dir") display = display_desc.get("display") display_as_args = display_desc["display_as_args"] #ie: "--start=xterm :10" paramiko_config = display_desc.copy() paramiko_config.update(display_desc.get("paramiko-config", {})) socket_info = { "host" : host, "port" : port, } def get_keyfiles(host_config, config_name="key"): keyfiles = (host_config or {}).get("identityfile") or get_default_keyfiles() keyfile = paramiko_config.get(config_name) if keyfile: keyfiles.insert(0, keyfile) return keyfiles with nogssapi_context(): from paramiko import SSHConfig, ProxyCommand ssh_config = SSHConfig() user_config_file = os.path.expanduser("~/.ssh/config") sock = None host_config = None if os.path.exists(user_config_file): with open(user_config_file) as f: ssh_config.parse(f) log("parsed user config '%s': %i hosts found", user_config_file, len(ssh_config.get_hostnames())) host_config = ssh_config.lookup(host) if host_config: log("got host config for '%s': %s", host, host_config) chost = host_config.get("hostname", host) cusername = host_config.get("user", username) cport = host_config.get("port", port) try: port = int(cport) except (TypeError, ValueError): raise InitExit(EXIT_SSH_FAILURE, "invalid ssh port specified: '%s'" % cport) from None proxycommand = host_config.get("proxycommand") if proxycommand: log("found proxycommand='%s' for host '%s'", proxycommand, chost) sock = ProxyCommand(proxycommand) log("ProxyCommand(%s)=%s", proxycommand, sock) from xpra.child_reaper import getChildReaper cmd = getattr(sock, "cmd", []) def proxycommand_ended(proc): log("proxycommand_ended(%s) exit code=%s", proc, proc.poll()) getChildReaper().add_process(sock.process, "paramiko-ssh-client", cmd, True, True, callback=proxycommand_ended) proxy_keys = get_keyfiles(host_config, "proxy_key") log("proxy keys=%s", proxy_keys) from paramiko.client import SSHClient ssh_client = SSHClient() ssh_client.load_system_host_keys() log("ssh proxy command connect to %s", (chost, cport, sock)) ssh_client.connect(chost, cport, sock=sock) transport = ssh_client.get_transport() do_ssh_paramiko_connect_to(transport, chost, cusername, password, host_config or ssh_config.lookup("*"), proxy_keys, paramiko_config) chan = paramiko_run_remote_xpra(transport, proxy_command, remote_xpra, socket_dir, display_as_args) peername = (chost, cport) conn = SSHProxyCommandConnection(chan, peername, peername, socket_info) conn.target = host_target_string("ssh", cusername, chost, port, display) conn.timeout = SOCKET_TIMEOUT conn.start_stderr_reader() conn.process = (sock.process, "ssh", cmd) from xpra.net import bytestreams from paramiko.ssh_exception import ProxyCommandFailure bytestreams.CLOSED_EXCEPTIONS = tuple(list(bytestreams.CLOSED_EXCEPTIONS)+[ProxyCommandFailure]) return conn keys = get_keyfiles(host_config) from xpra.scripts.main import socket_connect from paramiko.transport import Transport from paramiko import SSHException if "proxy_host" in display_desc: proxy_host = display_desc["proxy_host"] proxy_port = display_desc.get("proxy_port", 22) proxy_username = display_desc.get("proxy_username", username) proxy_password = display_desc.get("proxy_password", password) proxy_keys = get_keyfiles(host_config, "proxy_key") sock = socket_connect(dtype, proxy_host, proxy_port) middle_transport = Transport(sock) middle_transport.use_compression(False) try: middle_transport.start_client() except SSHException as e: log("start_client()", exc_info=True) raise InitExit(EXIT_SSH_FAILURE, "SSH negotiation failed: %s" % e) from None proxy_host_config = ssh_config.lookup(host) do_ssh_paramiko_connect_to(middle_transport, proxy_host, proxy_username, proxy_password, proxy_host_config or ssh_config.lookup("*"), proxy_keys, paramiko_config) log("Opening proxy channel") chan_to_middle = middle_transport.open_channel("direct-tcpip", (host, port), ('localhost', 0)) transport = Transport(chan_to_middle) transport.use_compression(False) try: transport.start_client() except SSHException as e: log("start_client()", exc_info=True) raise InitExit(EXIT_SSH_FAILURE, "SSH negotiation failed: %s" % e) do_ssh_paramiko_connect_to(transport, host, username, password, host_config or ssh_config.lookup("*"), keys, paramiko_config) chan = paramiko_run_remote_xpra(transport, proxy_command, remote_xpra, socket_dir, display_as_args) peername = (host, port) conn = SSHProxyCommandConnection(chan, peername, peername, socket_info) conn.target = "%s via %s" % ( host_target_string("ssh", username, host, port, display), host_target_string("ssh", proxy_username, proxy_host, proxy_port, None), ) conn.timeout = SOCKET_TIMEOUT conn.start_stderr_reader() return conn #plain TCP connection to the server, #we open it then give the socket to paramiko: sock = socket_connect(dtype, host, port) sockname = sock.getsockname() peername = sock.getpeername() log("paramiko socket_connect: sockname=%s, peername=%s", sockname, peername) transport = Transport(sock) transport.use_compression(False) try: transport.start_client() except SSHException as e: log("start_client()", exc_info=True) raise InitExit(EXIT_SSH_FAILURE, "SSH negotiation failed: %s" % e) from None do_ssh_paramiko_connect_to(transport, host, username, password, host_config or ssh_config.lookup("*"), keys, paramiko_config) chan = paramiko_run_remote_xpra(transport, proxy_command, remote_xpra, socket_dir, display_as_args) conn = SSHSocketConnection(chan, sock, sockname, peername, (host, port), socket_info) conn.target = host_target_string("ssh", username, host, port, display) conn.timeout = SOCKET_TIMEOUT conn.start_stderr_reader() return conn
from paramiko import SSHClient, SSHConfig import os #setup config config = SSHConfig() abs_path = ('/Users/sabina/.ssh/config') base_path = os.path.dirname(__file__) rel_path = os.path.relpath(abs_path) new_path = (os.path.join(base_path, rel_path)) config.parse(open(new_path, 'r')) o = config.lookup('we') #get into aws ssh_client = SSHClient() ssh_client.load_system_host_keys() print o.keys() ssh_client.connect(o['hostname'], username=o['user'], key_filename=o['identityfile']) cmds = ['pwd', 'ls', 'cd oakpark'] #stdin, stdout, stderr = ssh_client.exec_command(cmds[2]) #ssh_client.exec_command(cmds[2]) sftp = ssh_client.open_sftp() print sftp.getcwd() #lines = [] #for l in stdout: # lines.append(l) #print str(lines[1]) f = sftp.listdir('oakpark/') print type(f) files = []
from paramiko import SSHClient, SSHConfig import os #setup config config = SSHConfig() abs_path = ('/Users/sabina/.ssh/config') base_path = os.path.dirname(__file__) rel_path = os.path.relpath(abs_path) new_path = (os.path.join(base_path,rel_path)) config.parse(open(new_path,'r')) o = config.lookup('we') #get into aws ssh_client = SSHClient() ssh_client.load_system_host_keys() print o.keys() ssh_client.connect(o['hostname'], username=o['user'], key_filename=o['identityfile']) cmds=['pwd','ls','cd oakpark'] sftp = ssh_client.open_sftp() def get(): f = sftp.listdir('oakpark/non-duplicates/xml/') print type(f) files = [] read_files=[] for l in f: # print l fil = (sftp.open('oakpark/non-duplicates/xml/{}'.format(str(l)))) files.append(fil) read_files.append(fil.read())