コード例 #1
0
ファイル: multiapt.py プロジェクト: zeha/multiapt
class Host:
  def __init__(self, name):
    self.name = name
    self.ip = None
    self.ssh_pkey = None
    self._ssh = None

  def __str__(self):
    return self.name

  def transport(self):
    """Returns the ssh transport for this host."""
    if self._ssh == None:
      self._ssh = SSHClient()
      # import keys using paramikos method
      self._ssh.load_system_host_keys()
      # import openssh system host keys
      host_keys_file = '/etc/ssh/ssh_known_hosts'
      if os.path.exists(host_keys_file):
        self._ssh.load_system_host_keys(host_keys_file)
      # import saved host keys from/for multiapt
      host_keys_file = os.getenv('HOME','/')+'/.ssh/known_hosts_multiapt'
      if os.path.exists(host_keys_file):
        self._ssh.load_host_keys(host_keys_file)
      # now set our own filename for key save purposes
      self._ssh._host_keys_filename = host_keys_file
      # enable our own policy for host key problems
      self._ssh.set_missing_host_key_policy(SSHAskHostKeyPolicy())
      if Main.debug: print 'D: ssh.connect()'
      self._ssh.connect(self.ip, username=config.remote_username, pkey=self.ssh_pkey)
    return self._ssh
コード例 #2
0
ファイル: ssh.py プロジェクト: Fclem/isbio
	def __connect(self):
		assert self._server_pub_key and self._server_url and isinstance(self._my_host_key_policy, MissingHostKeyPolicy)
		client = SSHClient()
		client.set_missing_host_key_policy(self._my_host_key_policy)
		client.connect(self._server_url) # host alias from .ssh/config #
		self._ssh_client = client
		return True
コード例 #3
0
ファイル: run.py プロジェクト: perezjust/machine
def connect_ssh(dns_name, identity_file):
    '''
    '''
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy())
    client.connect(dns_name, username='******', key_filename=identity_file)
    
    return client
コード例 #4
0
 def verify(self):
     client = SSHClient()
     client.set_missing_host_key_policy(self)
     client.connect(
         self.host,
         username=self.username,
         password=self.password,
     )
コード例 #5
0
ファイル: cmd_sh.py プロジェクト: shizhz/tutu
def ssh_client(*args, **kwargs):
    client = SSHClient()
    try:
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(*args, **kwargs)
    except Exception, e:
        logger.warn("Could not connect to remote host.")
        logger.exception(e)
        raise e
コード例 #6
0
ファイル: run.py プロジェクト: erinix/middleware
class Context(object):
    def __init__(self, hostname, username, password, xml=False):
        self.hostname = hostname
        self.username = username
        self.password = password
        self.xml = xml
        self.ssh_client = SSHClient()
        self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
        self.ssh_client.connect(self.hostname, username=self.username, password=self.password)
コード例 #7
0
ファイル: vm.py プロジェクト: vpapaioannou/IReS-Platform
 def __create_ssh_client(self):
     sshclient = SSHClient()
     sshclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     for i in range(1, MAX_WAIT_FOR_LOOPS):
         try:
             sshclient.connect(hostname=self.hostname, port=22,
                               username=self.login_user, password=self.login_password, timeout=SLEEP_TIMEOUT)
             return sshclient
         except socket.error:    # no route to host is expected here at first
             time.sleep(SLEEP_TIMEOUT)
コード例 #8
0
ファイル: remote.py プロジェクト: shaoguangleo/dfms
def createClient(host, username=None, pkeyPath=None):
    """
    Creates an SSH client object that can be used to perform SSH-related
    operations
    """
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy())

    pkey = RSAKey.from_private_key_file(os.path.expanduser(pkeyPath)) if pkeyPath else None
    client.connect(host, username=username, pkey=pkey)
    return client
コード例 #9
0
def newSshClient(hostname, username="******", localPrivKey=None, timeout=None):
    from paramiko.client import SSHClient, AutoAddPolicy

    localPrivKey = localPrivKey or loadRsaPrivKey()

    missingHostKeyPolicy = AutoAddPolicy()
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(missingHostKeyPolicy)
    ssh.connect(hostname, username=username, pkey=localPrivKey, timeout=timeout)

    return ssh
コード例 #10
0
ファイル: helpers.py プロジェクト: codedbyjay/django-branches
def test_credentials(hostname, username, password, port):
	""" Returns True if the credentials work
	"""
	client = SSHClient()
	client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
	try:
		client.connect(hostname, username=username, password=password,
			port=port)
		return True
	except Exception as e:
		print(e)
		return False
コード例 #11
0
def test_keyfile(hostname, username, key_filename, port=22, timeout=15):
    """ Tests if a key file works
    """
    client = SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:
        client.connect(
            hostname, port=port, username=username,
            key_filename=key_filename, timeout=10)
        return True
    except Exception as e:
        print(e)
        return False
コード例 #12
0
class Shared(object):

    def __init__(self, args):
        self.args = args
        self.client = Client(
            'http://{0}{1}'.format(self.args.address, ':{0}'.format(self.args.port) if self.args.port else ''),
            '/api/v2.0/',
            username=self.args.username,
            password=self.args.password,
        )
        self.ssh_client = SSHClient()
        self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
        self.ssh_client.connect(args.address, port=args.sshport, username=args.username, password=args.password)
コード例 #13
0
ファイル: __init__.py プロジェクト: altai/focus-selenium-test
def ssh_instance_pass(step, instance_name, password):
    out = bash('ssh %s -A -f -L 11112:%s:22 root@%s -N' % (SSH_OPTS, world.instances[instance_name]['ip'], config['cloud']['master']))
    if out.successful():
        from paramiko.client import SSHClient

        client = SSHClient()
        client.load_system_host_keys()
        client.connect('localhost', username='******', password=password, port=11112)
        #stdin, stdout, stderr = client.exec_command('hostname')
        client.exec_command('hostname')
        print 'done'
        client.close()
コード例 #14
0
ファイル: aws.py プロジェクト: echo0101/mc-aws-cp
def stop_instance(instance):
  try:
    stop_script_location = instance.tags['stop_script']
  except KeyError:
    stop_script_location = '~/shutdown.sh' 
  client = SSHClient()
  client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  client.load_system_host_keys()
  client.connect(instance.ip_address, username="******")
  stdin, stdout, stderr = client.exec_command('%s "%s" "%s"' % (stop_script_location, app.config["API_KEY"], app.config["MY_URL"] + "/api/v1/stats"))
  try:
    for s in [stdin, stdout, stderr]:
      app.logger.info(s.read())
  except IOError:
    pass
  client.close()
コード例 #15
0
ファイル: __init__.py プロジェクト: tianshangjun/cloudstack
 def sshClient(self):
     if self._sshClient is None:
         self.loadSshConfig()
         self._sshClient = SSHClient()
         self._sshClient.set_missing_host_key_policy(AutoAddPolicy())
         self._sshClient.connect(self.hostname, self.sshPort, self.sshUser, key_filename=self.sshKey, timeout=10)
     return self._sshClient
コード例 #16
0
ファイル: SshOps.py プロジェクト: AleNunes/Python_Basic
 def __init__(self):
     self.client = SSHClient()
     self.client.load_system_host_keys()
     self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     config = ConfigParser.ConfigParser()
     config.read("/home/forlinux/www/4803-Python/ProjetoDexter/config.cfg")
     self.client.connect(config.get("docker","address"))
コード例 #17
0
ファイル: BaseSSHWrapper.py プロジェクト: nikolayg/MCWeb
 def initSSHClient(self):
     """
     Tries to establish connection with the host.
     @raise SSHException: if the connection could not be established. 
     """
     self.client = None
     
     waitPeriod = 10
     attempts = 10
     # Try to connect but not more than attempts times
     for iteration in range(attempts):
         # If not the first time - sleep a bit. Do not bombard the server with requests
         if iteration > 0:
             time.sleep(waitPeriod)
         
         # Try establishing ssh client
         try:
             self.client = SSHClient()
             self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
             if self.pemFile is not None:
                 self.client.connect(self.address, username=self.userName, key_filename=self.pemFile, timeout=self.timeout)
             else :
                 self.client.connect(self.address, username=self.userName, password=self.password, timeout=self.timeout)    
             break
         except (SSHException, socket.error) as e: 
             self.client = None
             log.error("Could not connect to host %s err: %s", self.readableName, str(e))
     
     # If we could not connect several times - throw an exception
     if self.client == None:
         raise SSHException('Could not connect ' + str(attempts) + ' times in a row')
コード例 #18
0
 def __init__(self):
     self.client = SSHClient()
     self.client.load_host_keys(settings.FDS_HOST_KEY)
     self.client.connect(settings.FDS_HOST, username=settings.FDS_USER, key_filename=settings.FDS_PRIVATE_KEY,
                         port=settings.FDS_PORT)
     self.sftp = self.client.open_sftp()
     log.info("Connected to FDS")
コード例 #19
0
class RemoteRunner(Runner):
    def __init__(self, *args, **kwargs):
        super(RemoteRunner, self).__init__(*args, **kwargs)
        self.context

    def start(self, command):
        self.ssh_client = SSHClient()
        self.ssh_client.load_system_host_keys()
        self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
        self.ssh_client.connect(self.context.remote_runner.hostname, username=self.context.remote_runner.username)
        self.ssh_channel = self.ssh_client.get_transport().open_session()
        if self.using_pty:
            self.ssh_channel.get_pty()
        self.ssh_channel.exec_command(command)

    def stdout_reader(self):
        return self.ssh_channel.recv

    def stderr_reader(self):
        return self.ssh_channel.recv_stderr

    def default_encoding(self):
        return locale.getpreferredencoding(True)

    def wait(self):
        return self.ssh_channel.recv_exit_status()

    def returncode(self):
        return self.ssh_channel.recv_exit_status()
コード例 #20
0
 def start(self, command):
     self.ssh_client = SSHClient()
     self.ssh_client.load_system_host_keys()
     self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
     self.ssh_client.connect(self.context.remote_runner.hostname, username=self.context.remote_runner.username)
     self.ssh_channel = self.ssh_client.get_transport().open_session()
     if self.using_pty:
         self.ssh_channel.get_pty()
     self.ssh_channel.exec_command(command)
コード例 #21
0
ファイル: server.py プロジェクト: msambol/Tyr
    def connection(self):

        try:
            connection = self.ssh_connection

            self.log.info('Determining is SSH transport is still active')
            transport = connection.get_transport()

            if not transport.is_active():
                self.log.warn('SSH transport is no longer active')
                self.log.info('Proceeding to re-establish SSH connection')
                raise Exception()

            else:
                self.log.info('SSH transport is still active')
                return connection
        except Exception:
            pass

        connection = SSHClient()
        connection.set_missing_host_key_policy(AutoAddPolicy())

        self.log.info('Attempting to establish SSH connection')

        while True:
            try:
                keys = ['~/.ssh/stage', '~/.ssh/prod']
                keys = [os.path.expanduser(key) for key in keys]

                connection.connect(self.instance.private_dns_name,
                                    username='******',
                                    key_filename=keys)
                break
            except Exception:
                self.log.warn('Unable to establish SSH connection')
                time.sleep(10)

        self.log.info('Successfully established SSH connection')

        self.ssh_connection = connection

        return connection
コード例 #22
0
ファイル: ssh.py プロジェクト: jmvrbanac/opencafe
    def _get_ssh_connection(self):
        """Returns an ssh connection to the specified host"""
        _timeout = True
        ssh = SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        _start_time = time.time()
        saved_exception = exceptions.StandardError()
        #doing this because the log file fills up with these messages
        #this way it only logs it once
        log_attempted = False
        socket_error_logged = False
        auth_error_logged = False
        ssh_error_logged = False
        while not self._is_timed_out(self.timeout, _start_time):
            try:
                if not log_attempted:
                    self._log.debug('Attempting to SSH connect to: ')
                    self._log.debug('host: %s, username: %s, password: %s' %
                                    (self.host, self.username, self.password))
                    log_attempted = True
                ssh.connect(hostname=self.host,
                            username=self.username,
                            password=self.password,
                            timeout=20,
                            key_filename=[],
                            look_for_keys=False,
                            allow_agent=False)
                _timeout = False
                break
            except socket.error as e:
                if not socket_error_logged:
                    self._log.error('Socket Error: %s' % str(e))
                    socket_error_logged = True
                saved_exception = e
                continue
            except paramiko.AuthenticationException as e:
                if not auth_error_logged:
                    self._log.error('Auth Exception: %s' % str(e))
                    auth_error_logged = True
                saved_exception = e
                time.sleep(2)
                continue
            except paramiko.SSHException as e:
                if not ssh_error_logged:
                    self._log.error('SSH Exception: %s' % str(e))
                    ssh_error_logged = True
                saved_exception = e
                time.sleep(2)
                continue
                #Wait 2 seconds otherwise
            time.sleep(2)
        if _timeout:
            self._log.error('SSHConnector timed out while trying to establish a connection')
            raise saved_exception

        #This MUST be done because the transport gets garbage collected if it
        #is not done here, which causes the connection to close on invoke_shell
        #which is needed for exec_shell_command
        ResourceManager.register(self, ssh.get_transport())
        return ssh
コード例 #23
0
ファイル: ssh.py プロジェクト: chocobn69/courir
    def connect(self, instance, ssh_user, ssh_ports, cmd, ssh_key_name):
        """
        execute a command on instance with ssh and return if cmd param is not None
        connect to ssh if cmd is None
        :param instance:
        :param ssh_user:
        :param ssh_ports:
        :param ssh_key_name:
        :param cmd: execute this command if not None
        :return:
        """

        # get instance public ip
        ssh_ip = instance.ip

        # we need to find the ssh key
        try:
            key_file = open(os.path.join(os.path.expanduser(self._key_path), ssh_key_name), 'r')
        except FileNotFoundError:
            try:
                key_file = open(os.path.join(os.path.expanduser(self._key_path), ssh_key_name + '.pem'), 'r')
            except FileNotFoundError:
                raise CourirSshException('private key %(key_name)s nor %(key_name)s.pem not found' % {
                    'key_name': ssh_key_name
                })

        client = SSHClient()
        client.set_missing_host_key_policy(AutoAddPolicy())

        logger.debug('connecting to %s with port %s and user %s', ssh_ip, ssh_ports[0], ssh_user)
        mykey = RSAKey.from_private_key(key_file)

        # we try with each ssh_port we have
        for count, ssh_port in enumerate(ssh_ports):
            try:
                logger.debug(ssh_ip)
                logger.debug(ssh_port)
                client.connect(hostname=ssh_ip, port=int(ssh_port), username=ssh_user, pkey=mykey, timeout=4)
                if cmd is None:
                    with NamedTemporaryFile(mode='w+') as tmp_key_file:
                        mykey.write_private_key(tmp_key_file, password=None)
                        tmp_key_file.flush()
                        cmd = 'ssh -i %s %s@%s -p %s' % (tmp_key_file.name, ssh_user, ssh_ip, ssh_port)
                        logger.debug(cmd)
                        os.system(cmd)
                else:
                    stdin, stdout, stderr = client.exec_command(command=cmd)
                    out_str = stdout.read()
                    out_err = stderr.read().strip(' \t\n\r')
                    print(out_str)
                    if out_err != '':
                        print(out_err)
                        sys.exit(1)

            except (ConnectionRefusedError, socket.timeout):
                # we will try another tcp port
                if count < len(ssh_ports):
                    continue
                else:
                    raise CourirSshException('connection error')
コード例 #24
0
ファイル: dash.py プロジェクト: BBGIP/dash
    def _connect(self):
        client = SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(AutoAddPolicy())
        try:

            client.connect(hostname=self.host, port=self.port, username=self.user, password=self.passwd, timeout=3)
            self.closed = False
            return client
        except AuthenticationException, e:
            return e
コード例 #25
0
ファイル: datastore.py プロジェクト: mxabierto/datal
 def __init__(self):
     # Remote path for saving all resources
     self.base_folder = settings.SFTP_DATASTORE_REMOTEBASEFOLDER
     # Local base folder for saving temporary files before upload
     self.tmp_folder = settings.SFTP_DATASTORE_LOCALTMPFOLDER
     # url for donwloading resources
     self.public_base_url = settings.SFTP_BASE_URL
     self.buckets = []
     self.connection = None
     self.ssh_client = SSHClient()
     self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
     self.ssh_client.load_system_host_keys()
     self.sftp = None
コード例 #26
0
def configure_cfme(ipaddr, ssh_username, ssh_password, region, db_password):
    cmd = "appliance_console_cli --region %s --internal --force-key -p %s" % (region, db_password)
    client = SSHClient()
    try:
        client.set_missing_host_key_policy(AutoAddPolicy()) 
        client.connect(ipaddr, username=ssh_username, password=ssh_password, allow_agent=False)
        print "Will run below command on host: %s" % (ipaddr)
        print cmd
        stdin, stdout, stderr = client.exec_command(cmd)
        status = stdout.channel.recv_exit_status()
        out = stdout.readlines()
        err = stderr.readlines()
        return status, out, err
    finally:
        client.close()
コード例 #27
0
ファイル: ssh.py プロジェクト: AleNunes/Python_Basic
def executar_comando_remoto(com):
    hosts = ["192.168.0.2"]
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    for h in hosts:
        try:
            ssh.connect(h)
            stdin,stdout,stderr = ssh.exec_command(com)
            if stderr.channel.recv_exit_status() != 0:
                print stderr.read()
            else:
                print stdout.read()
        except Exception as e:
            print "Nao conseguiu conectar ao servidor %s"%e
コード例 #28
0
class FDSConnection():
    def __init__(self):
        self.client = SSHClient()
        self.client.load_host_keys(settings.FDS_HOST_KEY)
        self.client.connect(settings.FDS_HOST, username=settings.FDS_USER, key_filename=settings.FDS_PRIVATE_KEY,
                            port=settings.FDS_PORT)
        self.sftp = self.client.open_sftp()
        log.info("Connected to FDS")

    def get_files(self):
        log.info("Receiving files from FDS...")
        fds_data_path = os.path.join(settings.BASE_DIR, settings.FDS_DATA_PATH)

        local_files = os.listdir(fds_data_path)

        self.sftp.chdir('yellow-net-reports')
        for file in self.sftp.listdir():
            if file not in local_files and file + '.processed' not in local_files:
                log.info("Receiving {}".format(file))
                self.sftp.get(file, os.path.join(fds_data_path, file))
                # self.sftp.remove(file)
            else:
                log.debug("Skipping already present file: {}".format(file))
コード例 #29
0
ファイル: sysctl.py プロジェクト: t3rmin4t0r/batchman
def timecheck(h):
	cmd = "sudo sysctl -w net.core.somaxconn=16000;"
	s = SSHClient()
	print "> %s" % (h,)
	s.set_missing_host_key_policy(AutoAddPolicy())
	s.connect(hostname=h[1], username=h[0])#, password='')
	(_in, _out, _err) = s.exec_command( cmd, bufsize=4096)
	print "< %s" % (h,)
	return (h,_out.read())
コード例 #30
0
ファイル: datastore.py プロジェクト: mxabierto/datal
class datastore_sftp(datastore):
    """ collect of independent functions, not really a Class """
    def __init__(self):
        # Remote path for saving all resources
        self.base_folder = settings.SFTP_DATASTORE_REMOTEBASEFOLDER
        # Local base folder for saving temporary files before upload
        self.tmp_folder = settings.SFTP_DATASTORE_LOCALTMPFOLDER
        # url for donwloading resources
        self.public_base_url = settings.SFTP_BASE_URL
        self.buckets = []
        self.connection = None
        self.ssh_client = SSHClient()
        self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
        self.ssh_client.load_system_host_keys()
        self.sftp = None

    def connect(self):
        """ don't use at INIT because it hangs all application"""
        logger = logging.getLogger(__name__)
        logger.error('Connecting SFTP %s:%s (%s, %s)' %(
            settings.SFTP_DATASTORE_HOSTNAME,
            settings.SFTP_DATASTORE_PORT,
            settings.SFTP_DATASTORE_USER,
            settings.SFTP_DATASTORE_PASSWORD)
        )

        # TODO: Remove
        con = sftp.Connection(
            host=settings.SFTP_DATASTORE_HOSTNAME,
            port=settings.SFTP_DATASTORE_PORT,
            username=settings.SFTP_DATASTORE_USER,
            password=settings.SFTP_DATASTORE_PASSWORD,
            log=True
        )
        self.connection = con
        #

        self.ssh_client.connect(
            settings.SFTP_DATASTORE_HOSTNAME,
            port=settings.SFTP_DATASTORE_PORT,
            username=settings.SFTP_DATASTORE_USER,
            password=settings.SFTP_DATASTORE_PASSWORD
        )
        self.sftp = self.ssh_client.open_sftp()

        # list all buckets (folders)
        try:
            self.buckets = self.sftp.listdir(path=self.base_folder)
            logger.error('Buckets: %s' %str(self.buckets))
        except Exception, e:
            logger.error('Error Connecting SFTP %s' % str(e))
            self.sftp.close()
コード例 #31
0
from paramiko.client import SSHClient

get_temp_cmd = 'bash /home/pi/Documents/gitProjects/icypi/src/mcp9808/get_temp_mcp9808.sh'

client = SSHClient()
client.load_system_host_keys()
client.connect('192.168.101.109', username='******')
stdin, stdout, stderr = client.exec_command(get_temp_cmd)

lines = stdout.readlines()
temp = float(lines[0].strip())

print(temp)
コード例 #32
0
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
コード例 #33
0
ファイル: ssh.py プロジェクト: jackerzz/spug
class SSH:
    def __init__(self,
                 hostname,
                 port=22,
                 username='******',
                 pkey=None,
                 password=None,
                 default_env=None,
                 connect_timeout=10):
        self.stdout = None
        self.client = None
        self.channel = None
        self.sftp = None
        self.eof = 'Spug EOF 2108111926'
        self.already_init = False
        self.default_env = self._make_env_command(default_env)
        self.regex = re.compile(r'Spug EOF 2108111926 (-?\d+)[\r\n]?')
        self.arguments = {
            'hostname':
            hostname,
            'port':
            port,
            'username':
            username,
            'password':
            password,
            'pkey':
            RSAKey.from_private_key(StringIO(pkey))
            if isinstance(pkey, str) else pkey,
            'timeout':
            connect_timeout,
            'banner_timeout':
            30
        }

    @staticmethod
    def generate_key():
        key_obj = StringIO()
        key = RSAKey.generate(2048)
        key.write_private_key(key_obj)
        return key_obj.getvalue(), 'ssh-rsa ' + key.get_base64()

    def get_client(self):
        if self.client is not None:
            return self.client
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(AutoAddPolicy)
        self.client.connect(**self.arguments)
        return self.client

    def ping(self):
        return True

    def add_public_key(self, public_key):
        command = f'mkdir -p -m 700 ~/.ssh && \
        echo {public_key!r} >> ~/.ssh/authorized_keys && \
        chmod 600 ~/.ssh/authorized_keys'

        exit_code, out = self.exec_command_raw(command)
        if exit_code != 0:
            raise Exception(f'add public key error: {out}')

    def exec_command_raw(self, command, environment=None):
        channel = self.client.get_transport().open_session()
        if environment:
            channel.update_environment(environment)
        channel.set_combine_stderr(True)
        channel.exec_command(command)
        code, output = channel.recv_exit_status(), channel.recv(-1)
        return code, self._decode(output)

    def exec_command(self, command, environment=None):
        channel = self._get_channel()
        command = self._handle_command(command, environment)
        channel.send(command)
        out, exit_code = '', -1
        for line in self.stdout:
            match = self.regex.search(line)
            if match:
                exit_code = int(match.group(1))
                line = line[:match.start()]
                out += line
                break
            out += line
        return exit_code, out

    def _win_exec_command_with_stream(self, command, environment=None):
        channel = self.client.get_transport().open_session()
        if environment:
            channel.update_environment(environment)
        channel.set_combine_stderr(True)
        channel.get_pty(width=102)
        channel.exec_command(command)
        stdout = channel.makefile("rb", -1)
        out = stdout.readline()
        while out:
            yield channel.exit_status, self._decode(out)
            out = stdout.readline()
        yield channel.recv_exit_status(), self._decode(out)

    def exec_command_with_stream(self, command, environment=None):
        channel = self._get_channel()
        command = self._handle_command(command, environment)
        channel.send(command)
        exit_code, line = -1, ''
        while True:
            line = self._decode(channel.recv(8196))
            if not line:
                break
            match = self.regex.search(line)
            if match:
                exit_code = int(match.group(1))
                line = line[:match.start()]
                break
            yield exit_code, line
        yield exit_code, line

    def put_file(self, local_path, remote_path):
        sftp = self._get_sftp()
        sftp.put(local_path, remote_path)

    def put_file_by_fl(self, fl, remote_path, callback=None):
        sftp = self._get_sftp()
        sftp.putfo(fl, remote_path, callback=callback)

    def list_dir_attr(self, path):
        sftp = self._get_sftp()
        return sftp.listdir_attr(path)

    def sftp_stat(self, path):
        sftp = self._get_sftp()
        return sftp.stat(path)

    def remove_file(self, path):
        sftp = self._get_sftp()
        sftp.remove(path)

    def _get_channel(self):
        if self.channel:
            return self.channel

        counter = 0
        self.channel = self.client.invoke_shell()
        command = 'set +o zle\nset -o no_nomatch\nexport PS1= && stty -echo\n'
        if self.default_env:
            command += f'{self.default_env}\n'
        command += f'echo {self.eof} $?\n'
        self.channel.send(command.encode())
        while True:
            if self.channel.recv_ready():
                line = self._decode(self.channel.recv(8196))
                if self.regex.search(line):
                    self.stdout = self.channel.makefile('r')
                    break
            elif counter >= 100:
                self.client.close()
                raise Exception('Wait spug response timeout')
            else:
                counter += 1
                time.sleep(0.1)
        return self.channel

    def _get_sftp(self):
        if self.sftp:
            return self.sftp

        self.sftp = self.client.open_sftp()
        return self.sftp

    def _make_env_command(self, environment):
        if not environment:
            return None
        str_envs = []
        for k, v in environment.items():
            k = k.replace('-', '_')
            if isinstance(v, str):
                v = v.replace("'", "'\"'\"'")
            str_envs.append(f"{k}='{v}'")
        str_envs = ' '.join(str_envs)
        return f'export {str_envs}'

    def _handle_command(self, command, environment):
        new_command = commands = ''
        if not self.already_init:
            commands = 'export SPUG_EXEC_FILE=$(mktemp)\n'
            commands += 'trap \'rm -f $SPUG_EXEC_FILE\' EXIT\n'
            self.already_init = True

        env_command = self._make_env_command(environment)
        if env_command:
            new_command += f'{env_command}\n'
        new_command += command
        new_command += f'\necho {self.eof} $?\n'
        b64_command = base64.standard_b64encode(new_command.encode())
        commands += f'echo {b64_command.decode()} | base64 -di > $SPUG_EXEC_FILE\n'
        commands += 'source $SPUG_EXEC_FILE\n'
        return commands

    def _decode(self, content):
        try:
            content = content.decode()
        except UnicodeDecodeError:
            content = content.decode(encoding='GBK', errors='ignore')
        return content

    def __enter__(self):
        self.get_client()
        transport = self.client.get_transport()
        if 'windows' in transport.remote_version.lower():
            self.exec_command = self.exec_command_raw
            self.exec_command_with_stream = self._win_exec_command_with_stream
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.client.close()
        self.client = None
コード例 #34
0
from paramiko.client import SSHClient

# Credentials here are for a always-on Sandbox from Cisco DevNet
SSH_USER = "******"
SSH_PASSWORD = "******"
SSH_HOST = "<Insert the IP/host of your device/server here>"
SSH_PORT = 22  # Change this if your SSH port is different

client = SSHClient()
client.load_system_host_keys()

try:
    client.connect(SSH_HOST,
                   port=SSH_PORT,
                   username=SSH_USER,
                   password=SSH_PASSWORD,
                   look_for_keys=False)
    print("Connected succesfully!")
except Exception:
    print("Failed to establish connection.")
finally:
    client.close()
コード例 #35
0
ファイル: sshsource.py プロジェクト: nsetzer/YueMusicPlayer
    def connect_v2(host,port,username,password=None,private_key=None,config_path=None):

        """
        Note:
            it may be possible to escalate root priveleges
            stdin,stdout,stderr = ssh.exec_command("sudo su; whoami", get_pty=True)
            various strategies exist for entering the password.
        """
        # build a configuration from the options
        cfg = {"hostname":host,
               "port":port,
               "username":username,
               "timeout":10.0,"compress":True,
               "allow_agent":False,
               "look_for_keys":False}
        if password is not None:
            cfg["password"] = password

        user_config = getSSHConfig(config_path,host)

        # copy the settings from the userconfig, overwriting the cfg.
        for k in ('hostname', 'username', 'port'):
            if k in user_config:
                cfg[k] = user_config[k]

        # proxy command allows for two factor authentication
        if 'proxycommand' in user_config:
            cfg['sock'] = paramiko.ProxyCommand(user_config['proxycommand'])
            cfg['timeout'] = 45.0; # give extra time for two factor

        print(cfg)

        cfg['pkey'] = None
        if private_key: # non-null, non-empty
            passphrase = "" # TODO: support passphrases
            cfg['pkey']=paramiko.RSAKey.from_private_key_file(\
                        private_key,passphrase)

        client = SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        #client.set_missing_host_key_policy(paramiko.WarningPolicy())

        try:
            client.connect(**cfg)
        except BadHostKeyException as e:
            sys.stderr.write("got:      %s\n"%e.key.asbytes());
            sys.stderr.write("expected: %s\n"%e.expected_key.asbytes());

            msg = "Connection error on %s:%s using privatekey=%s\n"%(\
                host,port,private_key)
            msg += "you may need to clear ~/.ssh/known_hosts "+\
                "for entries related to %s:%s\n"%(host,port);
            sys.stderr.write(msg);
            raise Exception(msg);

        src = SSHClientSource()
        src.client = client
        src.ftp = client.open_sftp()

        src.host=host
        src.port=port
        src.config=cfg

        return src
コード例 #36
0
ファイル: turnOnOff.py プロジェクト: lnaundorf/turnOnOff
def turn_off(id):
    server = SERVERS.get(id)

    if not server:
        return server_not_found()

    exit_status = -1

    ssh_settings = server['ssh']
    client = SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(AutoAddPolicy())
    client.connect(ssh_settings['address'],
                   username=ssh_settings['username'],
                   password=ssh_settings['password'])
    stdin, stdout, stderr = client.exec_command('shutdown -p now')

    #print("stdout: " + str(stdout.readlines()))
    #print("stderr: " + str(stderr.readlines()))

    exit_status = stdout.channel.recv_exit_status()
    print("Shutdown, exit status: %s" % exit_status)

    client.close()

    return jsonify({"success": exit_status == 0})
コード例 #37
0
import json
from paramiko.client import SSHClient

credentials = {}
with open("credentials.json") as fh:
     json.load(fh)

CMD = “show running-config”
for cred in credentials:
	out_file_name = str(cred['name']) + ".txt"
	client = SSHClient()
	client.load_system_host_keys()
	client.connect(SSH_HOST, port=cred['port'],
                              username=cred['username'],
                              password=cred['password'])
	stdin, stdout, stderr = client.exec_command(CMD)
	
	out_file = open(out_file_name, "w")
	output = stdout.readlines()
	for line in output:
		out_file.write(line)
	out_file.close()
	client.close()
	print("Executed command on " + cred['name'])
コード例 #38
0
ファイル: Mylib.py プロジェクト: bjjdyx345/PowerBankTest
 def __init__(self):
     self.ssh_client = SSHClient()
コード例 #39
0
    print("A kinto.fcgi already exist.")
else:
    print("A kinto.fcgi has been uploaded.")
try:
    with open("htaccess", "rb") as f:
        ftp.storbinary("STOR www/.htaccess", f)
except ftplib.error_perm:
    print("A .htaccess already exist.")
else:
    print("A .htaccess has been uploaded.")
ftp.close()


# Install kinto[postgresql]

ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(settings['ssh_host'], username=settings['prefixed_username'],
            password=PASSWORD, look_for_keys=False)
stdin, stdout, stderr = ssh.exec_command('PYTHONPATH=~/.local/ easy_install-2.6 --install-dir=~/.local -U pip')
print(stdout.read(), stderr.read())
stdin, stdout, stderr = ssh.exec_command('PYTHONPATH=~/.local/ ~/.local/pip install --user --no-binary --upgrade setuptools virtualenv virtualenvwrapper')
print(stdout.read(), stderr.read())
stdin, stdout, stderr = ssh.exec_command('~/.local/bin/virtualenv kinto/venv/ --python=python2.7')
print(stdout.read(), stderr.read())
stdin, stdout, stderr = ssh.exec_command('kinto/venv/bin/pip install kinto[postgresql] kinto-attachment flup')
print(stdout.read(), stderr.read())

# Run kinto migration to setup the database.
stdin, stdout, stderr = ssh.exec_command('kinto/venv/bin/kinto --ini kinto/kinto.ini migrate')
print(stdout.read(), stderr.read())
コード例 #40
0
    def __init__(
        self,
        host,
        user=None,
        port=None,
        config=None,
        gateway=None,
        forward_agent=None,
        connect_timeout=None,
        connect_kwargs=None,
    ):
        """
        Set up a new object representing a server connection.

        :param str host:
            the hostname (or IP address) of this connection.

            May include shorthand for the ``user`` and/or ``port`` parameters,
            of the form ``user@host``, ``host:port``, or ``user@host:port``.

            .. note::
                Due to ambiguity, IPv6 host addresses are incompatible with the
                ``host:port`` shorthand (though ``user@host`` will still work
                OK). In other words, the presence of >1 ``:`` character will
                prevent any attempt to derive a shorthand port number; use the
                explicit ``port`` parameter instead.

            .. note::
                If ``host`` matches a ``Host`` clause in loaded SSH config
                data, and that ``Host`` clause contains a ``Hostname``
                directive, the resulting `.Connection` object will behave as if
                ``host`` is equal to that ``Hostname`` value.

                In all cases, the original value of ``host`` is preserved as
                the ``original_host`` attribute.

                Thus, given SSH config like so::

                    Host myalias
                        Hostname realhostname

                a call like ``Connection(host='myalias')`` will result in an
                object whose ``host`` attribute is ``realhostname``, and whose
                ``original_host`` attribute is ``myalias``.

        :param str user:
            the login user for the remote connection. Defaults to
            ``config.user``.

        :param int port:
            the remote port. Defaults to ``config.port``.

        :param config:
            configuration settings to use when executing methods on this
            `.Connection` (e.g. default SSH port and so forth).

            Should be a `.Config` or an `invoke.config.Config`
            (which will be turned into a `.Config`).

            Default is an anonymous `.Config` object.

        :param gateway:
            An object to use as a proxy or gateway for this connection.

            This parameter accepts one of the following:

            - another `.Connection` (for a ``ProxyJump`` style gateway);
            - a shell command string (for a ``ProxyCommand`` style style
              gateway).

            Default: ``None``, meaning no gatewaying will occur (unless
            otherwise configured; if one wants to override a configured gateway
            at runtime, specify ``gateway=False``.)

            .. seealso:: :ref:`ssh-gateways`

        :param bool forward_agent:
            Whether to enable SSH agent forwarding.

            Default: ``config.forward_agent``.

        :param int connect_timeout:
            Connection timeout, in seconds.

            Default: ``config.timeouts.connect``.

        :param dict connect_kwargs:
            Keyword arguments handed verbatim to
            `SSHClient.connect <paramiko.client.SSHClient.connect>` (when
            `.open` is called).

            `.Connection` tries not to grow additional settings/kwargs of its
            own unless it is adding value of some kind; thus,
            ``connect_kwargs`` is currently the right place to hand in
            parameters such as ``pkey`` or ``key_filename``.

            Default: ``config.connect_kwargs``.

        :raises ValueError:
            if user or port values are given via both ``host`` shorthand *and*
            their own arguments. (We `refuse the temptation to guess`_).

        .. _refuse the temptation to guess:
            http://zen-of-python.info/
            in-the-face-of-ambiguity-refuse-the-temptation-to-guess.html#12
        """
        # NOTE: parent __init__ sets self._config; for now we simply overwrite
        # that below. If it's somehow problematic we would want to break parent
        # __init__ up in a manner that is more cleanly overrideable.
        super(Connection, self).__init__(config=config)

        #: The .Config object referenced when handling default values (for e.g.
        #: user or port, when not explicitly given) or deciding how to behave.
        if config is None:
            config = Config()
        # Handle 'vanilla' Invoke config objects, which need cloning 'into' one
        # of our own Configs (which grants the new defaults, etc, while not
        # squashing them if the Invoke-level config already accounted for them)
        elif not isinstance(config, Config):
            config = config.clone(into=Config)
        self._set(_config=config)
        # TODO: when/how to run load_files, merge, load_shell_env, etc?
        # TODO: i.e. what is the lib use case here (and honestly in invoke too)

        shorthand = self.derive_shorthand(host)
        host = shorthand["host"]
        err = (
            "You supplied the {} via both shorthand and kwarg! Please pick one."  # noqa
        )
        if shorthand["user"] is not None:
            if user is not None:
                raise ValueError(err.format("user"))
            user = shorthand["user"]
        if shorthand["port"] is not None:
            if port is not None:
                raise ValueError(err.format("port"))
            port = shorthand["port"]

        # NOTE: we load SSH config data as early as possible as it has
        # potential to affect nearly every other attribute.
        #: The per-host SSH config data, if any. (See :ref:`ssh-config`.)
        self.ssh_config = self.config.base_ssh_config.lookup(host)

        self.original_host = host
        #: The hostname of the target server.
        self.host = host
        if "hostname" in self.ssh_config:
            # TODO: log that this occurred?
            self.host = self.ssh_config["hostname"]

        #: The username this connection will use to connect to the remote end.
        self.user = user or self.ssh_config.get("user", self.config.user)
        # TODO: is it _ever_ possible to give an empty user value (e.g.
        # user='')? E.g. do some SSH server specs allow for that?

        #: The network port to connect on.
        self.port = port or int(self.ssh_config.get("port", self.config.port))

        # Non-None values - string, Connection, even eg False - get set
        # directly; None triggers seek in config/ssh_config
        if gateway is None:
            # SSH config wins over Invoke-style config
            if "proxyjump" in self.ssh_config:
                # Reverse hop1,hop2,hop3 style ProxyJump directive so we start
                # with the final (itself non-gatewayed) hop and work up to
                # the front (actual, supplied as our own gateway) hop
                hops = reversed(self.ssh_config["proxyjump"].split(","))
                prev_gw = None
                for hop in hops:
                    # Happily, ProxyJump uses identical format to our host
                    # shorthand...
                    if prev_gw is None:
                        # TODO: this isn't persisting config! which among other
                        # things can lead to not honoring skipping ssh config
                        # file loads...
                        cxn = Connection(hop)
                    else:
                        cxn = Connection(hop, gateway=prev_gw)
                    prev_gw = cxn
                gateway = prev_gw
            elif "proxycommand" in self.ssh_config:
                # Just a string, which we interpret as a proxy command..
                gateway = self.ssh_config["proxycommand"]
            else:
                # Neither of those? Our config value please.
                gateway = self.config.gateway
        #: The gateway `.Connection` or ``ProxyCommand`` string to be used,
        #: if any.
        self.gateway = gateway
        # NOTE: we use string above, vs ProxyCommand obj, to avoid spinning up
        # the ProxyCommand subprocess at init time, vs open() time.
        # TODO: make paramiko.proxy.ProxyCommand lazy instead?

        if forward_agent is None:
            # Default to config...
            forward_agent = self.config.forward_agent
            # But if ssh_config is present, it wins
            if "forwardagent" in self.ssh_config:
                # TODO: SSHConfig really, seriously needs some love here, god
                map_ = {"yes": True, "no": False}
                forward_agent = map_[self.ssh_config["forwardagent"]]
        #: Whether agent forwarding is enabled.
        self.forward_agent = forward_agent

        if connect_timeout is None:
            connect_timeout = self.ssh_config.get("connecttimeout",
                                                  self.config.timeouts.connect)
        if connect_timeout is not None:
            connect_timeout = int(connect_timeout)
        #: Connection timeout
        self.connect_timeout = connect_timeout

        #: Keyword arguments given to `paramiko.client.SSHClient.connect` when
        #: `open` is called.
        self.connect_kwargs = self.resolve_connect_kwargs(connect_kwargs)

        #: The `paramiko.client.SSHClient` instance this connection wraps.
        client = SSHClient()
        client.set_missing_host_key_policy(AutoAddPolicy())
        self.client = client

        #: A convenience handle onto the return value of
        #: ``self.client.get_transport()``.
        self.transport = None
コード例 #41
0
 def __init__(self):
     self.client = SSHClient()
     self.client.load_system_host_keys()
     self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     self.client.connect("192.168.0.2", username='******', password='******')
コード例 #42
0
def install_kinto_remotely(id_alwaysdata, credentials, ssh_host,
                           prefixed_username, status_handler):
    logs = StringIO()
    # SSH Login
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(ssh_host,
                username=prefixed_username,
                password=credentials[1],
                look_for_keys=False)

    # Install pip
    stdin, stdout, stderr = ssh.exec_command(
        'PYTHONPATH=~/.local/ easy_install-2.6 --install-dir=~/.local -U pip')
    logs.write(stdout.read())
    logs.write(stderr.read())
    status_handler.ssh_logs = logs

    # Install virtualenv
    stdin, stdout, stderr = ssh.exec_command(
        'PYTHONPATH=~/.local/ ~/.local/pip install --user --no-binary --upgrade '
        'setuptools virtualenv virtualenvwrapper')
    logs.write(stdout.read())
    logs.write(stderr.read())
    status_handler.ssh_logs = logs

    # Create virtualenv
    stdin, stdout, stderr = ssh.exec_command(
        '~/.local/bin/virtualenv kinto/venv/ --python=python2.7')
    logs.write(stdout.read())
    logs.write(stderr.read())
    status_handler.ssh_logs = logs

    # Install Kinto in the virtualenv
    stdin, stdout, stderr = ssh.exec_command(
        'kinto/venv/bin/pip install kinto[postgresql] kinto-attachment flup')
    logs.write(stdout.read())
    logs.write(stderr.read())
    status_handler.ssh_logs = logs

    # Run kinto migration to setup the database.
    stdin, stdout, stderr = ssh.exec_command(
        'kinto/venv/bin/kinto --ini kinto/kinto.ini migrate')
    logs.write(stdout.read())
    logs.write(stderr.read())
    status_handler.ssh_logs = logs
    ssh.close()
コード例 #43
0
def set_server(ip, port, n_conn, Tunnel, hostname=None, username=None):
    """
    this method is responsible for establishing a server(listnening for connection) 
    In a direct LAN enviroment if Tunnel was False; hostname isn't needed
    n_conn is the awaited number of connections to be established
    Hostname is needed it Tunnel was activated  
    """
    connection = []
    transport = None
    if Tunnel:

        sshclient = SSHClient()

        sshclient.load_system_host_keys()

        sshclient.set_missing_host_key_policy(AutoAddPolicy())

        try:
            sshclient.connect(hostname=hostname, username=username)
        except (BadHostKeyException, SSHException,
                AuthenticationException) as e:
            print("problem hapened ssh into {}".format(hostname))
            raise e
            sshclient.get_transport().close()
            return None, None
        try:
            try:
                transport = sshclient.get_transport()
                port = transport.request_port_forward(address=ip, port=port)
            except (SSHException):
                print("the Node refused the Given port {}".format(port))
                port = 0
                port = transport.request_port_forward(address=ip, port=port)
            print("port {}".format(port))
            print('starting up on {} : {}'.format(ip, port))
            for i in range(n_conn):
                connection.append(transport.accept(None))
                print('client_address is {}:{} '.format(
                    *connection[i].getpeername()))
        except (BadHostKeyException, SSHException,
                AuthenticationException) as e:
            print(
                "an unexpected problem has been faced in request_port_forward")
            for i in connection:
                i.close()
            transport.close()
            return None, None

        #port,s=tunneling_cmd_hpc_server(user=user,path=path,local_port=port)
    else:
        #s = None
        sock = socket.socket(
            socket.AF_INET, socket.SOCK_STREAM
        )  #specifying socket type is IPV4"socket.AF_INET" and TCP "SOCK_STREAM"
        server_address = (ip, port)  # saving ip and port as tuple
        try:
            sock.bind(server_address)  # attaching the socket to the pc (code)
            print('starting up on', server_address[0], ':', server_address[1])
            sock.listen(n_conn)  # start waiting for 1 client to connection
            for i in range(n_conn):
                connection_, client_address = sock.accept(
                )  #accepting that client and hosting a connection with him
                print('client_address is {}:{} '.format(*client_address))
                connection.append(connection_)
        except (OSError) as e:
            print("Making a server Failed")
            raise e
            for i in connection:
                i.close()
            return None, None

    print('The number of connections started successfully = {}'.format(
        n_conn))  # printing when the connection is successful
    return connection, transport  #returning the connection object for further use
コード例 #44
0
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)
コード例 #45
0
def connect_via_ssh(host, ssh_user, ssh_key_path):
    client = SSHClient()
    client.load_system_host_keys()
    client.connect(host, username=ssh_user, key_filename=ssh_key_path, allow_agent=False, look_for_keys=False, disabled_algorithms=dict(pubkeys=['rsa-sha2-512', 'rsa-sha2-256']))

    return client
コード例 #46
0
ファイル: sync.py プロジェクト: jstelzer/personal-tools
 files = []
 t0 = datetime.datetime.now()
 while True:
     while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
         line = sys.stdin.readline()
         files.append(line)
     size = len(files)
     if size:
         now = datetime.datetime.now()
         delta = now - t0
         if 5.0 < delta.total_seconds():
             runner = DirSync(args.remote_host, files)
             runner.main()
             if args.post_sync:
                 print "Running ({cmd})".format(cmd=args.post_sync)
                 with closing(SSHClient()) as client:
                     client.load_system_host_keys()
                     client.set_missing_host_key_policy(AutoAddPolicy())
                     print "Attempting to connect"
                     client.connect(args.remote_host,
                                    allow_agent=True,
                                    username=os.getenv('USER'))
                     print "Connected"
                     with closing(
                             client.get_transport().open_session()) as chan:
                         chan.set_combine_stderr(True)
                         chan.exec_command(args.post_sync)
                         dump_channel(chan)
                         rv = chan.recv_exit_status()
                         print "Remote command exited with {rv}".format(
                             rv=rv)
コード例 #47
0
class SSH:
    def __init__(self,
                 hostname,
                 port=SSH_PORT,
                 username='******',
                 pkey=None,
                 password=None,
                 connect_timeout=10):
        if pkey is None and password is None:
            raise Exception(
                'public key and password must have one is not None')
        self.client = None
        self.arguments = {
            'hostname':
            hostname,
            'port':
            port,
            'username':
            username,
            'password':
            password,
            'pkey':
            RSAKey.from_private_key(StringIO(pkey))
            if isinstance(pkey, str) else pkey,
            'timeout':
            connect_timeout,
        }

    @staticmethod
    def generate_key():
        key_obj = StringIO()
        key = RSAKey.generate(2048)
        key.write_private_key(key_obj)
        return key_obj.getvalue(), 'ssh-rsa ' + key.get_base64()

    def add_public_key(self, public_key):
        command = f'mkdir -p -m 700 ~/.ssh && \
        echo {public_key!r} >> ~/.ssh/authorized_keys && \
        chmod 600 ~/.ssh/authorized_keys'

        code, out = self.exec_command(command)
        if code != 0:
            raise Exception(out)

    def ping(self):
        with self:
            return True

    def get_client(self):
        if self.client is not None:
            return self.client
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(AutoAddPolicy)
        self.client.connect(**self.arguments)
        return self.client

    def put_file(self, local_path, remote_path):
        with self as cli:
            sftp = cli.open_sftp()
            sftp.put(local_path, remote_path)
            sftp.close()
            sftp.open()

    def put_content_to_remote(self, remote_path, content, mkdir=False):
        """
        将文本传输到远程服务器
        :param remote_path 远程路径
        :param content 文件内容
        :param mkdir 如果目录不存在, 是否创建相关目录
        """
        if mkdir:
            import os
            bash_dir = os.path.split(remote_path)
            if len(bash_dir) > 1 and bash_dir[0] != '':
                self.exec_command(f'mkdir -p {bash_dir[0]}')
        with self as cli:
            sftp = cli.open_sftp()
            with sftp.open(remote_path, 'wb', 1024) as f:
                f.write(content)

    def exec_command(self, command, timeout=1800, environment=None):
        command = 'set -e\n' + command
        with self as cli:
            chan = cli.get_transport().open_session()
            chan.settimeout(timeout)
            chan.set_combine_stderr(True)
            if environment:
                str_env = ' '.join(f"{k}='{v}'"
                                   for k, v in environment.items())
                command = f'export {str_env} && {command}'
            chan.exec_command(command)
            out = chan.makefile("r", -1)
            return chan.recv_exit_status(), out.read()

    def exec_command_with_stream(self,
                                 command,
                                 timeout=1800,
                                 environment=None):
        command = 'set -e\n' + command
        with self as cli:
            chan = cli.get_transport().open_session()
            chan.settimeout(timeout)
            chan.set_combine_stderr(True)
            if environment:
                str_env = ' '.join(f"{k}='{v}'"
                                   for k, v in environment.items())
                command = f'export {str_env} && {command}'
            chan.exec_command(command)
            stdout = chan.makefile("r", -1)
            out = stdout.readline()
            while out:
                yield chan.exit_status, out
                out = stdout.readline()
            yield chan.recv_exit_status(), out

    def __enter__(self):
        if self.client is not None:
            raise RuntimeError('Already connected')
        return self.get_client()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.client.close()
        self.client = None
コード例 #48
0
  ver que um objeto do SSHClient
  foi criado e qual posição na
  memória ele está armazenado.


  Modificado em 14 de março de 2017
  por Vitor Mazuco ([email protected])

  Editado em 28 Setembro 2018
  por Rafael Baena Neto ([email protected])
  Alteração para PEP 8 e Python 3
"""

from paramiko.client import SSHClient

client = SSHClient()

print(client)












コード例 #49
0
ファイル: sshsource.py プロジェクト: nsetzer/YueMusicPlayer
    def fromPrivateKey(host,port=22,username=None,password=None,private_key=None):
        """deprecated see connect_v2 """

        #ssh -i /path/to//private_key user@localhost -p 2222
        #ssh -i /path/to//private_key username@host -p port

        if SSHClient is None:
            raise Exception("Paramiko not installed")

        src = SSHClientSource()
        print(host,port,username,password,private_key)

        pkey = None
        if private_key: # non-null, non-empty
            passphrase = "" # TODO: support passphrases
            pkey=paramiko.RSAKey.from_private_key_file(\
                private_key,passphrase)

        client = SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        print("\n---\nConnect With: host=%s:%s user=%s privatekey=`%s`\n"%(\
              host,port,username,private_key))

        try:
            if pkey:
                client.connect(host,port=port,
                               username=username,
                               pkey=pkey,timeout=1.0,compress=True)
            else:
                client.connect(host,port=port,
                               username=username,password=password,
                               timeout=1.0,compress=True)
        except BadHostKeyException as e:
            sys.stderr.write("got:      %s\n"%e.key.asbytes());
            sys.stderr.write("expected: %s\n"%e.expected_key.asbytes());

            msg = "Connection error on %s:%s using privatekey=%s\n"%(\
                host,port,private_key)
            msg += "you may need to clear ~/.ssh/known_hosts "+\
                "for entries related to %s:%s\n"%(host,port);
            sys.stderr.write(msg);
            raise Exception(msg);
        src.client = client
        src.ftp = client.open_sftp()

        src.host=host
        src.port=port

        return src
コード例 #50
0
ファイル: sshcontrol.py プロジェクト: schrer/shepherd-bot
def __close_ssh_client(client: SSHClient) -> None:
    client.close()
コード例 #51
0
class _SSHConnection:
    """
    Helper class handling SFTP communication.

    If provided with a hostname, automatically initiates the SFTP session.

    :param hostname: The SSH host, defaults to :const:`None`.
    :type hostname: str, optional
    :param port: The port where the SSH host is listening, defaults to :const:`None`.
    :type port: int, optional
    :param username: The username on the target SSH host, defaults to :const:`None`.
    :type username: str, optional

    :ivar client: The SSH session handler.
    :vartype client: paramiko.client.SSHClient
    :ivar sftp_session: The SFTP session handler.
    :vartype sftp_session: paramiko.sftp_client.SFTPClient
    """

    client = None
    sftp_session = None

    def __init__(self,
                 hostname: str = None,
                 port: int = None,
                 username: str = None):
        """Initialize object."""
        if hostname:
            self.set_session(hostname, port, username)

    def set_session(self,
                    hostname: str,
                    port: int = None,
                    username: str = None):
        """
        Set up a SFTP session.

        :param str hostname: The SSH host.
        :param port: The port where the SSH host is listening, defaults to
          :const:`None`.
        :type port: int, optional
        :param username: The username on the target SSH host, defaults to :const:`None`.
        :type username: str, optional
        """
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        if port and username:
            self.client.connect(hostname, port, username)
        elif port:
            self.client.connect(hostname, port)
        elif username:
            self.client.connect(hostname, username=username)
        else:
            self.client.connect(hostname)

        self.sftp_session = self.client.open_sftp()

    def close_session(self):
        """Close the SFTP session."""
        if self.is_active():
            self.client.close()

    def mkdir_p(self, path: str):
        """
        Simulate the :command:`mkdir -p <path>` Unix command.

        It creates the directory and all its non-existing parents.

        :param str path: The path to a directory.
        """
        dirs = []

        while len(path) > 1:
            dirs.append(path)
            path = os.path.dirname(path)

        if len(path) == 1 and not path.startswith("/"):
            dirs.append(path)

        while dirs:
            path = dirs.pop()
            try:
                self.sftp_session.stat(path)
            except IOError:
                self.sftp_session.mkdir(path, mode=0o755)

    # def write_file(self, content: str, path: str):
    #     """
    #     Create or overwrite a file under `path` and store `content` in it.
    #
    #     It creates the parent directories if required.
    #
    #     :param str content: The file content.
    #     :param str path: Path to the new file.
    #     """
    #     self.mkdir_p(os.path.normpath(os.path.dirname(path)))
    #
    #     with self.sftp_session.open(path, "w") as sftp_file:
    #         sftp_file.write(content)

    def put_file(self, local_path, remote_path):
        """
        Copy the local file at `local_path` into `remote_path`.

        It creates the parent directories if required.

        :param str local_path: Path to the local file.
        :param str remote_path: Path to the remote file.
        """
        self.mkdir_p(os.path.normpath(os.path.dirname(remote_path)))
        self.sftp_session.put(local_path, remote_path)

    def is_active(self) -> bool:
        """Check whether the SSH session is active or not.

        :return: `True` if the SSH session is active, `False` otherwise.
        :rtype: bool
        """
        if self.client:
            transport = self.client.get_transport()

            if transport is not None and transport.is_active():
                try:
                    transport.send_ignore()
                    return True
                except EOFError:
                    return False

        return False
コード例 #52
0
#!usr/bin/python
# -*- coding: utf-8 -*-
"""
Created on Wed Apr  5 22:05:25 2017
@author: Everton
"""

from paramiko.client import SSHClient
import paramiko

client = SSHClient()

client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print client

host = '192.168.202.1'
user = '******'
password = '******'

client.connect(hostname=host, username=user, password=password)
stdin, stdout, stderr = client.exec_command('/sbin/ifconfig')

if stderr.channel.recv_exit_status() != 0:
    print stderr.channel.recv_exit_status()
    print stderr.read()
else:
    print stdout.read()
コード例 #53
0
ファイル: BaseSSHWrapper.py プロジェクト: tabash7/MCWeb
class BaseSSHWrapper(BaseAutoscalingClass):
    """ A base class, which can connect via SSH to a machine."""
    def __init__(self,
                 readableName,
                 address,
                 pemFile=None,
                 password=None,
                 userName="******",
                 timeout=600):
        """
        Constr.
        @param readableName: A readale description of the machine. Must not be None.
        @param address: The address of the machine. Must not be None.
        @param pemFile: The pem file for SSH authentication.
        @param password: The password for SSH authentication.
        @param userName: The userName for SSH authentication. Must not be None.
        @param timeout: The SSH connection timeout. Must not be None. Must be positive.
        """
        super(BaseSSHWrapper, self).__init__(readableName=readableName)

        assert address is not None, "Address is None"
        assert userName is not None, "User name is None"
        assert timeout is not None, "Timeout is None"
        assert timeout > 0, "Timeout is not positive: " + str(timeout)
        assert pemFile is None or os.path.isfile(
            pemFile), "File \"%s\" does not exist" % (pemFile)

        self.address = address
        self.password = password
        self.pemFile = pemFile
        self.userName = userName
        self.timeout = timeout

        log.info("Creating entity \"%s\" at %s", self.readableName,
                 self.address)

        self.initSSHClient()

    def initSSHClient(self):
        """
        Tries to establish connection with the host.
        @raise SSHException: if the connection could not be established. 
        """
        self.client = None

        waitPeriod = 10
        attempts = 10
        # Try to connect but not more than attempts times
        for iteration in range(attempts):
            # If not the first time - sleep a bit. Do not bombard the server with requests
            if iteration > 0:
                time.sleep(waitPeriod)

            # Try establishing ssh client
            try:
                self.client = SSHClient()
                self.client.set_missing_host_key_policy(
                    paramiko.AutoAddPolicy())
                if self.pemFile is not None:
                    self.client.connect(self.address,
                                        username=self.userName,
                                        key_filename=self.pemFile,
                                        timeout=self.timeout)
                else:
                    self.client.connect(self.address,
                                        username=self.userName,
                                        password=self.password,
                                        timeout=self.timeout)
                break
            except (SSHException, socket.error) as e:
                self.client = None
                log.error("Could not connect to host %s err: %s",
                          self.readableName, str(e))

        # If we could not connect several times - throw an exception
        if self.client == None:
            raise SSHException('Could not connect ' + str(attempts) +
                               ' times in a row')

    def getSSHClient(self):
        """ 
        Retrieves an SSH connection to the machine.
        @return a connection
        @raise SSHException: if the connection could not be established. 
        """
        if self.client == None:
            log.warning("SSH connection to %s will be recreated", self.address)
            self.initSSHClient()
        return self.client

    def execRemoteCommand(self,
                          command,
                          asynch=False,
                          recoverFromFailure=True):
        """ 
        Executes the command on the remove machine.
        @param command: The command to execute on the remote host. Must not be None.
        @param asynch: A boolean flag, whether to run the command asynchrounously or not.
        @param recoverFromFailure: A boolean flag, whether to try to recover if the connection had staled or failed.
        @return the output of the command (a list of text lines), if it was run synchrounously.
        @raise SSHException: if a connection could not be established, or the command gave an error. 
        """
        assert command is not None, "Command is None"
        assert asynch is not None, "Asynch is None"
        assert recoverFromFailure is not None, "recoverFromFailure is None"

        try:
            _, stdout, stderr = self.getSSHClient().exec_command(command)
            output = []
            if not asynch:
                output = stdout.readlines()
                errors = stderr.readlines()
                if errors:
                    log.warning(
                        "Error messages encountered when connecting to %s, messages: %s",
                        self.address, formatOutput(errors))
            return output
        except (SSHException, socket.error) as e:
            if recoverFromFailure:
                # Mark the connection for re-instantiation and try again
                self.client = None
                return self.execRemoteCommand(command, asynch, False)
            else:
                raise e

    def close(self):
        """ 
        Closes the underlying connection.
        @raise SSHException: if a connection could not be closed. 
        """
        if self.client != None:
            self.client.close()
            self.client = None

    def __del__(self):
        self.close()
コード例 #54
0
class SystemVM(object):
    def __init__(self, host='default', vagrantDir=None, controlVagrant=True):
        global _defaultVagrantDir
        self.host = host
        self._controlVagrant = controlVagrant
        if vagrantDir is None:
            vagrantDir = _defaultVagrantDir
        self._vagrant = Vagrant(root=vagrantDir)
        self._startedVagrant = False
        self._sshClient = None
        self._sshConfigStr = None
        self._sshConfig = None
        self._sshHostConfig = None

    def maybeUp(self):
        if not self._controlVagrant:
            return
        state = self._vagrant.status(vm_name=self.host)[0].state
        if state == Vagrant.NOT_CREATED:
            self._vagrant.up(vm_name=self.host)
            self._startedVagrant = True
        elif state in [Vagrant.POWEROFF, Vagrant.SAVED, Vagrant.ABORTED]:
            raise Exception(
                "SystemVM testing does not support resume(), do not use vagrant suspend/halt"
            )
        elif state == Vagrant.RUNNING:
            self._startedVagrant = False
        else:
            raise Exception("Unrecognized vagrant state %s" % state)

    def maybeDestroy(self):
        if not self._controlVagrant or not self._startedVagrant:
            return
        self._vagrant.destroy(vm_name=self.host)
        if self._sshClient is not None:
            self._sshClient.close()

    def loadSshConfig(self):
        if self._sshConfig is None:
            self._sshConfigStr = self._vagrant.ssh_config(vm_name=self.host)
            configObj = StringIO(self._sshConfigStr)
            self._sshConfig = SSHConfig()
            # noinspection PyTypeChecker
            self._sshConfig.parse(configObj)
            self._sshHostConfig = self._sshConfig.lookup(self.host)

    @property
    def sshConfig(self):
        if self._sshConfig is None:
            self.loadSshConfig()
        return self._sshConfig

    @property
    def sshConfigStr(self):
        if self._sshConfigStr is None:
            self.loadSshConfig()
        return self._sshConfigStr

    @property
    def sshClient(self):
        if self._sshClient is None:
            self.loadSshConfig()
            self._sshClient = SSHClient()
            self._sshClient.set_missing_host_key_policy(AutoAddPolicy())
            self._sshClient.connect(self.hostname,
                                    self.sshPort,
                                    self.sshUser,
                                    key_filename=self.sshKey,
                                    timeout=10)
        return self._sshClient

    @property
    def hostname(self):
        return self._sshHostConfig.get('hostname', self.host)

    @property
    def sshPort(self):
        return int(self._sshHostConfig.get('port', 22))

    @property
    def sshUser(self):
        return self._sshHostConfig.get('user', 'root')

    @property
    def sshKey(self):
        return self._sshHostConfig.get('identityfile', '~/.ssh/id_rsa')
コード例 #55
0
class RemoteClient(object):
    """Remote Client is a wrapper over SSHClient with utility functions.

    Args:
        host (string): The hostname of the server to connect. It can be an IP
            address of the server also.
        user (string, optional): The user to connect to the remote server. It
            defaults to root

    Attributes:
        host (string): The hostname passed in as a the argument
        user (string): The user to connect as to the remote server
        client (:class:`paramiko.client.SSHClient`): The SSHClient object used
            for all the communications with the remote server.
        sftpclient (:class:`paramiko.sftp_client.SFTPClient`): The SFTP object
            for all the file transfer operations over the SSH.
    """
    def __init__(self, host, ip=None, user='******'):
        self.host = host
        self.ip = ip
        self.user = user
        self.client = SSHClient()
        self.sftpclient = None
        self.client.set_missing_host_key_policy(AutoAddPolicy())
        self.client.load_system_host_keys()
        logging.debug("RemoteClient created for host: %s", host)

    def startup(self):
        """Function that starts SSH connection and makes client available for
        carrying out the functions. It tries with the hostname, if it fails
        it tries with the IP address if supplied
        """
        try:
            logging.debug("Trying to connect to remote server %s", self.host)
            self.client.connect(self.host, port=22, username=self.user)
            self.sftpclient = self.client.open_sftp()
        except (SSHException, socket.error):
            if self.ip:
                logging.warning("Connection with hostname failed. Retrying "
                                "with IP")
                self._try_with_ip()
            else:
                logging.error("Connection to %s failed.", self.host)
                raise ClientNotSetupException('Could not connect to the host.')

    def _try_with_ip(self):
        try:
            logging.debug("Connecting to IP:%s User:%s", self.ip, self.user)
            self.client.connect(self.ip, port=22, username=self.user)
            self.sftpclient = self.client.open_sftp()
        except (SSHException, socket.error):
            logging.error("Connection with IP (%s) failed.", self.ip)
            raise ClientNotSetupException('Could not connect to the host.')

    def download(self, remote, local):
        """Downloads a file from remote server to the local system.

        Args:
            remote (string): location of the file in remote server
            local (string): path where the file should be saved
        """
        if not self.sftpclient:
            raise ClientNotSetupException(
                'Cannot download file. Client not initialized')

        try:
            self.sftpclient.get(remote, local)
            return "Download successful. File at: {0}".format(local)
        except OSError:
            return "Error: Local file %s doesn't exist." % local
        except IOError:
            return "Error: Remote location %s doesn't exist." % remote

    def upload(self, local, remote):
        """Uploads the file from local location to remote server.

        Args:
            local (string): path of the local file to upload
            remote (string): location on remote server to put the file
        """
        if not self.sftpclient:
            raise ClientNotSetupException(
                'Cannot upload file. Client not initialized')

        try:
            self.sftpclient.put(local, remote)
            return "Upload successful. File at: {0}".format(remote)
        except OSError:
            return "Error: Local file %s doesn't exist." % local
        except IOError:
            return "Error: Remote location %s doesn't exist." % remote

    def exists(self, filepath):
        """Returns whether a file exists or not in the remote server.

        Args:
            filepath (string): path to the file to check for existance

        Returns:
            True if it exists, False if it doesn't
        """
        if not self.client:
            raise ClientNotSetupException(
                'Cannot run procedure. Client not initialized')
        cin, cout, cerr = self.client.exec_command('stat {0}'.format(filepath))
        if len(cout.read()) > 5:
            return True
        elif len(cerr.read()) > 5:
            return False

    def run(self, command):
        """Run a command in the remote server.

        Args:
            command (string): the command to be run on the remote server

        Returns:
            tuple of three strings containing text from stdin, stdout an stderr
        """
        if not self.client:
            raise ClientNotSetupException(
                'Cannot run procedure. Client not initialized')

        #buffers = self.client.exec_command(command, timeout=30)
        buffers = self.client.exec_command(command)
        output = []
        for buf in buffers:
            try:
                output.append(buf.read())
            except IOError:
                output.append('')

        return tuple(output)

    def get_file(self, filename):
        """Reads content of filename on remote server

        Args:
            filename (string): name of file to be read from remote server

        Returns:
            tuple: True/False, file like object / error
        """
        f = StringIO.StringIO()
        try:
            r = self.sftpclient.getfo(filename, f)
            f.seek(0)
            return r, f
        except Exception as err:
            return False, err

    def put_file(self, filename, filecontent):
        """Puts content to a file on remote server

        Args:
            filename (string): name of file to be written on remote server
            filecontent (string): content of file

        Returns:
            tuple: True/False, file size / error
        """
        f = StringIO.StringIO()
        f.write(filecontent)
        f.seek(0)

        try:
            r = self.sftpclient.putfo(f, filename)
            return True, r.st_size
        except Exception as err:
            return False, err

    def mkdir(self, dirname):
        """Creates a new directory.

        Args:
            dirname (string): the full path of the directory that needs to be
                created

        Returns:
            a tuple containing the success or failure of operation and dirname
                on success and error on failure
        """
        try:
            self.sftpclient.mkdir(dirname)
            return True, dirname
        except Exception as err:
            return False, err

    def listdir(self, dirname):
        """Lists all the files and folders in a directory.

        Args:
            dirname (string): the full path of the directory that needs to be
                listed

        Returns:
            a list of the files and folders in the directory
        """
        try:
            r = self.sftpclient.listdir(dirname)
            return True, r
        except Exception as err:
            return False, err

    def close(self):
        """Close the SSH Connection
        """
        self.client.close()

    def __repr__(self):
        return "RemoteClient({0}, ip={1}, user={2})".format(
            self.host, self.ip, self.user)
コード例 #56
0
class USIEngine:
    def __init__(self,
                 name,
                 host,
                 engine_path,
                 nodes=None,
                 multiPV=1,
                 threads=1,
                 delay=0,
                 delay2=0):
        self.name = name
        self.nodes = nodes
        self.multiPV = multiPV
        self.quit_event = threading.Event()

        self.client = SSHClient()
        self.client.set_missing_host_key_policy(paramiko.client.WarningPolicy)
        #self.client.load_system_host_keys()
        keys = self.client.get_host_keys()
        keys.clear()
        self.client.connect(host)
        dirname = os.path.dirname(engine_path)
        command = f'cd {dirname} && {engine_path}'
        self.stdin, self.stdout, self.stderr = \
                self.client.exec_command(command, bufsize=0)

        self.queue = queue.Queue()
        self.watcher_thread = threading.Thread(target=self.stream_watcher,
                                               name='engine_watcher',
                                               args=(self.stdout, ))
        self.watcher_thread.start()
        self.pvs = [[]] * multiPV
        self.status = 'wait'
        self.position = 'startpos'

        self.send('usi')
        self.wait_for('usiok')
        self.set_option('Threads', threads)
        self.set_option('USI_Ponder', 'false')
        self.set_option('NetworkDelay', delay)
        self.set_option('NetworkDelay2', delay2)
        self.set_option('MultiPV', multiPV)
        if nodes:
            self.set_option('NodesLimit', nodes)
        #self.send('isready')
        #self.wait_for('readyok')

    def stream_watcher(self, stream):
        # for line in iter(stream.readline, b''):
        prog = re.compile('.*score cp (-?\d+) (?:multipv (\d+))? .*pv (.+)$')
        #for line in iter(stream.readline, b''):
        while (not self.quit_event.isSet()) and (not stream.closed):
            line = stream.readline().strip()
            if len(line):
                logging.debug(f'{self.name} > {line}')
                print(f'info string {self.name} > {line}', flush=True)
                match = prog.match(line)
                if match:
                    logging.debug(f'match: {match.group(1, 2, 3)}')
                    if match.group(2):
                        # multi PV
                        num = int(match.group(2)) - 1
                    else:
                        # single PV
                        num = 0
                    logging.debug(f'{self.name}: Found score of pv {num}')
                    self.pvs[num] = [int(match.group(1)), match.group(3)]

                # bestmove
                if line.startswith('bestmove'):
                    self.status = 'wait'

                self.queue.put(line)
        logging.debug(f'{self.name}: terminating the engine watcher thread')

    def set_option(self, name, value):
        self.send(f'setoption name {name} value {value}')

    def __del__(self):
        pass
        #self.terminate()

    def terminate(self):
        self.stop()
        self.quit_event.set()
        self.send('usi')
        self.watcher_thread.join(1)
        self.send('quit')
        self.status = 'quit'
        #self.client.close()

    def send(self, command):
        logging.debug(f'sending {command} to {self.name}')
        print(f'info string sending {command} to {self.name}', flush=True)
        self.stdin.write((command + '\n').encode('utf-8'))
        self.stdin.flush()

    def wait_for(self, command):
        logging.debug(f'{self.name}: waiting for {command}')
        lines = ""
        while self.client.get_transport().is_active():
            line = self.queue.get()
            lines += f'{line}\n'
            if (line == command):
                logging.debug(f'{self.name}: found {command}')
                self.status = 'wait'
                return lines

    def wait_for_bestmove(self):
        logging.debug(f'{self.name}: waiting for bestmove...')
        infostr(f'{self.name}: waiting for bestmove...')
        while self.client.get_transport().is_active():
            line = self.queue.get()
            if (line.startswith('bestmove')):
                logging.debug(f'{self.name}: found bestmove')
                infostr(f'{self.name}: found bestmove')
                bestmove = line[9:].split()[0].strip()
                self.status = 'wait'
                return bestmove

    def set_position(self, pos):
        self.position = pos
        self.send(f'position {pos}')

    def clear_queue(self):
        while True:
            try:
                line = self.queue.get_nowait()
                print(f'info string {self.name}: clearing queue: {line}',
                      flush=True)
            except queue.Empty:
                break

    def ponder(self, command):
        infostr(f'{self.name}: in ponder()')
        self.go_command = command
        if 'ponder' not in command:
            command = command.replace('go', 'go ponder')
        self.send(command)
        self.status = 'ponder'
        infostr(f'{self.name}: end of ponder()')

    def stop(self):
        infostr(f'{self.name}: in stop()')
        if self.status in ['go', 'ponder']:
            self.send('stop')
            self.wait_for_bestmove()
            self.status = 'wait'
コード例 #57
0
ファイル: models.py プロジェクト: jank0s/nat64check
    def run_browser_tests(self):
        common_options = [
            'phantomjs',
            '--debug=true',
            '--ignore-ssl-errors=true',
            '--local-url-access=false',
            '--local-storage-path=/dev/null',
            '--offline-storage-path=/dev/null',
            '/dev/stdin',
        ]

        browser_command = ' '.join(common_options +
                                   [shlex.quote(self.idna_url)])

        # Read the private key
        private_key = RSAKey.from_private_key_file(settings.SSH_PRIVATE_KEY)

        # Do the v4-only, v6-only and the NAT64 request in parallel
        v4only_client = SSHClient()
        v4only_client.load_host_keys(settings.SSH_KNOWN_HOSTS)
        v4only_client.connect(settings.V4_HOST,
                              username=settings.SSH_USERNAME,
                              pkey=private_key,
                              allow_agent=False,
                              look_for_keys=False)

        logger.debug("Running '{}' on {}".format(browser_command,
                                                 settings.V4_HOST))
        v4only_stdin, v4only_stdout, v4only_stderr = v4only_client.exec_command(
            browser_command, timeout=120)

        if self.ipv6_dns_results:
            v6only_client = SSHClient()
            v6only_client.load_host_keys(settings.SSH_KNOWN_HOSTS)
            v6only_client.connect(settings.V6_HOST,
                                  username=settings.SSH_USERNAME,
                                  pkey=private_key,
                                  allow_agent=False,
                                  look_for_keys=False)

            logger.debug("Running '{}' on {}".format(browser_command,
                                                     settings.V4_HOST))
            v6only_stdin, v6only_stdout, v6only_stderr = v6only_client.exec_command(
                browser_command, timeout=120)
        else:
            v6only_client = v6only_stdin = v6only_stdout = v6only_stderr = None

        nat64_client = SSHClient()
        nat64_client.load_host_keys(settings.SSH_KNOWN_HOSTS)
        nat64_client.connect(settings.NAT64_HOST,
                             username=settings.SSH_USERNAME,
                             pkey=private_key,
                             allow_agent=False,
                             look_for_keys=False)

        logger.debug("Running '{}' on {}".format(browser_command,
                                                 settings.V4_HOST))
        nat64_stdin, nat64_stdout, nat64_stderr = nat64_client.exec_command(
            browser_command, timeout=120)

        # Placeholders
        v4only_img = None
        v4only_img_bytes = None
        v6only_img = None
        v6only_img_bytes = None
        nat64_img = None
        nat64_img_bytes = None

        self.v4only_data = {}
        self.v6only_data = {}
        self.nat64_data = {}

        # Push the test script to the workers
        script_filename = os.path.realpath(
            os.path.join(os.path.dirname(__file__), 'render_page.js'))
        script = open(script_filename, 'rb').read()

        v4only_stdin.write(script)
        v4only_stdin.close()
        v4only_stdin.channel.shutdown_write()

        if v6only_client:
            v6only_stdin.write(script)
            v6only_stdin.close()
            v6only_stdin.channel.shutdown_write()

        nat64_stdin.write(script)
        nat64_stdin.close()
        nat64_stdin.channel.shutdown_write()

        # Wait for tests to finish
        try:
            logger.debug("Receiving data from IPv4-only test")

            v4only_json = v4only_stdout.read()
            v4only_debug = v4only_stderr.read()
            v4only_exit = v4only_stdout.channel.recv_exit_status()

            self.v4only_data = json.loads(
                v4only_json.decode('utf-8'),
                object_pairs_hook=OrderedDict) if v4only_json else {}
            self.v4only_debug = v4only_debug.decode('utf-8')

            self.v4only_data['exit_code'] = v4only_exit

            if 'image' in self.v4only_data:
                if self.v4only_data['image']:
                    v4only_img_bytes = base64.decodebytes(
                        self.v4only_data['image'].encode('ascii'))
                    # noinspection PyTypeChecker
                    v4only_img = skimage.io.imread(
                        io.BytesIO(v4only_img_bytes))
                del self.v4only_data['image']
        except socket.timeout:
            logger.error("{}: IPv4-only load timed out".format(self.url))

        if v6only_client:
            try:
                logger.debug("Receiving data from IPv6-only test")

                v6only_json = v6only_stdout.read()
                v6only_debug = v6only_stderr.read()
                v6only_exit = v6only_stdout.channel.recv_exit_status()

                self.v6only_data = json.loads(
                    v6only_json.decode('utf-8'),
                    object_pairs_hook=OrderedDict) if v6only_json else {}
                self.v6only_debug = v6only_debug.decode('utf-8')

                self.v6only_data['exit_code'] = v6only_exit

                if 'image' in self.v6only_data:
                    if self.v6only_data['image']:
                        v6only_img_bytes = base64.decodebytes(
                            self.v6only_data['image'].encode('ascii'))
                        # noinspection PyTypeChecker
                        v6only_img = skimage.io.imread(
                            io.BytesIO(v6only_img_bytes))
                    del self.v6only_data['image']
            except socket.timeout:
                logger.error("{}: IPv6-only load timed out".format(self.url))
        else:
            logger.info("{}: Not running IPv6-only test".format(self.url))

        try:
            logger.debug("Receiving data from NAT64 test")

            nat64_json = nat64_stdout.read()
            nat64_debug = nat64_stderr.read()
            nat64_exit = nat64_stdout.channel.recv_exit_status()

            self.nat64_data = json.loads(
                nat64_json.decode('utf-8'),
                object_pairs_hook=OrderedDict) if nat64_json else {}
            self.nat64_debug = nat64_debug.decode('utf-8')

            self.nat64_data['exit_code'] = nat64_exit

            if 'image' in self.nat64_data:
                if self.nat64_data['image']:
                    nat64_img_bytes = base64.decodebytes(
                        self.nat64_data['image'].encode('ascii'))
                    # noinspection PyTypeChecker
                    nat64_img = skimage.io.imread(io.BytesIO(nat64_img_bytes))
                del self.nat64_data['image']
        except socket.timeout:
            logger.error("{}: NAT64 load timed out".format(self.url))

        # Done talking to workers, close connections
        if v4only_client:
            v4only_client.close()
        if v6only_client:
            v6only_client.close()
        if nat64_client:
            nat64_client.close()

        # Calculate score based on resources
        v4only_resources_ok = self.v4only_resources[0]
        if v4only_resources_ok > 0:
            self.v6only_resource_score = min(
                self.v6only_resources[0] / v4only_resources_ok, 1)
            logger.info("{}: IPv6-only Resource Score = {:0.2f}".format(
                self.url, self.v6only_resource_score))

            self.nat64_resource_score = min(
                self.nat64_resources[0] / v4only_resources_ok, 1)
            logger.info("{}: NAT64 Resource Score = {:0.2f}".format(
                self.url, self.nat64_resource_score))
        else:
            logger.error(
                "{}: did not load over IPv4-only, unable to perform resource test"
                .format(self.url))

        return_value = 0
        if v4only_img_bytes:
            # Store the image
            self.v4only_image.save('v4.png',
                                   ContentFile(v4only_img_bytes),
                                   save=False)
        else:
            return_value |= 1

        if v6only_img_bytes:
            # Store the image
            self.v6only_image.save('v6.png',
                                   ContentFile(v6only_img_bytes),
                                   save=False)
        else:
            return_value |= 2

        if nat64_img_bytes:
            # Store the image
            self.nat64_image.save('nat64.png',
                                  ContentFile(nat64_img_bytes),
                                  save=False)
        else:
            return_value |= 4

        if v4only_img is not None:
            logger.debug("{}: Loading IPv4-only screenshot".format(self.url))

            if v6only_img is not None:
                logger.debug("{}: Loading IPv6-only screenshot".format(
                    self.url))

                # Suppress stupid warnings
                with warnings.catch_warnings(record=True):
                    self.v6only_image_score = compare_ssim(v4only_img,
                                                           v6only_img,
                                                           multichannel=True)
                    logger.info("{}: IPv6-only Image Score = {:0.2f}".format(
                        self.url, self.v6only_image_score))
            else:
                logger.warning(
                    "{}: did not load over IPv6-only, 0 score".format(
                        self.url))
                self.v6only_image_score = 0.0

            if nat64_img is not None:
                logger.debug("{}: Loading NAT64 screenshot".format(self.url))

                # Suppress stupid warnings
                with warnings.catch_warnings(record=True):
                    self.nat64_image_score = compare_ssim(v4only_img,
                                                          nat64_img,
                                                          multichannel=True)
                    logger.info("{}: NAT64 Image Score = {:0.2f}".format(
                        self.url, self.nat64_image_score))
            else:
                logger.warning("{}: did not load over NAT64, 0 score".format(
                    self.url))
                self.nat64_image_score = 0.0

        else:
            logger.error(
                "{}: did not load over IPv4-only, unable to perform image test"
                .format(self.url))

        self.save()

        return return_value
コード例 #58
0
ファイル: ssh.py プロジェクト: qmutz/xpra
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'", user_config_file)
            try:
                log("%i hosts found", len(ssh_config.get_hostnames()))
            except KeyError:
                pass
            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) 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)
            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
コード例 #59
0
def set_client(ip,
               port,
               numb_conn,
               Tunnel,
               hostname=None,
               username=None,
               Key_path=None,
               passphrase=None):
    """
    this method is responsible for establishing a connection to a server (Ip and port of the server needed to be specified)
    In a direct LAN enviroment if Tunnel was Fale (hostname,username,keypath,passphrase) aren't needed;
    connection thought SSH tunneling if tunnel was True 
    Hostname and username needed to be specified 
    keypath and pass phrase needed to be specified if there isn't an ssh agent otherwise it isn't needed.
    """
    client = []
    transport = None
    if Tunnel:
        server_address = (ip, port)
        sshclient = SSHClient()
        sshclient.load_system_host_keys()
        sshclient.set_missing_host_key_policy(AutoAddPolicy())
        try:
            sshclient.connect(hostname=hostname,
                              username=username,
                              passphrase=passphrase,
                              key_filename=Key_path)
            transport = sshclient.get_transport()
        except (BadHostKeyException, SSHException,
                AuthenticationException) as e:
            print("A problem trying to connection to the Host {}".format(
                hostname))
            raise e
            transport.close()
            return None, None
        try:
            for i in range(numb_conn):
                client.append(
                    transport.open_channel(kind='direct-tcpip',
                                           src_addr=server_address,
                                           dest_addr=server_address))
        except SSHException as e:
            print(
                "problem Has been faced trying to make direct-tcpip conneciton"
            )
            raise e
            for i in client:
                i.close()
            sshclient.get_transport().close()
            raise (KeyboardInterrupt)
        except (KeyboardInterrupt):
            for i in client:
                i.close()
            sshclient.get_transport().close()
            return None, None
    else:
        server_address = (ip, port)
        try:
            for i in range(numb_conn):
                client.append(socket.socket(socket.AF_INET,
                                            socket.SOCK_STREAM))
                client[i].connect(server_address)
        except (OSError):
            print("The connection #{} failed".format(i + 1))
            print("The process is stopping")
            raise (KeyboardInterrupt)
        except (KeyboardInterrupt):
            for i in client:
                i.close()
            return None, None

    return client, transport  #returning the connection object for further use
コード例 #60
0
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()