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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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()
Ejemplo n.º 4
0
 def report(self):
     '''
     Get instance and connection info for all enabled projects.
     '''
     client = SSHClient()
     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     client.connect(env['OS_SSH_HOST'], username=env['OS_SSH_USERNAME'], password=env['OS_SSH_PASSWORD'])
     for project in self.project_list:
         if not eval(project[2]) or project[1] in ('admin', 'services'):
             continue
         ttys = []
         stdin, stdout, stderr = client.exec_command('source /etc/nova/openrc && nova list --tenant %s'%project[0])            
         output = stdout.read()
         serverlist = filter(lambda x: x.count(project[0]), output.splitlines()).__len__() 
         stdin, stdout, stderr = client.exec_command('source /etc/nova/openrc && neutron floatingip-list --tenant-id %s -F floating_ip_address'%project[0])
         floatingips = re.findall('\d+\.\d+\.\d+\.\d+', stdout.read())
         print "====INFO====", project[1], project[2], project[0], serverlist, floatingips
         ttys = []
         for ip in floatingips:
             if not self.ping_ip_reachable(ip):
                 continue
             sshp = SshconnectionProcess(ip)
             sshp.run()
             ttys += sshp.return_result()
         self.report_result.append({project[1].encode('ascii'): \
                                 {'instance':serverlist, 'connection':ttys.__len__(), 'enable': eval(project[2]), \
                                  'tenant_id':project[0].encode('ascii'), 'floatingips':floatingips}})
     client.close()
Ejemplo n.º 5
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")
Ejemplo n.º 6
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
Ejemplo n.º 7
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()
Ejemplo n.º 8
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()
Ejemplo n.º 9
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()
Ejemplo n.º 10
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()
Ejemplo n.º 11
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())
Ejemplo n.º 12
0
class PRISMASSHClient:
    def __init__(self, address, user=None, passwd=None):
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(AutoAddPolicy())
        self.client.load_system_host_keys()
        if user is None:
            self.client.connect(address)
        else:
            if passwd is not None:
                self.client.connect(address, username=user, password=passwd)
            else:
                self.client.connect(address, username=user)

    def close(self):
        self.client.close()

    def list_from_directory(self, directory):
        stdin, stdout, stderr = self.client.exec_command("ls " + directory)
        result = stdout.read().splitlines()
        return result

    def download_file(self, remote_filepath, local_filepath):
        sftp = self.client.open_sftp()
        sftp.get(remote_filepath, local_filepath)
        sftp.close()

    def size_of_file(self, remote_filepath):
        sftp = self.client.open_sftp()
        stat = sftp.stat(remote_filepath)
        sftp.close()
        return stat.st_size
Ejemplo n.º 13
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})
Ejemplo n.º 14
0
def unsafe_connect():
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy)
    client.connect("example.com")

    # ... interaction with server

    client.close()
Ejemplo n.º 15
0
class TransferAgent(object):

    def __init__(self):
        self.client = SSHClient()
        self.client.load_system_host_keys()

        self.sftp_client = None

    def load_host_keys(self, filepath):
        self.client.load_host_keys(filepath)

    def connect(self, host, username=None, port=22, private_key_file=None):
        self.client.connect(host, username=username, port=port,
                                  key_filename=private_key_file)
        self.sftp_client = self.client.open_sftp()
        return self

    def close(self):
        self.sftp_client.close()
        self.client.close()

    def dest_file_exists(self, destpath):
        try:
            self.sftp_client.stat(destpath)
        except:
            return False

        return True

    def transfer(self, srcpath, destdir, callback=None, randomize=True,
                 name_length=7, max_attempts=4):
        if not randomize:
            destpath = self.transform_path(srcpath, destdir)
            return self.do_transfer(srcpath, destpath, callback=callback)

        for _ in range(max_attempts):
            name = generate_random_name(name_length)
            destpath = self.transform_path(srcpath, destdir, rename=name)

            if self.dest_file_exists(destpath):
                continue

            return self.do_transfer(srcpath, destpath, callback=callback)

        raise TransferError("Exceeded max transfer attempts")

    def do_transfer(self, src, dest, callback=None):
        self.sftp_client.put(src, dest, callback=callback)
        return dest

    def transform_path(self, srcpath, destdir, rename=None):
        basename = os.path.basename(srcpath)
        _, ext = os.path.splitext(basename)

        if rename:
            basename = rename + ext

        return posixpath.join(destdir, basename)
Ejemplo n.º 16
0
def ssh_client(host):
    client = SSHClient()
    # client.load_system_host_keys()
    client.set_missing_host_key_policy(WarningPolicy)
    try:
        client.connect(host, username="******")
        yield client
    finally:
        client.close()
Ejemplo n.º 17
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()
Ejemplo n.º 18
0
 def keystone_project_list(self):
     '''
     command: openstack project list, keystone tenant-list(deprecated)
     '''
     client = SSHClient()
     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     client.connect(env['OS_SSH_HOST'], username=env['OS_SSH_USERNAME'], password=env['OS_SSH_PASSWORD'])
     stdin, stdout, stderr = client.exec_command('source /etc/nova/openrc && openstack project list --long')
     output = stdout.read()
     self.project_list = re.findall('(\w{32})\s+\|\s+([\w-]+)\s+\|\s+[\w\t]*\s+\|\s+(True|False)\s+\|', output)
     client.close()
Ejemplo n.º 19
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
Ejemplo n.º 20
0
 def neutront_floating_ip_list(self):
     '''
     command: neutron floatingip-list [--show-details], neutron floatingip-show FLOATINGIP_ID
     '''
     client = SSHClient()
     client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
     client.connect(env['OS_SSH_HOST'], username=env['OS_SSH_USERNAME'], password=env['OS_SSH_PASSWORD'])
     stdin, stdout, stderr = client.exec_command('source /etc/nova/openrc && neutron floatingip-list')
     output = stdout.read()
     self.floating_ips = re.findall('([\w-]+)\s+\|\s+([\d.]+)\s+\|\s+([\d.]+)\s+\|\s+([\w-]+)', output)
     client.close()
Ejemplo n.º 21
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()
def place_code_on_workers():
    client = SSHClient()
    client.load_system_host_keys()
    for worker in worker_list.keys():
        client.connect(worker, username=USERNAME)
        scp = SCPClient(client.get_transport())
        scp.put(['worker.py'], remote_path='/home/{}/'.format(USERNAME))
        print("{color}[{}] {}{end_color}".format(
            worker, "Copied worker.py",
            color=worker_list[worker]['text_color'],
            end_color=Colors.ENDC))
        client.close()
Ejemplo n.º 23
0
def ssh_ping(hostname, port):
    ssh_client = SSHClient()
    ssh_client.set_missing_host_key_policy(AutoAddPolicy)
    try:
        ssh_client.connect(hostname,
                           port=port,
                           username='******',
                           pkey=RSAKey.from_private_key(
                               StringIO(Setting.ssh_private_key)))
    except AuthenticationException:
        return False
    ssh_client.close()
    return True
Ejemplo n.º 24
0
    def fetch_moosefs_drive_content(self, remote_host_ip: str) -> dict:
        # A sample result dictionary
        # result_dict = {
        # 'files': ['temp_file101', 'temp_file102'],
        # 'cotent': [file 1 content here', 'file 2 content here' ]
        # }

        try:
            result_dict = dict()
            result_dict['files'] = list()
            result_dict['content'] = list()

            mfsClientVM = SSHClient()
            mfsClientVM.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            mfsClientVM.load_system_host_keys()

            mfsClientVM.connect(hostname=remote_host_ip,
                                username=self.remote_host_username)

            print("Fetching file and its content on VM with IP: " +
                  remote_host_ip)

            # Fetch file name on client VM
            stdin, stdout, stderr = mfsClientVM.exec_command(
                'cd /mnt/mfs/; ls')
            outlines = stdout.readlines()
            stdin.close()
            file_name = ''.join(outlines)
            file_name = str(file_name).rstrip('\n')
            result_dict['files'].append(file_name)
            print('File name is: ' + file_name)

            # Fetch file content on client VM
            stdin, stdout, stderr = mfsClientVM.exec_command(
                'cd /mnt/mfs/; cat ' + file_name)
            outlines = stdout.readlines()
            stdin.close()
            file_content = ''.join(outlines)
            file_content = str(file_content).rstrip('\n')
            result_dict['content'].append(file_content)
            print('File Content is: ' + file_content)

            mfsClientVM.close()
            return result_dict
        except Exception as e:
            print(
                "Something went wrong while fetching the moosefs drive content"
            )
            print("Error Details: " + str(e))
            return None
Ejemplo n.º 25
0
def add_public_key(hostname, port, password):
    ssh_client = SSHClient()
    ssh_client.set_missing_host_key_policy(AutoAddPolicy)
    ssh_client.connect(hostname, port=port, username='******', password=password)
    try:
        _, stdout, stderr = ssh_client.exec_command(
            'mkdir -p -m 700 /root/.ssh && \
        echo %r >> /root/.ssh/authorized_keys && \
        chmod 600 /root/.ssh/authorized_keys' % Setting.ssh_public_key)
        if stdout.channel.recv_exit_status() != 0:
            raise Exception('Add public key error: ' +
                            ''.join(x for x in stderr))
    finally:
        ssh_client.close()
def start_training():
    client_list = []
    for worker_name in worker_list.keys():
        client = SSHClient()
        client.load_system_host_keys()
        client.connect(worker_name, username=USERNAME)
        channel = client.get_transport().open_session()
        client_list.append((worker_name, client, channel, {'stdout': "", 'stderr': ""}))
    for worker_name, client, channel, outs in client_list:
        channel.exec_command('python3 worker.py')
    at_least_one_worker_still_running = True
    while at_least_one_worker_still_running:
        at_least_one_worker_still_running = False
        for worker_name, client, channel, outs in client_list:
            rl, wl, xl = select([channel], [], [], 0.0)
            if not channel.exit_status_ready():
                at_least_one_worker_still_running = True
            if len(rl) > 0:
                outs['stdout'] += channel.recv(RECV_BUFFER_SIZE).decode('utf-8')
                outs['stderr'] += channel.recv_stderr(RECV_BUFFER_SIZE).decode('utf-8')
                if channel.exit_status_ready():
                    if len(outs['stdout']) > 0:
                        print("{color}[{}] {}{end_color}".format(
                            worker_name, outs['stdout'],
                            color=worker_list[worker_name]['text_color'],
                            end_color=Colors.ENDC))
                        outs['stdout'] = ""
                    if len(outs['stderr']) > 0:
                        print("{color}[{}] {}{end_color}".format(
                            worker_name, outs['stderr'],
                            color=worker_list[worker_name]['text_color'],
                            end_color=Colors.ENDC))
                        outs['stderr'] = ""
                lines_stdout = outs['stdout'].split("\n")
                outs['stdout'] = lines_stdout[-1]
                lines_stderr = outs['stderr'].split("\n")
                outs['stderr'] = lines_stderr[-1]
                for line in lines_stdout[:-1]:
                    print("{color}[{}] {}{end_color}".format(
                        worker_name, line,
                        color=worker_list[worker_name]['text_color'],
                        end_color=Colors.ENDC))
                for line in lines_stderr[:-1]:
                    print("{color}[{}] {}{end_color}".format(
                        worker_name, line,
                        color=worker_list[worker_name]['warn_color'],
                        end_color=Colors.ENDC))
    for worker_name, client, channel, outs in client_list:
        client.close()
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()
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()
Ejemplo n.º 29
0
def ssh_connection(
    instance: Instance, ssh_credentials: SSHCredentials
) -> Iterator[SSHClient]:
    """Connect to server and yield SSH client."""
    _, _, public_ip, ssh_port, _ = instance
    username, key_filename = ssh_credentials

    client = SSHClient()
    client.set_missing_host_key_policy(IgnoreHostKeyPolicy)
    client.connect(
        hostname=public_ip, port=ssh_port, username=username, key_filename=key_filename
    )

    yield client

    client.close()
Ejemplo n.º 30
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()
Ejemplo n.º 31
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()
Ejemplo n.º 32
0
def _exec_command(command, switch_name=''):
    if not switch_name:
        switch_name = [s for s in current_app.config['switches']][0]
    current_app.logger.info('running on %s: %s' % (switch_name, command))
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy())
    client.connect(current_app.config['switches'][switch_name]['address'],
            username=current_app.config['switches'][switch_name]['user'],
            key_filename=current_app.config['switches'][switch_name]['key'])
    sin, sout, serr = client.exec_command(command)
    output = sout.read().decode('ascii')
    errout = serr.read().decode('ascii')
    client.close()
    if errout or 'Cmd exec error' in output:
        abort(500, "Error executing '%s' on %s" % (command, switch_name))
    current_app.logger.info('output from %s: %s' % (switch_name, output))
    return output, errout
def configure_secondary_cfme(ipaddr, primary_ip, ssh_username, ssh_password, db_password):
    cmd = "appliance_console_cli --hostname %s --dbname vmdb_production" \
          "--fetch-key %s --username root --password %s --sshpassword %s" \
          % (primary_ip, primary_ip, db_password, ssh_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()
Ejemplo n.º 34
0
def ssh_signup(user):
    # TODO: configure nodes used for authentication
    auth_node = next(iter(SSH.AVAILABLE_NODES))

    ssh_key = TensorHiveManager().dedicated_ssh_key
    test_client = SSHClient()
    test_client.load_system_host_keys()
    test_client.set_missing_host_key_policy(WarningPolicy())

    try:
        test_client.connect(auth_node, username=user['username'], pkey=ssh_key)
    except AuthenticationException:
        return {'msg': G['unpriviliged']}, 403
    except (BadHostKeyException, SSHException, socket.error) as e:
        return 'An error ocurred while authenticating: {}'.format(e), 500
    finally:
        test_client.close()

    return do_create(user)
    def verify_moosefs_drive_content(self, remote_host_ip: str) -> list:
        # A sample result dictionary
        # result_dict = {
        # 'files': ['temp_file101', 'temp_file102'],
        # 'cotent': [file 1 content here', 'file 2 content here' ]
        # }

        try:
            result_list = list()

            mfsClientVM = SSHClient()
            mfsClientVM.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            mfsClientVM.load_system_host_keys()

            # mfsClientVM.connect(hostname=remote_host_ip,
            #                     username=self.remote_host_username,
            #                     key_filename='cs6620Key101.pem')

            mfsClientVM.connect(hostname=remote_host_ip,
                                username=self.remote_host_username)

            print("Verifying file content on VM with IP: " + remote_host_ip)

            # Count Bs in file on client VM
            stdin, stdout, stderr = mfsClientVM.exec_command(
                'cd /mnt/mfs/test7; grep -c "B" testfile.txt')
            outlines = stdout.readlines()
            stdin.close()
            count = ''.join(outlines)
            # count = int(count)
            result_list.append(count)
            print('File contains ' + count + ' Bs')

            mfsClientVM.close()
            return result_list

        except Exception as e:
            print(
                "Something went wrong while verifying the moosefs drive content"
            )
            print("Error Details: " + str(e))
            return None
Ejemplo n.º 36
0
    def run_command(self, command_str):
        """ Uses paramiko ssh client to execute `command_str` on this remote Instance.
        Args:
            command_str (str): The remote shell command to execute.
        """
        ssh_client = SSHClient()
        ssh_client.set_missing_host_key_policy(AutoAddPolicy)
        print("Connecting to %s:%i " % (self.ssh_host, self.ssh_port))
        try:
            ssh_client.connect(self.ssh_host,
                               port=int(self.ssh_port),
                               username='******',
                               key_filename=self.client._get_ssh_key_file())
            print("Running command '%s'" % command_str)
            stdin, stdout, stderr = ssh_client.exec_command(command_str)
            print(stdout.read().decode('utf-8'))
            print(stderr.read().decode('utf-8'))

        # except NoValidConnectionsError as err:
        #     raise InstanceError(self.id, err.errors)
        finally:
            ssh_client.close()
Ejemplo n.º 37
0
class RemoteSession():
    def __init__(self, ip, username=REMOTE_USER):
        self.username = username

        self.client = SSHClient()
        self.client.set_missing_host_key_policy(IgnoreHostKeyPolicy)
        self.client.connect(ip, username=self.username)

        self.sftp = self.client.open_sftp()

    def execute(self, command):
        stdin, stdout, stderr = self.client.exec_command(command)

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

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

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

        return

    def download(self, src, dst):
        self.sftp.get(src, dst)
        return

    def close(self):
        self.client.close()
Ejemplo n.º 38
0
 def efact_check_history(self):
     efact = self.env.ref("l10n_es_facturae_efact.efact_backend")
     ICP = self.env["ir.config_parameter"].sudo()
     connection = SSHClient()
     connection.load_system_host_keys()
     connection.connect(
         ICP.get_param("account.invoice.efact.server", default=None),
         port=int(ICP.get_param("account.invoice.efact.port",
                                default=None)),
         username=ICP.get_param("account.invoice.efact.user", default=None),
         password=ICP.get_param("account.invoice.efact.password",
                                default=None),
     )
     sftp = connection.open_sftp()
     path = sftp.normalize(".")
     sftp.chdir(path + statout_path)
     attrs = sftp.listdir_attr(".")
     attrs.sort(key=lambda attr: attr.st_atime)
     to_remove = []
     for attr in attrs:
         file = sftp.open(attr.filename)
         datas = file.read()
         file.close()
         update_record = efact.create_record(
             "l10n_es_facturae_efact_update",
             {
                 "edi_exchange_state": "input_received",
                 "exchange_filename": attr.filename,
             },
         )
         update_record._set_file_content(datas)
         efact.with_delay().exchange_process(update_record)
         to_remove.append(attr.filename)
     for filename in to_remove:
         sftp.remove(filename)
     sftp.close()
     connection.close()
Ejemplo n.º 39
0
class SSH(AbstractFs):

    policy = AutoAddPolicy
    recv_size = 1024

    _ssh = None
    _basepath = "/"

    def __init__(self, host, username="******", basepath=None):
        logger.debug("Creating SSH client")
        self._ssh = SSHClient()
        self._ssh.load_system_host_keys()

        logger.debug("Setting %s for missing host keys", self.policy.__name__)
        self._ssh.set_missing_host_key_policy(self.policy)

        logger.info(
            "Connecting to %s%s%s",
            "{}@".format(username) or "",
            host,
            ":{}".format(basepath) or "",
        )
        self._ssh.connect(host, username=username, compress="true")

        if basepath:
            logger.info("Use basepath %s", basepath)
            self._basepath = basepath

    def _collect_stream(self, channel, recv_fn):
        content = ""
        buffer = None

        while True:
            buffer = recv_fn(self.recv_size)
            content += buffer.decode("utf-8")

            if len(buffer) == 0:
                break

        return content

    def _exec(self, cmd, log_error=True):
        channel = self._ssh.get_transport().open_session()
        channel.exec_command(cmd)

        stdout = self._collect_stream(channel, channel.recv)
        stderr = self._collect_stream(channel, channel.recv_stderr)

        exit_code = channel.recv_exit_status()

        logger.debug(stdout)

        if exit_code != 0 and log_error:
            logger.error(stderr)
            logger.error("Command %s failed with exit code %s", cmd, exit_code)

        return exit_code, stdout, stderr

    def touch(self, filename):
        logger.debug("Touching file %s", filename)
        self._exec('touch "{}"'.format(filename))

    def rename(self, remote_src, remote_dest):
        logger.debug("Renaming %s into %s", remote_src, remote_dest)

        exit_code, _, _ = self._exec('mv "{}" "{}"'.format(remote_src, remote_dest))

        if exit_code != 0:
            raise SSHError("Failed renaming {} into {}".format(remote_src, remote_dest))

    def exists(self, path):
        logger.debug("Checking if %s exists...", path)
        exit_code, _, _ = self._exec('test -f "{}"'.format(path), log_error=False)
        exists = exit_code == 0

        logger.debug("File %s does%s exists.", path, "" if exists else " not")
        return exists

    def copy(self, remote_src, local_dst):
        exit_code, remote_content, _ = self._exec('cat "{}"'.format(remote_src))

        if exit_code != 0:
            raise SSHError("Cannot read from %s", remote_src)

        with open(local_dst, "w") as f:
            f.write(remote_content)

    # def remove(self, path):
    #     return os.remove(path)

    def symlink(self, remote_src, remote_dest, relative_to=None):
        if relative_to:
            remote_src = remote_src.replace(relative_to, ".")

        logger.debug("Symlinking %s to %s", remote_src, remote_dest)

        exit_code, _, _ = self._exec('ln -sf "{}" "{}"'.format(remote_src, remote_dest))

        if exit_code != 0:
            raise SSHError("Failed symlinking {} to {}".format(remote_src, remote_dest))

    def glob(self, remote_path, regex_str):
        logger.debug("Globbing %s for %s", remote_path, regex_str)

        exit_code, remote_content, _ = self._exec('ls "{}"'.format(remote_path))

        if exit_code != 0:
            raise SSHError("Failed listing {}".format(remote_path))

        regex = re.compile(regex_str)

        entries = (e for e in remote_content.split() if regex.match(e))
        entries = (os.path.join(remote_path, e) for e in entries)

        return entries

    def rmtree(self, remote_path):
        logger.debug("Removing tree %s", remote_path)

        exit_code, _, _ = self._exec('rm -rf "{}"'.format(remote_path))

        if exit_code != 0:
            raise SSHError("Failed removing {}".format(remote_path))

    def close(self):
        logger.debug("Closing SSH connection...")
        self._ssh.close()

        logger.debug("Connection closed.")
Ejemplo n.º 40
0
class SshClusterConnection(AbstractConnection):
    def __init__(self, girder_token, cluster):
        self._girder_token = girder_token
        self._cluster = cluster

    def _load_rsa_key(self, path, passphrase):
        return RSAKey.from_private_key_file(path, password=passphrase)

    def __enter__(self):
        self._client = SSHClient()
        self._client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

        username = parse('config.ssh.user').find(self._cluster)[0].value
        hostname = parse('config.host').find(self._cluster)[0].value
        passphrase \
            = parse('config.ssh.passphrase').find(self._cluster)
        if passphrase:
            passphrase = passphrase[0].value
        else:
            passphrase = None

        key_name = parse('config.ssh.key').find(self._cluster)[0].value
        key_path = os.path.join(cumulus.config.ssh.keyStore,
                                key_name)

        private_key = self._load_rsa_key(key_path, passphrase)

        self._client.connect(hostname=hostname, username=username,
                             pkey=private_key)

        return self

    def __exit__(self, type, value, traceback):
        self._client.close()

    def execute(self, command, ignore_exit_status=False, source_profile=True):
        if source_profile:
            command = 'source /etc/profile && %s' % command

        chan = self._client.get_transport().open_session()
        chan.exec_command(command)
        stdout = chan.makefile('r', -1)
        stderr = chan.makefile_stderr('r', -1)

        output = stdout.readlines() + stderr.readlines()
        exit_code = chan.recv_exit_status()
        if ignore_exit_status and exit_code != 0:
            raise SshCommandException(command, exit_code, output)

        return output

    @contextmanager
    def get(self, remote_path):
        sftp = None
        file = None
        try:
            sftp = self._client.get_transport().open_sftp_client()
            file = sftp.open(remote_path)
            yield file
        finally:
            if file:
                file.close()
                sftp.close()

    def isfile(self, remote_path):

        with self._client.get_transport().open_sftp_client() as sftp:
            try:
                s = sftp.stat(remote_path)
            except IOError:
                return False

            return stat.S_ISDIR(s.st_mode)

    def mkdir(self, remote_path, ignore_failure=False):
        with self._client.get_transport().open_sftp_client() as sftp:
            try:
                sftp.mkdir(remote_path)
            except IOError:
                if not ignore_failure:
                    raise

    def makedirs(self, remote_path):
        with self._client.get_transport().open_sftp_client() as sftp:
            current_path = ''
            if remote_path[0] == '/':
                current_path = '/'

            for path in remote_path.split('/'):
                if not path:
                    continue
                current_path = os.path.join(current_path, path)
                try:
                    sftp.listdir(current_path)
                except IOError:
                    sftp.mkdir(current_path)

    def put(self, stream, remote_path):
        with self._client.get_transport().open_sftp_client() as sftp:
            sftp.putfo(stream, remote_path)

    def stat(self, remote_path):
        with self._client.get_transport().open_sftp_client() as sftp:
            return sftp.stat(remote_path)

    def remove(self, remote_path):
        with self._client.get_transport().open_sftp_client() as sftp:
            return sftp.remove(remote_path)

    def list(self, remote_path):
        with self._client.get_transport().open_sftp_client() as sftp:
            for path in sftp.listdir_iter(remote_path):
                yield {
                    'name': path.filename,
                    'user': path.st_uid,
                    'group': path.st_gid,
                    'mode': path.st_mode,
                    # For now just pass mtime through
                    'date': path.st_mtime,
                    'size': path.st_size
                }
Ejemplo n.º 41
0
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()
Ejemplo n.º 42
0
def main():
    user_config_file = path.join(path.expanduser('~'), '.fernglas.cfg')
    project_config_file = path.join(getcwd(), '.fernglas.cfg')
    config = SafeConfigParser()
    config.read([user_config_file, project_config_file])
    servers = {}
    if not config.has_section('main'):
        print("No main config found")
        exit(1)
    if not config.has_option('main', 'servers'):
        print("No servers defined")
        exit(2)
    server_sections = config.get('main', 'servers').split(',')
    server_sections = [section.strip() for section in server_sections]
    for server in server_sections:
        servers[server] = dict(config.items(server))
        if 'port' in servers[server]:
            servers[server]['port'] = int(servers[server]['port'])

    issue = sys.argv[1]

    package = run_setup(path.join(getcwd(), 'setup.py'))
    package_name = package.get_name()

    repo = Repo(getcwd())
    latest_issue_commit = repo.git.log(all=True, grep=issue, n=1, format='%H')
    if not latest_issue_commit:
        print("No commits found for " + issue)
        exit(3)

    config = SSHConfig()
    ssh_config_path = path.expanduser('~/.ssh/config')
    if path.exists(ssh_config_path):
        try:
            config.parse(open(ssh_config_path))
        except Exception as e:
            print("Could not parse ssh config: " + str(e))

    client = SSHClient()
    client.load_system_host_keys()
    # XXX support ecdsa?
    client.set_missing_host_key_policy(AutoAddPolicy())

    for key, server in servers.items():
        host_config = config.lookup(server['hostname'])
        connect_opts = {}
        for key_ssh, key_paramiko in SSH_CONFIG_MAPPING.items():
            if key_ssh in host_config:
                connect_opts[key_paramiko] = host_config[key_ssh]
        connect_opts.update(dict(
            (opt, servers[key][opt]) for opt in SSH_OPTIONS
            if opt in servers[key]))
        client.connect(**connect_opts)
        stdin, stdout, stderr = client.exec_command('grep {0} {1}'.format(
            package_name, server['versions-path']))
        deployed_version = stdout.read().strip().replace(package_name, '')\
            .replace('=', '').strip()
        version_tags = [tag for tag in repo.tags
                        if tag.name == deployed_version]
        tag = version_tags[0]
        version_commit = tag.commit.hexsha
        status = repo.is_ancestor(latest_issue_commit, version_commit)
        print("{0} is {2}deployed to {1}".format(
            issue, key, (not status) and 'not ' or ''))
    client.close()
Ejemplo n.º 43
0
class SFTPClient(object):
    """Dynamic extension on paramiko's SFTPClient."""

    MAX_PACKET_SIZE = SFTPFile.__dict__['MAX_REQUEST_SIZE']

    ssh_client = None
    client = None
    raise_exceptions = False
    original_arguments = {}
    debug = False

    _log = logging.getLogger(LOG_NAME)
    _dircache = []

    def __init__(self, **kwargs):
        """Constructor."""
        self.original_arguments = kwargs.copy()
        self._connect(**kwargs)

    def __enter__(self):
        """For use with a with statement."""
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """For use with a with statement."""
        self.close_all()

    def _connect(self, **kwargs):
        kwargs_to_paramiko = dict(
            look_for_keys=kwargs.pop('look_for_keys', True),
            username=kwargs.pop('username'),
            port=kwargs.pop('port', 22),
            allow_agent=False,
            timeout=kwargs.pop('timeout', None),
        )
        host = kwargs.pop('hostname', 'localhost')
        password = kwargs.pop('password')
        keepalive = kwargs.pop('keepalive', 5)
        if password:
            kwargs_to_paramiko['password'] = password
        self.raise_exceptions = kwargs.pop('raise_exceptions', False)

        self.ssh_client = SSHClient()
        self.ssh_client.load_system_host_keys()
        self.ssh_client.connect(host, **kwargs_to_paramiko)

        self.client = self.ssh_client.open_sftp()
        channel = self.client.get_channel()
        channel.settimeout(kwargs_to_paramiko['timeout'])
        channel.get_transport().set_keepalive(keepalive)

        # 'Extend' the SFTPClient class
        is_reconnect = kwargs.pop('is_reconnect', False)
        members = inspect.getmembers(self.client,
                                     predicate=inspect.ismethod)
        self._log.debug('Dynamically adding methods from original SFTPClient')
        for (method_name, method) in members:
            if method_name[0:2] == '__' or method_name == '_log':
                self._log.debug('Ignorning {}()'.format(method_name))
                continue

            if not is_reconnect and hasattr(self, method_name):
                raise AttributeError('Not overwriting property "{}". This '
                                     'version of Paramiko is not '
                                     'supported.'.format(method_name))

            self._log.debug('Adding method {}()'.format(method_name))
            setattr(self, method_name, method)

    def close_all(self):
        """Close client and SSH client handles."""
        self.client.close()
        self.ssh_client.close()

    def clear_directory_cache(self):
        """Reset directory cache."""
        self._dircache = []

    def listdir_attr_recurse(self, path='.'):
        """List directory attributes recursively."""
        for da in self.client.listdir_attr(path=path):
            is_dir = da.st_mode & 0o700 == 0o700
            if is_dir:
                try:
                    for x in self.listdir_attr_recurse(
                            path_join(path, da.filename)):
                        yield x
                except IOError as e:
                    if self.raise_exceptions:
                        raise e
            else:
                yield (path_join(path, da.filename), da,)

    def _get_callback(self, start_time, _log):
        def cb(tx_bytes, total_bytes):
            total_time = datetime.now() - start_time
            total_time = total_time.total_seconds()
            total_time_s = floor(total_time)

            if (total_time_s % LOG_INTERVAL) != 0:
                return

            nsize_tx = naturalsize(tx_bytes,
                                   binary=True,
                                   format='%.2f')
            nsize_total = naturalsize(total_bytes,
                                      binary=True,
                                      format='%.2f')

            speed_in_s = tx_bytes / total_time
            speed_in_s = naturalsize(speed_in_s,
                                     binary=True,
                                     format='%.2f')

            _log.info('Downloaded {} / {} in {} ({}/s)'.format(
                nsize_tx,
                nsize_total,
                naturaldelta(datetime.now() - start_time),
                speed_in_s,
                total_time_s))

        return cb

    def mirror(self,
               path='.',
               destroot='.',
               keep_modes=True,
               keep_times=True,
               resume=True):
        """
        Mirror a remote directory to a local location.

        path is the remote directory. destroot must be the location where
        destroot/path will be created (the path must not already exist).

        keep_modes and keep_times are boolean to ensure permissions and time
        are retained respectively.

        Pass resume=False to disable file resumption.
        """
        n = 0
        resume_seek = None
        cwd = self.getcwd()

        for _path, info in self.listdir_attr_recurse(path=path):
            if info.st_mode & 0o700 == 0o700:
                continue

            dest_path = path_join(destroot, dirname(_path))
            dest = path_join(dest_path, basename(_path))

            if dest_path not in self._dircache:
                try:
                    makedirs(dest_path)
                except OSError:
                    pass
                self._dircache.append(dest_path)

            if isdir(dest):
                continue

            try:
                with open(dest, 'rb'):
                    current_size = os.stat(dest).st_size

                    if current_size != info.st_size:
                        resume_seek = current_size
                        if resume:
                            self._log.info('Resuming file {} at {} '
                                           'bytes'.format(dest, current_size))
                        raise IOError()  # ugly goto
            except IOError:
                while True:
                    try:
                        # Only size is used to determine complete-ness here
                        # Hash verification is in the util module
                        if resume_seek and resume:
                            read_tuples = []

                            n_reads = ceil((info.st_size - resume_seek) /
                                           self.MAX_PACKET_SIZE) - 1
                            n_left = ((info.st_size - resume_seek) %
                                      self.MAX_PACKET_SIZE)
                            offset = 0

                            for n in range(n_reads):
                                read_tuples.append((resume_seek + offset,
                                                    self.MAX_PACKET_SIZE,))
                                offset += self.MAX_PACKET_SIZE
                            read_tuples.append((resume_seek + offset, n_left,))

                            with self.client.open(_path) as rf:
                                with open(dest, 'ab') as f:
                                    f.seek(resume_seek)
                                    resume_seek = None

                                    for chunk in rf.readv(read_tuples):
                                        f.write(chunk)
                        else:
                            dest = realpath(dest)
                            self._log.info('Downloading {} -> '
                                           '{}'.format(_path, dest))

                            start_time = datetime.now()
                            self.client.get(_path, dest)

                            self._get_callback(start_time, self._log)(
                                info.st_size, info.st_size)

                        # Do not count files that were already downloaded
                        n += 1

                        break
                    except (socket.timeout, SFTPError) as e:
                        # Resume at position - 10 bytes
                        resume_seek = os.stat(dest).st_size - 10
                        if isinstance(e, socket.timeout):
                            self._log.error('Connection timed out')
                        else:
                            self._log.error('{!s}'.format(e))

                        if resume:
                            self._log.info('Resuming GET {} at {} '
                                           'bytes'.format(_path,
                                                          resume_seek))
                        else:
                            self._log.debug('Not resuming (resume = {}, '
                                            'exception: {})'.format(resume,
                                                                    e))
                            raise e

                        self._log.debug('Re-establishing connection')
                        self.original_arguments['is_reconnect'] = True
                        self._connect(**self.original_arguments)
                        if cwd:
                            self.chdir(cwd)

            # Okay to fix existing files even if they are already downloaded
            try:
                if keep_modes:
                    chmod(dest, info.st_mode)
                if keep_times:
                    utime(dest, (info.st_atime, info.st_mtime,))
            except IOError:
                pass

        return n

    def __str__(self):
        """Return string representation."""
        return '{} (wrapped by {}.SFTPClient)'.format(
            str(self.client), __name__)
    __unicode__ = __str__
Ejemplo n.º 44
0
from paramiko.client import SSHClient

client = SSHClient()
client.load_system_host_keys()
client.connect(hostname = opt_server,
               username = opt_username,
               key_filename = opt_keyfile)
output = client.exec_command('ls')
print output

from scp import SCPClient
scp = SCPClient(client.get_transport())
scp.put("ssh.py", "ssh.py")
scp.put("scp.py", "scp.py")
print client.exec_command('md5sum ssh.py scp.py')
client.close()

"""
  The UUID of a task will serve as the filename for all the files associated with the task.
  As well as the handle for marking/handling the task.

  A task has these states
    - TODO (we should verify that the file has been generated, if not, alert)
        copy the file on WEB01 to OPT01
        mark the task as GOING_TO_RUN
        submit the job as qsub (a shell script with param)
            (qsub should be able to mark something as READY_TO_MAIL

    - READY_TO_MAIL
        copy the file from OPT01 to WEB01
        send the email out
    ftp.mkd(".local")
except ftplib.error_perm:
    pass
try:
    ftp.mkd("kinto")
except ftplib.error_perm:
    pass
try:
    ftp.storbinary("STOR kinto/kinto.ini", StringIO(config))
except ftplib.error_perm:
    print("A kinto config already exist.")
else:
    print("A kinto config 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 cliquet[postgresql]')
print(stdout.read(), stderr.read())
ssh.close()