Esempio n. 1
0
def run_command_on_instances(instances, command, env_file, key_file):
    ec2r = boto3.resource('ec2')

    ssh = SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    for instance in instances['Instances']:
        inst_id = instance['InstanceId']
        instance_obj = ec2r.Instance(inst_id)
        instance_obj.reload()
        inst_ip = instance_obj.public_dns_name
        sys.stderr.write("Copying env_file.txt to host...\n")
        call([
            'scp', '-i', key_file, env_file.name,
            'ubuntu@%s:/home/ubuntu/env_file.txt' % inst_ip
        ])
        time.sleep(2)
        sys.stderr.write("Running command %s on remote host...\n" % (command))
        ssh.connect(inst_ip, username='******', key_filename=key_file)
        stdin, stdout, stderr = ssh.exec_command(command)
        stdin.flush()
        output = stdout.read().splitlines()
        error = stderr.read().splitlines()
        sys.stderr.write(
            'Startup standard output\n%s:Startup standard error:\n%s\n' %
            (output, error))
        ssh.close()
Esempio n. 2
0
def execute_remote_command(ip, command, quiet=False, username=REMOTE_USER):
    client = SSHClient()
    client.set_missing_host_key_policy(IgnoreHostKeyPolicy)
    client.connect(ip, username=username)
    stdin, stdout, stderr = client.exec_command(command)

    rc = stdout.channel.recv_exit_status()
    out = stdout.read().decode('utf-8').splitlines()
    err = stderr.read().decode('utf-8').splitlines()

    client.close()

    if rc != 0 and not quiet:
        log = logging.getLogger('TPCH')

        log.error("ssh command returned %d" % rc)
        log.error("ssh -l %s %s %s" % (username, ip, command))
        print(command)
        for line in out:
            print(line)
        for line in err:
            print(line)
        print()

    if rc != 0:
        raise RuntimeError(command)

    return out, err
def create_worker(host):
    config = SSHConfig()
    proxy = None
    if os.path.exists(os.path.expanduser('~/.ssh/config')):
        config.parse(open(os.path.expanduser('~/.ssh/config')))
        if host.hostname is not None and \
                        'proxycommand' in config.lookup(host.hostname):
            proxy = ProxyCommand(config.lookup(host.hostname)['proxycommand'])

    # proxy = paramiko.ProxyCommand("ssh -o StrictHostKeyChecking=no [email protected] nc 118.138.239.241 22")

    worker = SSHClient()
    worker.load_system_host_keys()
    worker.set_missing_host_key_policy(AutoAddPolicy())

    worker.hostname = host.hostname  # store all this for later reference (e.g., logging, reconnection)
    worker.username = host.username
    worker.password = host.password
    worker.proxy = proxy
    if not host.key_filename is None:
        worker.pkey = RSAKey.from_private_key_file(host.key_filename,
                                                   host.key_password)
    else:
        worker.pkey = None

    # time.sleep(4)
    # worker.connect(hostname=host.hostname, username=host.username, password=host.password, key_filename=host.key_filename, sock=proxy, timeout=3600)

    worker.connect(hostname=host.hostname,
                   username=host.username,
                   password=host.password,
                   pkey=worker.pkey,
                   sock=proxy)

    return worker
Esempio n. 4
0
def try_ssh(virtual_machine: VirtualMachine, retries: int = 10):
    """
    Try to connect to a virtual machine using ssh
    :param virtual_machine: the virtual machine
    :param retries: the maximum of retries
    """
    retry = 0
    connected = False

    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.load_system_host_keys()
    while retry < retries and not connected:
        try:
            logger.debug('Trying ssh connection to %s %s out of %s retries',
                         virtual_machine.hostname, retry, retries)
            ssh.connect(virtual_machine.hostname,
                        port=virtual_machine.ssh_port,
                        username=virtual_machine.ssh_username,
                        password=virtual_machine.ssh_password)
            connected = True
        except (BadHostKeyException, AuthenticationException, SSHException,
                socket.error):
            time.sleep(10)
        retry += 1

    if not connected:
        raise Exception(
            '[{}] Unable to connect to the machine after {} tries'.format(
                virtual_machine.hostname, retry))
    logger.info('Connection established to %s after %d out of %d retries',
                virtual_machine.name, retry, retries)
Esempio n. 5
0
def run_remote_command(args, hostgroup_name, hostgroup, command):
    """Run the appropriate command on hosts in a given host group based on
the action being taken"""
    client = SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(AutoAddPolicy())
    ssh_key = None
    host_results = {}
    if args.ssh_key:
        ssh_key = "%s/.ssh/%s" % (os.environ['HOME'], args.ssh_key)
    for host in hostgroup:
        try:
            client.connect(host.address,
                           allow_agent=True,
                           username=os.getenv('USER'))
        except Exception, e:
            print "Error running remote command on (%s:%s) (%s)" % (
                host.name, host.address, e)
            continue
        print "(%s:%s) => (%s)" % (host.name, host.address, command)
        chan = client.get_transport().open_session()
        chan.set_combine_stderr(True)
        chan.exec_command(command)
        dump_channel(chan)
        rv = chan.recv_exit_status()
        host_results[host.name] = rv
        chan.close()
        client.close()
        _summarize_exit_code(rv)
Esempio n. 6
0
 def ssh(self, address):
     """
     Create an SSH connection to the instance.
     """
     try:
         ssh = SSHClient()
         # ssh.load_system_host_keys()
         """
         VM instances' IP addresses are indeterminate,
         so there is no good way to detect
         a Man In The Middle attack.
         The fact that a different instance now has the same IP address
         could be a MITM attack, or it could be due to deletion
         of the old instance, or it could be due to re-arranging of
         IP addresses among the same set of instances.
         We don't care much about MITM attacks anyway - they would
         merely cause us to test the correct functioning of some
         attacker's computer, instead of our instance, which would
         be bad only if the attacker's computer were less broken
         than our instance, thus hiding problems with our instance.
         This is a less bad problem than SSH connectivity randomly
         failing when IP addresses get re-used.
         So just disable protection against MITM attacks, by:
         1. Auto-adding unknown hosts to the known hosts file and
         2. Not loading any known hosts files.
         """
         ssh.set_missing_host_key_policy(AutoAddPolicy())
         ssh.connect(address,
                     username=self.params['ssh_username'],
                     key_filename=self.params['ssh_key_file'])
     except:
         self.logger.debug(self.instance().get_console_output(10))
         raise
     return ssh
Esempio n. 7
0
    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')
Esempio n. 8
0
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})
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()
Esempio n. 10
0
class MySSHClient():
    def __init__(self):
        self.ssh_client = SSHClient()

    def ssh_login(self, host_ip, username, password):
        try:
            self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
            self.ssh_client.connect(host_ip,
                                    port=22,
                                    username=username,
                                    password=password)
        except AuthenticationException:
            log.warning('username or password error')
            return 1001
        except NoValidConnectionsError:
            log.warning('connect time out')
            return 1002
        except:
            log.warning('unkown error')
            print("Unexpect error:", sys.exc_info()[0])
            return 1003
        return 1000

    def ssh_logout(self):
        log.warning('will exit host')
        self.ssh_client.close()

    def execute_some_command(self, command):
        _, stdout, _ = self.ssh_client.exec_command(command)
        print(stdout.read().decode())
Esempio n. 11
0
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
def ssh_login(host, username, password):
    r = SSH_Client()
    r.load_system_host_keys()
    r.set_missing_host_key_policy(AutoAddPolicy())

    info("Trying to open a SSH connection to {}".format(host))

    try:
        r.connect(host, username=username, password=password)
    except SSH_BadHostKeyException as errstr:
        error("SSH host key for {0} could not be verified: {1}".format(host, errstr))
        return False
    except SSH_AuthenticationException:
        error("SSH authentication failed for {}".format(host))
        return False
    except SSH_SSHException as errstr:
        error("Unknown SSH error while connecting to {0}: {1}".format(host, errstr))
        return False
    except OSError as err:
        error("Can't connect to SSH server {0}: '{1}'".format(host, err))
        return False
    except:
        error("Unknown error encountered while connecting to SSH server {}".format(host))
        return False

    info("SSH connection to {} opened successfully".format(host))
    return r
Esempio n. 13
0
def ssh_connection(
    instance: Instance, ssh_credentials: SSHCredentials
) -> Iterator[SSHClient]:
    """Connect to server and yield SSH client."""
    username, key_filename = ssh_credentials

    instance_ssh_port: int = cast(int, instance.ssh_port)
    ignore_host_key_policy: Union[
        Type[MissingHostKeyPolicy], MissingHostKeyPolicy
    ] = cast(
        Union[Type[MissingHostKeyPolicy], MissingHostKeyPolicy], IgnoreHostKeyPolicy
    )

    client = SSHClient()
    client.set_missing_host_key_policy(ignore_host_key_policy)
    client.connect(
        hostname=str(instance.public_ip),
        port=instance_ssh_port,
        username=username,
        key_filename=key_filename,
    )

    yield client

    client.close()
Esempio n. 14
0
def create_backup_repository(service):
    """
    - create filesystem folders
    - store ssh key
    - create subaccount
    """
    # Create folder and SSH key
    client = SSHClient()
    client.load_system_host_keys()
    client.connect(**settings.STORAGE_SERVER)
    ftp = client.open_sftp()
    dirname = str(uuid4())
    ftp.mkdir(dirname)
    ftp.chdir(dirname)
    ftp.mkdir(".ssh")
    ftp.chdir(".ssh")
    with ftp.open("authorized_keys", "w") as handle:
        handle.write(service.last_report.ssh_key)

    # Create account on the service
    url = "https://robot-ws.your-server.de/storagebox/{}/subaccount".format(
        settings.STORAGE_BOX)
    response = requests.post(
        url,
        data={
            "homedirectory": "weblate/{}".format(dirname),
            "ssh": "1",
            "external_reachability": "1",
            "comment": "Weblate backup service {}".format(service.pk),
        },
        auth=(settings.STORAGE_USER, settings.STORAGE_PASSWORD),
    )
    data = response.json()
    return "ssh://{}@{}:23/./backups".format(data["subaccount"]["username"],
                                             data["subaccount"]["server"])
Esempio n. 15
0
class SshClient():
    def __init__(self):
        self.ssh_client = SSHClient()

    def ssh_login(self, host_ip, username, password):
        try:
            # 设置允许连接known_hosts文件中的主机(默认连接不在known_hosts文件中的主机会拒绝连接抛出SSHException)
            self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
            self.ssh_client.connect(host_ip, port=22, username=username, password=password)
        except AuthenticationException:
            print('username or password error')
            return 1001
        except NoValidConnectionsError:
            print('connect time out')
            return 1002
        except:
            print("Unexpected error:", sys.exc_info()[0])
            return 1003
        return 1000

    def execute_some_command(self, command):
        stdin, stdout, stderr = self.ssh_client.exec_command(command)
        print(stdout.read().decode())
        return stdout.read().decode()

    def ssh_logout(self):
        self.ssh_client.close()
Esempio n. 16
0
	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
Esempio n. 17
0
class SSHTarget:
    def __init__(self, host, port=22, timeout=10):
        self.host = host
        self.port = int(port)
        self.timeout = timeout

        self.ssh_client = SSHClient()
        self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())

    def connect(self, username, password):
        self.ssh_client.connect(self.host,
                                self.port,
                                username,
                                password,
                                timeout=self.timeout)

    def connect_with_key(self, username, key):
        self.ssh_client.connect(self.host,
                                self.port,
                                username,
                                key_filename=key,
                                timeout=self.timeout)

    def close(self):
        self.ssh_client.close()

    def send(self, data):
        tdin, stdout, stderr = self.ssh_client.exec_command(data)

        if stderr.readline() != "":
            Logger.warning("STDERR was not null! (" +
                           stderr.read().decode("utf-8") + ")")

        return stdout.read().decode("utf-8")
Esempio n. 18
0
def create_worker(host):
    config = SSHConfig()
    proxy = None
    if os.path.exists(os.path.expanduser('~/.ssh/config')):
        with open(os.path.expanduser('~/.ssh/config')) as f:
            config.parse(f)
        if host.hostname is not None and 'proxycommand' in config.lookup(
                host.hostname):
            proxy = ProxyCommand(config.lookup(host.hostname)['proxycommand'])

    worker = SSHClient()
    worker.load_system_host_keys()
    worker.set_missing_host_key_policy(AutoAddPolicy())

    # store data for later reference
    worker.host = host.hostname
    worker.username = host.username
    worker.password = host.password
    worker.key_filename = host.key_filename

    worker.connect(hostname=host.hostname,
                   username=host.username,
                   password=host.password,
                   key_filename=host.key_filename,
                   sock=proxy)
    return worker
Esempio n. 19
0
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
    def run(self):
        if self.GITHUB_TOKEN is None:
            logging.critical('No github OAuth token defined in the GITHUB_TOKEN env variable')
            sys.exit(1)
        if self.SSH_PKEY is None:
            logging.critical('SSH_KEY not configured, please set it to you private SSH key file')
            sys.exit(1)

        github = Github(self.GITHUB_TOKEN)
        ssh = SSHClient()
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.SSH_PKEY = os.path.expanduser(self.SSH_PKEY)

        orga = github.get_organization('nuxeo')  # type: Organization
        repo = orga.get_repo('nuxeo.com')  # type: Repository
        opened_pulls = [('/var/www/nuxeo.com/pr-%d.' % pull.number) + self.PREVIEW_DOMAIN for pull in repo.get_pulls()]

        try:
            proxy = ProxyCommand(('ssh -i %s -W 10.10.0.63:22 ' % self.SSH_PKEY) + self.BASTION_IP)
            ssh.connect('10.10.0.63', username='******', sock=proxy, key_filename=self.SSH_PKEY)
            _, stdout, _ = ssh.exec_command('ls -d /var/www/nuxeo.com/pr-*')
            [ssh.exec_command('rm -rf ' + line.strip()) for line in stdout.readlines() if line.strip() not in opened_pulls]
            ssh.close()
        except SSHException, e:
            logging.critical('Could work on remote: %s', e)
            sys.exit(1)
Esempio n. 21
0
    def create_new_connection(self, index):
        """Attempts to create a new SSHThread object at the given index"""
        # Spawns a paramiko SSHClient
        ssh_client = SSHClient()
        ssh_client.load_system_host_keys()
        ssh_client.set_missing_host_key_policy(AutoAddPolicy())

        # We loop until we can make a connection
        while True:
            # Get the next ssh connection name (hostname) to attempt
            ssh_name = next(self.ssh_gen)
            # Ignore if we already have a connection with this computer
            if ssh_name in self.active_connections:
                continue

            # Try the connection and continue on a fail
            try:
                ssh_client.connect(ssh_name, timeout=TIMEOUT, port=PORT)
            except Exception as e:
                #print(e)
                time.sleep(0.1)
                continue

            # Store connection in array and add hostname to set
            self.ssh_threads[index] = SSHThread(ssh_client, self.command,
                                                ssh_name, self.print_finish)
            self.active_connections.add(ssh_name)

            print("Connection made with %s." % ssh_name)
            break
Esempio n. 22
0
class RemoteClient(object):
    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):
        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 PasswordRequiredException:
            raise ClientNotSetupException('Pubkey is encrypted.')

        except SSHException as e:
            raise ClientNotSetupException(e)

        except:
            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 PasswordRequiredException:
            raise ClientNotSetupException('Pubkey is encrypted.')

        except SSHException as e:
            raise ClientNotSetupException(e)

        except socket.error:
            logging.error("Connection with IP (%s) failed.", self.ip)
            raise ClientNotSetupException('Could not connect to the host.')

    def run(self, command):
        if not self.client:
            raise ClientNotSetupException(
                'Cannot run procedure. Client not initialized')

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

        return tuple(output)
Esempio n. 23
0
    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
Esempio n. 24
0
class MySshClient():
    def __init__(self):
        self.config = configparser.ConfigParser()
        self.filename = os.path.join(os.path.dirname(__file__), args.filename).replace("\\", "/")
        self.config.read(self.filename)
        self.ssh_client = SSHClient()
        self.shell = None


    # 此函数用于输入用户名密码登录主机
    def ssh_login(self):
        try:
            # 设置允许连接known_hosts文件中的主机(默认连接不在known_hosts文件中的主机会拒绝连接抛出SSHException)
            self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
            self.ssh_client.connect(hostname=self.config.get('ssh', 'host'), port=self.config.get('ssh', 'port'),
                                    username=self.config.get('ssh', 'username'),
                                    password=self.config.get('ssh', 'password'))


        except AuthenticationException:
            logging.warning('username or password error')
            return False
        except NoValidConnectionsError:
            logging.warning('connect time out')
            return False
        except:
            logging.warning('unknow error')
            print("Unexpected error:", sys.exc_info()[0])
            return False
        return True

    # 此函数用于执行command参数中的命令并打印命令执行结果
    def execute_some_command(self,command):
        try:
            stdin, stdout, stderr = self.ssh_client.exec_command(command)
            readlines = stdout.readlines()
            str1 = stdout.read().decode()
            for line in readlines:
                print(line)
        except Exception as e:
            logging.error(str(e))


    def multi_run_comment(self, command_list):
        if not self.shell:
            self.shell = self.ssh_client.invoke_shell()
            try:
                for cmd in command_list:
                    print("do cmd", cmd)
                    self.shell.send(cmd + '\n')
                    time.sleep(0.8)
                    recved_buff = self.shell.recv(1024)
                    print('recved_buff', recved_buff)
            except Exception as e:
                logging.error(str(e))

    # 此函数用于退出登录
    def ssh_logout(self):
        logging.warning('will exit host')
        self.ssh_client.close()
Esempio n. 25
0
class MySshClient():
    def __init__(self):
        self.ssh_client = SSHClient()

    # 此函数用于输入用户名密码登录主机
    def ssh_login(self, host_ip, username, password):
        try:
            # 设置允许连接known_hosts文件中的主机(默认连接不在known_hosts文件中的主机会拒绝连接抛出SSHException)
            self.ssh_client.set_missing_host_key_policy(AutoAddPolicy())
            self.ssh_client.connect(host_ip, port=22, username=username, password=password)
        except AuthenticationException:
            logging.warning('username or password error')
            return 1001
        except NoValidConnectionsError:
            logging.warning('connect time out')
            return 1002
        except:
            logging.warning('unknow error')
            print("Unexpected error:", sys.exc_info()[0])
            return 1003
        return 1000

    # 此函数用于执行command参数中的命令并打印命令执行结果
    def execute_some_command(self, command):
        stdin, stdout, stderr = self.ssh_client.exec_command(command)
        res=stdout.read().decode()
        print(res)
        return res

    # 此函数用于退出登录
    def ssh_logout(self):
        logging.warning('will exit host')
        self.ssh_client.close()
Esempio n. 26
0
def unsafe_connect():
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy)
    client.connect("example.com")

    # ... interaction with server

    client.close()
Esempio n. 27
0
 def _remote(self):
     client = SSHClient()
     client.load_system_host_keys()
     client.connect(hostname=self.host,
                    port=self.port,
                    username=self.username,
                    password=self.password)
     return client
Esempio n. 28
0
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
Esempio n. 29
0
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
Esempio n. 30
0
 def verify(self):
     client = SSHClient()
     client.set_missing_host_key_policy(self)
     client.connect(
         self.host,
         username=self.username,
         password=self.password,
     )
Esempio n. 31
0
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
Esempio n. 32
0
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())
Esempio n. 33
0
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)
Esempio n. 34
0
def timecheck(h):
	cmd = "date +%s"
	s = SSHClient()
	print "> %s" % (h,)
	s.set_missing_host_key_policy(AutoAddPolicy())
	s.connect(hostname=h[1], username=h[0])
	(_in, _out, _err) = s.exec_command( cmd, bufsize=4096)
	print "< %s" % (h,)
	return (h,_out.read())
Esempio n. 35
0
 def _exec_ssh_cmd(self, cmdline):
     client = SSHClient()
     client.set_missing_host_key_policy(AutoAddPolicy())
     client.connect(self.droplet_ip, username='******',
                    key_filename=self.ssh_key_path)
     stdin, stdout, stderr = client.exec_command(cmdline)
     for line in stdout:
         logging.info(line)
     for line in stderr:
         logging.info(line)
Esempio n. 36
0
 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)
Esempio n. 37
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
    retry = 30
    error = None
    while retry > 0:
        try:
            stdin, stdout, stderr = ssh.exec_command(
                "PYTHONPATH=~/.local/ easy_install-2.6 --install-dir=~/.local -U pip"
            )
            retry = 0
        except ssh_exception.AuthenticationException as e:
            error = e
            sleep(5)
            retry -= 1

    if retry == 0 and error is not None:
        logs.write(error)

    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()
Esempio n. 38
0
 def run(self, project):
     if not self.vm.start_VM():
         return -1, ''
     if not os.path.exists(os.path.join(project.tempdir, project.target)):
         raise FileNotFoundError('Error: Executable file has not been created!')
     copy_to_vm = [os.path.join(project.tempdir, project.target)]
     copy_from_vm = ['CUnitAutomated-Results.xml']
     print('Connecting to remote machine...')
     client = SSHClient()
     client.set_missing_host_key_policy(AutoAddPolicy())
     client.load_system_host_keys()
     client.connect(self.host, username=self.username, password=self.password, timeout=10)
     return_code = 0
     data = ''
     with client.open_sftp() as sftp:
         try:
             self.rmtree(sftp, self.remote_path)
         except FileNotFoundError:
             pass
         try:
             sftp.mkdir(self.remote_path)
         except OSError:
             pass
         for f in copy_to_vm:
             remote_file = os.path.join(self.remote_path, os.path.basename(f))
             sftp.put(f, remote_file)
             sftp.chmod(remote_file, 0o777)
             stdin, stdout, stderr = client.exec_command('cd {}; timeout {}s {}'.format(self.remote_path, self.timeout, remote_file))
             return_code = stdout.channel.recv_exit_status()
             print('[Remote] Error code: {}'.format(return_code))
             stdout_string = '[Remote] ' + ''.join(stdout)
             if stdout_string:
                 print('[Remote] STDOUT:')
                 print(stdout_string)
             stderr_string = '[Remote] ' + ''.join(stderr)
             if stderr_string:
                 print('[Remote] STDERR:')
                 print(stderr_string)
         for f in copy_from_vm:
             # get all result files
             remote_file = os.path.join(self.remote_path, os.path.basename(f))
             try:
                 with tempfile.TemporaryFile() as local_file:
                     sftp.getfo(remote_file, local_file)
                     local_file.seek(0)
                     data = local_file.read()
             except FileNotFoundError:
                 print('Remote file not found!')
         # delete all files in home directory
         self.rmtree(sftp, self.remote_path)
     client.close()
     if self.shutdown_vm_after:
         self.vm.stop_VM()
     return return_code, data
Esempio n. 39
0
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
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
Esempio n. 41
0
File: dash.py Progetto: 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
Esempio n. 42
0
 def _exec_ssh_cmd(self, cmdline):
     pkey_buf = StringIO(self.state.pkey)
     client = SSHClient()
     client.set_missing_host_key_policy(AutoAddPolicy())
     client.connect(self.droplet_ip, username='******',
                    pkey=RSAKey.from_private_key(pkey_buf))
     stdin, stdout, stderr = client.exec_command(cmdline)
     for line in stdout:
         logging.info(line)
     for line in stderr:
         logging.info(line)
Esempio n. 43
0
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()
Esempio n. 44
0
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
Esempio n. 45
0
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()
Esempio n. 46
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
Esempio n. 47
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)
Esempio n. 48
0
	def recvr(h):
		s = SSHClient()
		print "> %s" % (h,)
		s.set_missing_host_key_policy(AutoAddPolicy())
		s.connect(hostname=h[1], username=h[0])	
		(_in, _out, _err) = s.exec_command(client_cmd, bufsize=4096)
		_in.write(bytecode)
		_in.flush()
		print "< %s" % (h,)
		err = _err.read()
		if(err):
			print err
		return (h,_out.read())
Esempio n. 49
0
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
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()
Esempio n. 51
0
class SshOps:
    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"))

    def runCommand(self,command):
        stdin,stdout,stderr = self.client.exec_command(command)
        if stderr.channel.recv_exit_status() != 0:
            return {"status":1,"message":stderr.read()}
        else:
            return {"status":0,"message":stdout.read()}
Esempio n. 52
0
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()