Esempio n. 1
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()
    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. 3
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. 4
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. 5
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
Esempio n. 6
0
def __power_off_linux():
    cfg = SSHConfig.from_path(Path.home() / ".ssh" / "config")\
                    .lookup(SSH_INSTANCE)
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(
        **{
            "hostname": cfg.get("hostname"),
            "port": cfg.get("port") or 22,
            "username": cfg.get("user"),
            "password": cfg.get("password"),
            "key_filename": cfg.get("identityfile"),
        })
    ssh.exec_command("sudo poweroff")
Esempio n. 7
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. 8
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. 9
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. 10
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. 11
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. 12
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
Esempio n. 13
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. 14
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})
Esempio n. 15
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
Esempio n. 16
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. 17
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. 18
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()
Esempio n. 19
0
    def exec_ssh_client_command(self, ssh_client: SSHClient, command: str) -> Tuple[int, bytes, bytes]:
        self.log.info("Running command: %s", command)

        # set timeout taken as params
        stdin, stdout, stderr = ssh_client.exec_command(
            command=command,
            get_pty=self.get_pty,
            timeout=self.timeout,
            environment=self.environment,
        )
        # get channels
        channel = stdout.channel

        # closing stdin
        stdin.close()
        channel.shutdown_write()

        agg_stdout = b''
        agg_stderr = b''

        # capture any initial output in case channel is closed already
        stdout_buffer_length = len(stdout.channel.in_buffer)

        if stdout_buffer_length > 0:
            agg_stdout += stdout.channel.recv(stdout_buffer_length)

        # read from both stdout and stderr
        while not channel.closed or channel.recv_ready() or channel.recv_stderr_ready():
            readq, _, _ = select([channel], [], [], self.cmd_timeout)
            for recv in readq:
                if recv.recv_ready():
                    line = stdout.channel.recv(len(recv.in_buffer))
                    agg_stdout += line
                    self.log.info(line.decode('utf-8', 'replace').strip('\n'))
                if recv.recv_stderr_ready():
                    line = stderr.channel.recv_stderr(len(recv.in_stderr_buffer))
                    agg_stderr += line
                    self.log.warning(line.decode('utf-8', 'replace').strip('\n'))
            if (
                stdout.channel.exit_status_ready()
                and not stderr.channel.recv_stderr_ready()
                and not stdout.channel.recv_ready()
            ):
                stdout.channel.shutdown_read()
                try:
                    stdout.channel.close()
                except Exception:
                    # there is a race that when shutdown_read has been called and when
                    # you try to close the connection, the socket is already closed
                    # We should ignore such errors (but we should log them with warning)
                    self.log.warning("Ignoring exception on close", exc_info=True)
                break

        stdout.close()
        stderr.close()

        exit_status = stdout.channel.recv_exit_status()

        return exit_status, agg_stdout, agg_stderr
def setup_ssh_connection(ip, key, user='******', port=22, timeout=10):
    """
    Construct an ssh connection to the VM instance.
    """
    print "Attempting to setup ssh connection to %s@%s:%d" % (user, ip, port)
    if key is None:
        raise Exception("setup_ssh_connection: You didn't supply a key!")

    try:
        if os.getenv("BOCK_TEST_DEBUG", 0) > 0:
            paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
        client = SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(ip, port, user, pkey=key, timeout=timeout)
        print "SSH connection successful"
    except SSHException as e:
        print "Failed to ssh to %s (%s)" % (ip, e)
        raise
    # Check if this is a new ubuntu cloud image that discourages the use
    # of the root account
    cmd = "echo YES"
    stdin, stdout, stderr = client.exec_command(cmd)
    stdout = stdout.readline()
    if stdout != "YES\n":
        print "Test of ssh connection failed"
        print "Got: %s" %  stdout

        if stdout == 'Please login as the user "ubuntu" rather than the user "root".\n':
            print "New style ubuntu image found, using user ubuntu"
            client = SSHClient()
            client.set_missing_host_key_policy(paramiko.WarningPolicy())
            client.connect(ip, port, "ubuntu", pkey=key, timeout=timeout)
            print "SSH connection successful"
            cmd = "echo YES"
            stdin, stdout, stderr = client.exec_command(cmd)
            stdout = stdout.readline()
            if stdout != "YES\n":
                print "Re-test of ssh connection failed"
                print "Got: %s" % stdout
                raise Exception("SSH connection test failed")
            print "SSH connection using user 'ubuntu' successful"
            return client
        else:
            raise Exception("SSH connection test failed")
    return client
Esempio n. 21
0
def setup_ssh_connection(ip, key, user='******', port=22, timeout=10):
    """
    Construct an ssh connection to the VM instance.
    """
    print "Attempting to setup ssh connection to %s@%s:%d" % (user, ip, port)
    if key is None:
        raise Exception("setup_ssh_connection: You didn't supply a key!")

    try:
        if os.getenv("BOCK_TEST_DEBUG", 0) > 0:
            paramiko.common.logging.basicConfig(level=paramiko.common.DEBUG)
        client = SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(ip, port, user, pkey=key, timeout=timeout)
        print "SSH connection successful"
    except SSHException as e:
        print "Failed to ssh to %s (%s)" % (ip, e)
        raise
    # Check if this is a new ubuntu cloud image that discourages the use
    # of the root account
    cmd = "echo YES"
    stdin, stdout, stderr = client.exec_command(cmd)
    stdout = stdout.readline()
    if stdout != "YES\n":
        print "Test of ssh connection failed"
        print "Got: %s" % stdout

        if stdout == 'Please login as the user "ubuntu" rather than the user "root".\n':
            print "New style ubuntu image found, using user ubuntu"
            client = SSHClient()
            client.set_missing_host_key_policy(paramiko.WarningPolicy())
            client.connect(ip, port, "ubuntu", pkey=key, timeout=timeout)
            print "SSH connection successful"
            cmd = "echo YES"
            stdin, stdout, stderr = client.exec_command(cmd)
            stdout = stdout.readline()
            if stdout != "YES\n":
                print "Re-test of ssh connection failed"
                print "Got: %s" % stdout
                raise Exception("SSH connection test failed")
            print "SSH connection using user 'ubuntu' successful"
            return client
        else:
            raise Exception("SSH connection test failed")
    return client
Esempio n. 22
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. 23
0
def execute_cmd(ip_addr, cmd):
    client = SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(AutoAddPolicy())
    client.connect(ip_addr,
      username=dtacq_un,
      password=dtacq_pw)
    _, aout, aerr = client.exec_command(cmd)
    return aout.read(), aerr.read()
Esempio n. 24
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. 25
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. 26
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. 27
0
    def _test_server(self, server, floating_ip):
        if server.status != 'ACTIVE':
            return Skipped()
        result = Success()
        ip_address = floating_ip.ip_address

        client = SSHClient()
        client.set_missing_host_key_policy(AutoAddPolicy())
        try:
            client.connect(ip_address,
                           username='******',
                           timeout=CONF.ssh_timeout)
        except Exception as e:
            return Failure("Failed to ssh to server.", exception=e)

        stdin, stdout, stderr = client.exec_command('hostname -f')
        stderr_output = stderr.read()
        if stderr_output:
            self.log.error('stderr: %r', stderr_output)
        remote_hostname = stdout.read().strip().split('.', 1)[0]

        expected_hostname = ('sanity-%s' % server.metadata['host_id']).split(
            '.', 1)[0]
        if remote_hostname != expected_hostname:
            return Failure("Hostname mismatch the servers %s hostname is %s" %
                           (expected_hostname, remote_hostname))

        stdin, stdout, stderr = client.exec_command(
            'ping -c 5 %s' % self._state.get('external_test_ip', '8.8.8.8'))
        stdout = stdout.read().strip()
        match = self.ping_re.search(stdout)
        if not match:
            return Failure("Host has no external connectivity.", output=stdout)
        else:
            result = match.groupdict()
            if int(result['received']) == 0:
                return Failure("Host has no external connectivity.",
                               output=stdout)
            else:
                return Success(result=result)

        return result
Esempio n. 28
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. 29
0
def connect_to_ssh(addy):
    username = input("Username: ")
    password = getpass.getpass()
    ''' do a session and get info'''
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy)
    client.connect(str(addy), 22, username=username, password=password)
    output = client.exec_command('show configuration|no-more')
    config = output[1].readlines()
    #Works for juniper... don't print it, save to file after diff
    compare_config(config, addy)
Esempio n. 30
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. 31
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()
Esempio n. 32
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()
Esempio n. 33
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. 34
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. 35
0
class SSHOps:
    def __init__(self):
        self.client = SSHClient()
        self.client.load_system_host_keys()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.client.connect("192.168.0.2", username='******', password='******')

    def runCommand(self, comando):
        stdin, stdout, stderr = self.client.exec_command(comando)
        if stderr.channel.recv_exit_status() != 0:
            return {"status": 1, "message": stderr.read()}
        else:
            return {"status": 0, "message": stdout.read()}
Esempio n. 36
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. 37
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()
Esempio n. 38
0
 def get_bmc_ip(self):
     try:
         client = SSHClient()
         client.set_missing_host_key_policy(IgnoreHostKeyPolicy)
         client.connect(
             self._wedge_local_addr,
             username=self.username,
             password=self.password,
         )
         _, stdout, _ = client.exec_command("ip -o -4 addr show",
                                            timeout=60)
         return (stdout.read().decode().splitlines()[1].split()[3].split(
             "/")[0])
     except Exception:
         return None
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. 40
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. 41
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
Esempio n. 42
0
class SshOps:
    def __init__(self):
        self.ssh = SSHClient()
        self.ssh.load_system_host_keys()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(hostname='127.0.0.1',
                         username='******',
                         password='******')

    def runCommand(self, command):
        stdin, stdout, stderr = self.ssh.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. 43
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()
Esempio n. 44
0
def executar_comando(servidor,senha):	
	ssh = SSHClient()
	ssh.load_system_host_keys()
	ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())


	try:
		ssh.connect("localhost",username="******",password=senha)
		stdin, stdout, stderr = ssh.exec_command("ls -la")	
		if stderr.channel_recv_exit_status() != 0:
			print stderr.read()
		else:
			print stdout.read()

	except Exception as e:
			
		print "A conexao falhou"
Esempio n. 45
0
def executar_comando(usuario, senha):
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())


    try:
        ssh.connect('localhost', username =usuario, password=senha)
        stdin, stdout, stderr = ssh.exec_command('ls -la')
        if stderr.channel.recv_exit_status() != 0:
            print stderr.read()
        else:
            print stdout.read()


    except Exception as e:
        print 'Falhou ao conectar: ', e
Esempio n. 46
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
Esempio n. 47
0
class SSH:
    def __init__(self):
        self.servidor = "192.168.0.2"
        self.ssh = SSHClient()
        self.ssh.load_system_host_keys()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(self.servidor)

    def executarComandoRemoto(self,com):    
        try:
            stdin,stdout,stderr = self.ssh.exec_command(com)
            if stderr.channel.recv_exit_status() != 0:
                return stderr.read()
            else:
                return stdout.read()
        except Exception as e:
            print "Nao conseguiu conectar ao servidor %s"%e
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()
Esempio n. 49
0
def run_command(address, command):

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

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

            connection.connect(address, username='******',
                                key_filename=keys)
            break
        except Exception:
            log.warn('Unable to establish an SSH connection')
            log.debug('Retrying in 10 seconds')
            time.sleep(10)

    log.debug('Established an SSH connection')
    log.debug('Running {command} on {address}'.format(command=command,
                                                        address=address))

    stdin, stdout, stderr = connection.exec_command(command)

    try:
        stdin = stdin.read()
    except IOError:
        stdin = None

    try:
        stdout = stdout.read()
    except IOError:
        stdout = None

    try:
        stderr = stderr.read()
    except IOError:
        stderr = None

    log.debug('STDIN: {stdin}'.format(stdin = stdin))
    log.debug('STDOUT: {stdout}'.format(stdout = stdout))
    log.debug('STDERR: {stderr}'.format(stderr = stderr))

    return stdout
Esempio n. 50
0
    def connect(self, instance, tag_ssh_user, tag_ssh_port, cmd):
        """
        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 tag_ssh_user:
        :param tag_ssh_port:
        :param cmd: execute this command if not None
        :return:
        """
        ssh_user = SergentSsh.get_ssh_user(instance, tag_ssh_user)
        ssh_port = SergentSsh.get_ssh_port(instance, tag_ssh_port)

        if self._using_vpn is True:
            ssh_ip = instance.private_ip_address
        else:
            ssh_ip = instance.ip_address

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

        logger.debug('connecting to %s with port %s and user %s', ssh_ip, ssh_port, ssh_user)
        mykey = RSAKey.from_private_key(self._key_file)

        client.connect(hostname=ssh_ip, port=ssh_port, username=ssh_user, pkey=mykey)

        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)
Esempio n. 51
0
#!/usr/bin/python
from task_constants import *

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
Esempio n. 52
0
from paramiko.client import SSHClient
from paramiko.client import AutoAddPolicy
client=SSHClient()
autoAddpolicy=AutoAddPolicy()
client.set_missing_host_key_policy(autoAddpolicy)
client.load_system_host_keys()
hosts=['192.168.100.5','192.168.100.2','192.168.100.3','192.168.100.4','192.168.100.6']
for host in hosts:
	client.connect(host,port=22,username='******',password='******',look_for_keys=False, allow_agent=False)
	
	stdin, stdout, stderr = client.exec_command('launcher stop')
Esempio n. 53
0
#!/usr/bin/python

from paramiko.client import SSHClient
import paramiko
from datetime import datetime

hosts = ["192.168.1.10","192.168.1.106","192.168.1.100"]

ssh = SSHClient()
# ler as chaves
ssh.load_system_host_keys()
# se eh um server que nao conheco fazer add
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

for h in hosts:
	try:
		print "Conectando no servidor %s na data %s"%(h,datetime.now())
		ssh.connect(h)
		stdin,stdout,stderr = ssh.exec_command('w')
		if stderr.channel.recv_exit_status() != 0:
			print stderr.read()
		else:
			print stdout.read()
		print "Saindo do servidor %s na data %s"%(h,datetime.now())
	except Exception as e:
		print "Nao conseguiu conectar ao servidor: %s"%e
Esempio n. 54
0
#!/usr/bin/python

from paramiko.client import SSHClient
import paramiko

ssh = SSHClient()
# ler as chaves
ssh.load_system_host_keys()
# se eh um server que nao conheco fazer add
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("192.168.1.106")
stdin,stdout,stderr = ssh.exec_command('ls -la')

if stderr.channel.recv_exit_status() != 0:
	print stderr.read()
else:
	print stdout.read()
Esempio n. 55
0
			gen = generators[ext]
		except KeyError:
			_die("Generator for extension %s not found!" % ext)
		
		with open(name, 'w') as f:
			f.write(gen(conf))
		sys.exit(0)
	elif arg == "install":
		from paramiko.client import SSHClient
	
		assert len(sys.argv) != 4, "Invalid number of args!"
		local, remote = sys.argv[2:3]
		assert os.path.isfile(local), "%s must be readable file!" % local
		
		client = SSHClient()
		sftp = None
		try:
			client.connect(**ssh_conf)
			client.exec_command(ACQUIRE_COMMAND)
			
			sftp = client.open_sftp()
			sftp.chdir(ROOT_DIR)
			sftp.put(local, '.' + remote)
		finally:
			if sftp is not None:
				sftp.close()
			client.exec_command(RELEASE_COMMAND)
			client.close()
	else:
		_die("Unknown command: %s" % arg)
Esempio n. 56
0
def ssh(hostname, username, password, cmd):
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy())
    client.connect(hostname, username=username, password=password, timeout=3)
    stdin, stdout, stderr = client.exec_command(cmd)
    return stdin, stdout, stderr
Esempio n. 57
0
from paramiko.client import SSHClient
from paramiko.client import AutoAddPolicy
client=SSHClient()
autoAddpolicy=AutoAddPolicy()
client.set_missing_host_key_policy(autoAddpolicy)
client.load_system_host_keys()
hosts=['192.168.100.2','192.168.100.3','192.168.100.4','192.168.100.6']
for host in hosts:
	client.connect(host,port=22,username='******',password='******',look_for_keys=False, allow_agent=False)
	stdin, stdout, stderr = client.exec_command('su cassandra -l -c "/opt/cassandra/bin/cassandra"')
Esempio n. 58
0
# -*- coding:utf-8 -*-

# BEFORE START!!!!!!
# Create file named "config.py"
# set there the HOST_NAME, USER_NAME and PASSWORD variables

import config
from paramiko import AutoAddPolicy
from paramiko.client import SSHClient

client = SSHClient()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(hostname=config.HOST_NAME, username=config.USER_NAME, password=config.PASSWORD)

# Передача файлов
sftp = client.open_sftp()
if sftp:
	sftp.put('hw.txt', '~')

stdin, stdout, stderr = client.exec_command('pkill -u u48649 -f django-wrapper.fcgi')

for line in stdout:
    print line.strip('\n')
    
client.close()