Ejemplo n.º 1
0
 def job(self, device, _):
     path_backup = Path.cwd() / 'logs' / 'job_logs'
     now = strip_all(str(datetime.now()))
     path_dir = path_backup / f'logs_{now}'
     source = path_backup / f'logs_{now}.tgz'
     makedirs(path_dir)
     for job in fetch_all('Job'):
         with open(path_dir / f'{job.name}.json', 'w') as log_file:
             dump(job.logs, log_file)
     with open_tar(source, 'w:gz') as tar:
         tar.add(path_dir, arcname='/')
     ssh_client = SSHClient()
     ssh_client.set_missing_host_key_policy(AutoAddPolicy())
     ssh_client.connect(device.ip_address,
                        username=device.username,
                        password=device.password,
                        look_for_keys=False)
     destination = f'{self.destination_path}/logs_{now}.tgz'
     self.transfer_file(ssh_client, source, destination)
     ssh_client.close()
     if self.delete_folder:
         rmtree(path_dir)
     if self.delete_archive:
         remove(source)
     return {
         'success': True,
         'result': f'logs stored in {destination} ({device.ip_address})'
     }
Ejemplo n.º 2
0
class conn():
  def __init__(self, user, passwd, ip):
    self.user = user
    self.passwd = passwd
    self.ip = ip
    
    self.sshConn = SSHClient()
    self.sshConn.set_missing_host_key_policy(AutoAddPolicy())

    try:
      self.sshConn.connect(self.ip, port = 22, 
        username = self.user,password = self.passwd, 
        timeout = 60, allow_agent = False,look_for_keys = False)
          
      self.status = 'Succeeded'
    except:
      self.status = 'Failed'

  def runCommand(self, command):
    self.stdin, self.stdout, self.stderr = (None,None,None)
    if self.status == 'Succeeded':
      self.stdin, self.stdout, self.stderr = self.sshConn.exec_command(command)
   
  def closeSocket(self):
    self.sshConn.close()
Ejemplo n.º 3
0
def _execute_command_on_node(host, cmd):
    ssh = SSHClient()
    try:
        _setup_ssh_connection(host, ssh)
        return _open_channel_and_execute(ssh, cmd)
    finally:
        ssh.close()
Ejemplo n.º 4
0
def _exec_ssh_command(exec_info_dict, username, ip, password, command, phase):

    ssh = SSHClient()

    ssh.load_system_host_keys()

    ssh.connect(ip, username=username, password=password)

    stdin, stdout, stderr = ssh.exec_command("source ~/.profile && " + command)

    #print(stdout.read()) # blocking

    ### for debugging
    #print(stderr.read()) # blocking
    #exit_status = 0
    ###

    exit_status = stdout.channel.recv_exit_status()  # blocking

    ssh.close()

    workstation_exec_info_dict = exec_info_dict.get(username, {})

    workstation_exec_info_dict[phase] = {
        "command": command,
        "ssh_exit_status": exit_status
    }

    exec_info_dict[username] = workstation_exec_info_dict
Ejemplo n.º 5
0
 def __init__(self, cfg, info, log_fd, log_file):
     super(XBuilderMirrorPlugin, self).__init__(cfg, info, log_fd, log_file)
     self.ssh = self.cfg['mirror'].copy()
     for k, v in self.ssh.items():
         if not v and k != 'pkey':
             raise XUtilsError('[mirror plugin]: mandatory parameter \'%s\' not set.' % k)
     # Test SSH connection
     ssh = SSHClient()
     try:
         ssh.set_missing_host_key_policy(AutoAddPolicy())
         ssh.connect(self.ssh['server'], username=self.ssh['user'], key_filename=self.ssh['pkey'])
     except BadHostKeyException:
         raise XUtilsError('Server host key verification failed.')
     except AuthenticationException:
         raise XUtilsError('Authentication to %s@%s failed.' % (self.ssh['user'], self.ssh['server']))
     except SSHException:
         raise XUtilsError('Unable to establish a SSH connection to %s' % self.ssh['server'])
     finally:
         stdin, stdout, stderr = ssh.exec_command(  # pylint: disable=unused-variable
             'touch %s/foo && rm %s/foo' % (self.ssh['base_dir'], self.ssh['base_dir'])
         )
         if stderr.read():
             raise XUtilsError(
                 'User %s does not have sufficient permission on server %s to create/delete files.' %
                 (self.ssh['user'], self.ssh['server'])
             )
         ssh.close()
Ejemplo n.º 6
0
def oxe_reboot(host, port, password, swinst_password):
    """Reboot OXE Call Server
    
    Args:
        host (TYPE): Description
        port (TYPE): Description
        password (TYPE): Description
        swinst_password (TYPE): Description
    """
    # connect OXE through SSH and execute 'rainbowagent -v'
    client = SSHClient()  # use the paramiko SSHClient
    client.set_missing_host_key_policy(
        AutoAddPolicy())  # automatically add SSH key
    try:
        client.connect(host, port, username='******', password=password)
    except AuthenticationException:
        print('*** Failed to connect to {}:{}'.format(host, port))
    channel = client.invoke_shell()
    while channel.recv_ready() is False:
        sleep(3)  # OXE is really slow on mtcl connexion
    stdout = channel.recv(4096)
    channel.send('reboot\n')
    while channel.recv_ready() is False:
        sleep(1)
    stdout += channel.recv(1024)
    channel.send(swinst_password + '\n')
    while channel.recv_ready() is False:
        sleep(0.5)
    stdout += channel.recv(1024)
    # pprint.pprint(stdout)
    channel.close()
    client.close()
Ejemplo n.º 7
0
class SSHConnection:
    def __init__(self, controller, ip, uName, uPass):
        try:
            self.ssh = SSHClient()
            self.ssh.load_system_host_keys()
            self.ssh.set_missing_host_key_policy\
            (paramiko.AutoAddPolicy())
            self.ssh.connect(hostname=ip, port=22, \
             username=uName, password=uPass, timeout=2)
        except Exception as ex:
            controller.error(ex)
#Function to get files with SCP.

    def fileGet(self, controller, srcDir, dstDir):
        try:
            self.scp = SCPClient(self.ssh.get_transport())
            self.scp.get(srcDir, dstDir)
            self.scp.close()
            self.ssh.close()
        except Exception as ex:
            controller.error(ex)


#Function to put files with SCP.

    def filePut(self, controller, srcDir, dstDir):
        try:
            self.scp = SCPClient(self.ssh.get_transport())
            self.scp.put(srcDir, dstDir)
            self.scp.close()
            self.ssh.close()
        except Exception as ex:
            controller.error(ex)
Ejemplo n.º 8
0
def main():
    commands = {"deps": deps, "remote-version": remote_version, "push": push, "clean": clean,
                "versions": versions, "list": list_available, "stage": stage, "info": child_information, "history": child_history, "help": print_help}
    sys.argv.pop(0) #remove filename

    command = None
    if len(sys.argv) > 0:
        command = sys.argv.pop(0)
    if command is None or command not in commands:
        command = "help"

    try:
        if command=="help":
            client = None
        else:
            print "Setting up remote repo connection..."
            client = SSHClient()
            client.load_system_host_keys()
            client.connect(REPOSITORY_HOST, username=REPOSITORY_USER, key_filename=REPOSITORY_KEY)
            print "Connected to repo"

        try:
            commands[command](client, sys.argv)
            if command != "help":
                print ""
                print command + " [SUCCESSFUL]"

        except Exception, e:
            print e
            print command + " [FAILED]"

    finally:
        if client is not None:
            client.close()
Ejemplo n.º 9
0
 def job(self, payload: dict, device: Device) -> dict:
     path_backup = Path.cwd() / "logs" / "job_logs"
     now = strip_all(str(datetime.now()))
     path_dir = path_backup / f"logs_{now}"
     source = path_backup / f"logs_{now}.tgz"
     makedirs(path_dir)
     for job in fetch_all("Job"):
         with open(path_dir / f"{job.name}.json", "w") as log_file:
             dump(job.logs, log_file)
     with open_tar(source, "w:gz") as tar:
         tar.add(path_dir, arcname="/")
     ssh_client = SSHClient()
     ssh_client.set_missing_host_key_policy(AutoAddPolicy())
     ssh_client.connect(
         device.ip_address,
         username=device.username,
         password=device.password,
         look_for_keys=False,
     )
     destination = f"{self.destination_path}/logs_{now}.tgz"
     self.transfer_file(ssh_client, source, destination)
     ssh_client.close()
     if self.delete_folder:
         rmtree(path_dir)
     if self.delete_archive:
         remove(source)
     return {
         "success": True,
         "result": f"logs stored in {destination} ({device.ip_address})",
     }
Ejemplo n.º 10
0
    def host_connect(self, host):
        # Connect
        selector = self.imported_config['sites'][self.site]['hosts'][host]
        ip = selector['ip_address']
        username = selector['connect_user']
        secret = input('Password:'******' '
        stdin, stdout, stderr = [' ', ' ', ' ']

        while cli_input != 'ezcli exit':
            cli_input = input('ezcli(' + str(host) + '):')
            stdin, stdout, stderr = client.exec_command(cli_input)
            print(stdout.read().decode("utf8"))

        # Print output of command. Will wait for command to finish.

        # Get return code from command (0 is default for success)

        # Because they are file objects, they need to be closed
        stdin.close()
        stdout.close()
        stderr.close()

        # Close the client itself
        client.close()
Ejemplo n.º 11
0
def sshtungetip():
    """
   Подключается по sftp к серверу получает данные JSON. Осуществляет поиск IP и возращает его в качестве параметра в
   место вызова.
    """
    global setstatus
    global login
    setstatus = "Получение ip адреса"
    from paramiko import SSHClient
    from paramiko import AutoAddPolicy
    from paramiko import RSAKey
    getipsftp = SSHClient()
    pk = RSAKey.from_private_key_file('srv.key')
    getipsftp.load_system_host_keys()
    getipsftp.set_missing_host_key_policy(AutoAddPolicy())
    getipsftp.connect(hostname=publicipadress[0],
                      port=str(publicipadress[1]),
                      username='******',
                      pkey=pk)
    sftp = getipsftp.open_sftp()
    import json
    fip = None
    with sftp.file('ip-client.json', 'r') as f:
        dataip = json.load(f)
    getipsftp.close()
    for x in dataip:
        if login == x["User"]["login"] or login in x["User"]["FullName"]:
            fip = x["User"]["ipaddress"]["ip"][0]
            fullname = x["User"]["login"]
    if fip == "Не найдено" or fip is None:
        fip = "IP не найден"
    time.sleep(1)
    return (fip)
Ejemplo n.º 12
0
class SshService:
    def __init__(self):
        self._client = SSHClient()

    def login(self, username="******", hostname='laptop'):
        self._client.load_system_host_keys()
        self._client.connect(hostname=hostname, username=username)

    def exit(self):
        self._client.close()

    def exec(self, command) -> str:
        stdin, stdout, stderr = types(self._client.exec_command(command))
        self._handle_error(stderr)
        return self._handle_out(stdout)

    def sudo_exec(self, command) -> str:
        stdin, stdout, stderr = types(self._client.exec_command(f"sudo -S {command}"))
        if stdin.writable():
            stdin.write(os.getenv('sudoPass') + '\n')
            stdin.flush()
        self._handle_error(stderr)
        return self._handle_out(stdout)

    def _handle_error(self, stderr):
        if stderr.readable():
            err = stderr.read()
            if len(err) > 0 and not err.startswith(b'[sudo] password for '):
                raise Exception(err)

    def _handle_out(self, stdout):
        if stdout.readable():
            return stdout.read().decode()
        else:
            return None
class ssh_remote_command():
    def __init__(self):
        self.sshclient = SSHClient()

    def issue_command(self, host, command):
        combinded_ouput = []
        try:
            self.sshclient.set_missing_host_key_policy(
                paramiko.AutoAddPolicy())
            key_path = os.path.expanduser("~/.ssh/authorized_keys")
            self.sshclient.connect(host,
                                   username="******",
                                   key_filename=key_path)
            stdin, stdout, stderr = self.sshclient.exec_command(command)

            output = stdout.readlines()

            for line in output:
                combinded_ouput.append(line.strip())

            newstuff = ''.join(combinded_ouput)
            self.sshclient.close()
            brandnewstuff = ast.literal_eval(newstuff)
            return brandnewstuff

        except Exception as e:
            self.sshclient.close()
            logger.warn("Connection Failed: %s" % e)
Ejemplo n.º 14
0
 def job(self, payload: dict, device: Device) -> dict:
     now = strip_all(str(datetime.now()))
     source = Path.cwd() / "migrations" / f"backup_{now}.tgz"
     migrate_export(
         Path.cwd(),
         {
             "import_export_types": list(import_properties),
             "name": f"backup_{now}"
         },
     )
     with open_tar(source, "w:gz") as tar:
         tar.add(Path.cwd() / "migrations" / f"backup_{now}", arcname="/")
     ssh_client = SSHClient()
     ssh_client.set_missing_host_key_policy(AutoAddPolicy())
     ssh_client.connect(
         device.ip_address,
         username=device.username,
         password=device.password,
         look_for_keys=False,
     )
     destination = f"{self.destination_path}/backup_{now}.tgz"
     self.transfer_file(ssh_client, source, destination)
     ssh_client.close()
     if self.delete_folder:
         rmtree(Path.cwd() / "migrations" / f"backup_{now}")
     if self.delete_archive:
         remove(source)
     return {
         "success": True,
         "result": f"backup stored in {destination} ({device.ip_address})",
     }
Ejemplo n.º 15
0
def ssh_cli_command(ipadd, user, pwd, namafile):
    client = SSHClient()
    client.set_missing_host_key_policy(AutoAddPolicy())
    try:
        client.connect(ipadd, username=user, password=pwd)
        remote_shell = client.invoke_shell()

    except AuthenticationException:
        print("Authentication failed, please verify your credentials !")
        return False
    except SSHException as sshException:
        print("Unable to establish SSH connection: %s" % sshException)
        return False
    except socket.error:
        print("Unable to connect to host " + ipadd)
        return False

    try:
        f = open(namafile, "r")
    except:
        print("File " + namafile + " is not found !")
        return False

    for line in f:
        if (len(line) > 1):
            remote_shell.send(line.strip() + "\n")
            while not remote_shell.recv_ready():
                time.sleep(.1)
            output = remote_shell.recv(10000)
            print(output.decode("ascii"))

    f.close()

    client.close()
    return True
Ejemplo n.º 16
0
def oxe_get_oxe_version(host, port, password):
    """Summary
    
    Args:
        host (TYPE): Description
        port (TYPE): Description
        password (TYPE): Description
    
    Returns:
        TYPE: Description
    """
    # connect OXE through SSH and execute 'rainbowagent -v'
    client = SSHClient()  # use the paramiko SSHClient
    client.set_missing_host_key_policy(
        AutoAddPolicy())  # automatically add SSH key
    try:
        client.connect(host, port, username='******', password=password)
    except AuthenticationException:
        print('*** Failed to connect to {}:{}'.format(host, port))
    command = 'siteid'
    stdin, stdout, stderr = client.exec_command(command)
    tmp = stdout.readlines()

    # pprint.pprint(tmp)
    # todo test string patch static/dynamic
    major = tmp[2].split()[4].upper()
    static = tmp[3].split()[3]
    dynamic = tmp[4].split()[4]
    version = {'OXE version': major + '.' + static + dynamic}
    pprint(version)
    client.close()
    return version
Ejemplo n.º 17
0
def get(nodes_file, request):
    """Return the logs"""
    args = dict(request.args.items())
    try:
        container = args.pop('container')
    except KeyError:
        raise errors.BadRequest('No container on URL arguments')
    results = dict()

    with open(nodes_file) as f:
        nodes = yaml.load(f)
        try:
            rsa_key_file = nodes.pop('rsa_key')
            pkey = RSAKey.from_private_key_file(rsa_key_file)
        except Exception as e:
            print('Failed to read RSA Key, {} {}'.format(type(e), e))
            raise
        ssh = SSHClient()
        ssh.set_missing_host_key_policy(AutoAddPolicy)
        cmd = 'docker logs {container} {args}'.format(
            container=container,
            args=' '.join(['--{}={}'.format(a, args[a]) for a in args]))

        for _, ips in nodes.items():
            for ip in ips:
                ssh.connect(hostname=ip, username='******', pkey=pkey)
                print('ssh root@{ip} {cmd}'.format(ip=ip, cmd=cmd))
                _in, _out, _err = ssh.exec_command(cmd)
                status = _out.channel.recv_exit_status()
                results[ip] = dict(status=status,
                                   stdout=_out.read(),
                                   stderr=_err.read())
        ssh.close()
    return results
Ejemplo n.º 18
0
Archivo: utils.py Proyecto: jlpcri/mdta
 def _read_remote_results(self, retry_attempts=3):
     client = SSHClient()
     client.set_missing_host_key_policy(AutoAddPolicy())
     client.load_system_host_keys()
     client.connect(self.remote_server,
                    username=self.remote_user,
                    password=self.remote_password)
     command = "grep {0} /var/mdta/report/CallReport.log".format(
         self.filename)
     for i in range(15):
         print("Attempt {0}".format(i))
         stdin, stdout, stderr = client.exec_command(command)
         result_line = stdout.read()
         print(result_line)
         if result_line:
             break
         time.sleep(0.25)
     client.close()
     try:
         result_fields = result_line.decode('utf-8').split(",")
         result = {
             'result': result_fields[-4],
             'reason': result_fields[-2],
             'call_id': result_fields[2]
         }
     except IndexError:
         address_in_use = self._check_call_start_failure()
         if address_in_use:
             if retry_attempts >= 1:
                 time.sleep(6)
                 print("Retries left: {0}".format(retry_attempts - 1))
                 self._invoke_remote_hat()
                 result = self._read_remote_results(
                     retry_attempts=retry_attempts - 1)
             else:
                 result = {
                     'result': 'FAIL',
                     'reason': 'Unable to place call, port in use',
                     'call_id': '0'
                 }
         elif not result_line:
             result = {
                 'result': 'FAIL',
                 'reason':
                 'No results for this test case were found in the logs.',
                 'call_id': '0'
             }
         else:
             result = {
                 'result': 'FAIL',
                 'reason': 'Failed to read results: ' + str(result_line),
                 'call_id': '0'
             }
     except Exception as e:
         result = {
             'result': 'FAIL',
             'reason': 'An untrapped error occurred: ' + str(e.args),
             'call_id': '0'
         }
     return result
Ejemplo n.º 19
0
 def start_monitor(self):
     local_name = platform.node()
     print "Starting Cassandra Monitor for %s" % self.server
     while True:
         try:
             ssh = SSHClient()
             ssh.load_system_host_keys()
             ssh.set_missing_host_key_policy(AutoAddPolicy())
             ssh.connect(self.server, timeout=5, allow_agent=False)
             stdin, stdout, stderr = ssh.exec_command(CASS_CMD)
             stdin.close()
             for line in stdout:
                 # Any line that shows up will be a downed server
                 # server datacenter rack status state load owns token
                 downed_node = line.split()
                 raise DownedNodeException(downed_node[0])
             stdout.close()
             err = stderr.read()
             if err:
                 raise Exception("Unknown error: %s" % str)
             stderr.close()
             ssh.close()
         except DownedNodeException as e:
             self.mark_error(e.node, e)
         except SSHException as e:
             self.mark_error(self.server, "%s could not connect to %s: %s" % (local_name, self.server, e))
         except Exception as e:
             self.mark_error(local_name, "Unknown error: %s" % e)
         else:
             self.clear_error()
         time.sleep(INTERVAL)
Ejemplo n.º 20
0
 def _exec(self):
     exitcode = 0
     client = SSHClient()
     client.load_system_host_keys()
     client.set_missing_host_key_policy(WarningPolicy())
     try:
         client.connect(self.config.get('hostname'),
                        int(self.config.get('port', 22)),
                        key_filename=self.identity_file,
                        username=self.config.get('user'))
         stdout, stderr, channel = self.exec_command(client)
         Printer(self.host, stdout, stderr).loop()
         if channel:
             exitcode = channel.recv_exit_status()
     except IOError as e:
         print(colored('{0}: {1}'.format(self.host, str(e)), 'red'))
         exitcode = 1
     except (BadHostKeyException, AuthException, SSHException) as e:
         print(colored('{0}: {1}'.format(self.host, e.message)), 'red')
         exitcode = 1
     except Exception as e:
         print(colored('{0}: {1}'.format(self.host, e.message)), 'red')
         exitcode = 1
     finally:
         client.close()
         return exitcode
Ejemplo n.º 21
0
 def __validate_creds__(self):
     try:
         usernames=[]
         passwords=[]
         if not self.username in usernames:
             usernames.insert[0](self.username)
         if not self.password in passwords:
             passwords.insert[0](self.password)
         uname=iter(usernames)
         pword=iter(passwords)
         self.username=uname.next()
         self.password=pword.next()
         ssh_cli=SSHClient()
         ssh_cli.set_missing_host_key_policy(AutoAdd())
         ssh_cli.connect(self.address,port=22,username=self.username,password=self.password)
     except paramiko.AuthenticationException:
         ssh_cli.close()
         try:
             self.password=pword.next()
         except StopIteration:
             try:
                 self.username=uname.next()
             else:
                 pword=iter(passwords)
                 self.password=pword.next()         
Ejemplo n.º 22
0
def main():
    commands = {"deps": deps, "remote-version": remote_version, "push": push, "clean": clean,
                "versions": versions, "list": list_available, "stage": stage, "info": child_information, "history": child_history, "help": print_help}
    sys.argv.pop(0) #remove filename

    command = None
    if len(sys.argv) > 0:
        command = sys.argv.pop(0)
    if command is None or command not in commands:
        command = "help"

    try:
        if command=="help":
            client = None
        else:
            client = SSHClient()
            client.load_system_host_keys()
            client.connect(REPOSITORY_HOST, username=REPOSITORY_USER, key_filename=REPOSITORY_KEY)

        try:
            commands[command](client, sys.argv)
            if command != "help":
                print ""
                print command + " [SUCCESSFUL]"

        except Exception, e:
            print e
            print command + " [FAILED]"

    finally:
        if client is not None:
            client.close()
Ejemplo n.º 23
0
def run_script(targets):
    print("[+] Running install script...")
    for target in targets:
        client = SSHClient()
        client.load_host_keys(HOSTS)
        client.connect(
            target,
            username=USER,
            password=PASS,
        )

        run_script = "bash /home/wisp/install_vmtools.sh"
        stdin, stdout, stderr = client.exec_command(run_script)

        # Print any output or errors as they arise
        if stdout: print(stdout.read().decode("utf-8"))
        if stderr: print(stderr.read().decode("utf-8"))

        # Get recturn code
        print(f"\nReturn Code: {stdout.channel.recv_exit_status()}")

        # Close all file objects
        stdin.close()
        stdout.close()
        stderr.close()

        client.close()

        print(f"[+] Process for host {target} completed!")
Ejemplo n.º 24
0
class MySSH:
    def __init__(self, host, password, user):
        self.host = host
        self.password = password
        self.user = user
        try:
            self.client = SSHClient()
            # 我们第一次连接时会去核实host它会去找known_host_key的这个文件,没有按如下处理
            self.client.set_missing_host_key_policy(AcceptPolicy())
            # 我们hostkey不在时的规定:set policy to use when connecting to servers without a known_host_key,然后我们另起炉灶
            self.client.connect(hostname=host,
                                username=user,
                                password=password,
                                timeout=10)
            log.debug(f'{user}成功登录{host}主机')
        except Exception as e:
            print(e)
            log.debug(f'{user}登录{host}主机失败:原因{e}')
            self.client = None

    def send_cmd(self, cmd):
        """发送命令取得返回值"""
        try:
            stdin, stdout, stderr = self.client.exec_command(cmd)
            log.debug(f'{self.user}登录{self.host}执行了{cmd}命令')
            print(stdout, stderr)
            return stdout
        except SSHException as e:
            log.debug(f'{self.user}登录{self.host}执行了{cmd}命令未成功,原因:{e}')
            print(e)

    def close(self):
        """关闭连接"""
        self.client.close()
        log.debug(f'{self.user}登录的{self.host}主机断开连接')
Ejemplo n.º 25
0
    def _ssh(self, script_file, parameters):
        """
        Copies a templated script to the EC2 instance and yields the SSH
        session.
        """

        import os.path
        from paramiko import SSHClient
        from paramiko.client import AutoAddPolicy

        this_file_dir = os.path.dirname(__file__)
        with open(os.path.join(this_file_dir, script_file)) as fh:
            script = fh.read() % parameters

        response = self.ec2.describe_instances(InstanceIds=[self.instance_id])
        instance = response['Reservations'][0]['Instances'][0]
        ip = instance['PublicIpAddress']

        ssh = SSHClient()
        # TODO: is there any way to get the host key via AWS APIs?
        ssh.set_missing_host_key_policy(AutoAddPolicy)
        ssh.connect(ip, username='******', pkey=self.key, look_for_keys=False)

        try:
            with ssh.open_sftp() as ftp:
                with ftp.file(script_file, 'w') as fh:
                    fh.write(script)

                yield ssh

        except Exception as e:
            raise RuntimeError('ERROR: %s (on ubuntu@%s)' % (e, ip))

        finally:
            ssh.close()
Ejemplo n.º 26
0
def scp_file(filepath):
    try:
        from paramiko import SSHClient, AutoAddPolicy
        from scp import SCPClient
    except ImportError:
        print(
            "Import Error! Please install paramiko and scp packages if not available:\n"
            "$ conda install paramiko\n"
            "$ pip install scp\n")
        sys.exit(1)  # another way: `raise SystemExit`

    print("Transfering files...")
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    # ssh.load_system_host_keys()
    ssh.connect(hostname="prada-research.net",
                username="******",
                password="******")

    # SCPCLient takes a paramiko transport as its only argument
    scp_client = SCPClient(ssh.get_transport())
    # scp_client.get("/var/www/html/demo/datasets/" + os.path.basename(filepath))
    scp_client.put(filepath,
                   "/var/www/html/demo/datasets/" + os.path.basename(filepath))
    scp_client.close()
    ssh.close()
    print("Finished transfering.")
Ejemplo n.º 27
0
class SSH:
    def __init__(self, **kwargs):
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(AutoAddPolicy())
        self.kwargs = kwargs

    def __enter__(self):
        kw = self.kwargs
        try:
            self.client.connect(
                hostname=kw.get('hostname'),
                port=int(kw.get('port', 22)),
                username=kw.get('username'),
                password=kw.get('password'),
            )
        except AuthenticationException:
            print("Authentication failed, please verify your credentials")
        except SSHException as sshException:
            print(f"Could not establish SSH connection {sshException}")
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.client.close()

    def exec_cmd(self, cmd):
        stdin, stdout, stderr = self.client.exec_command(cmd)
        data = stdout.read()
        data = data.decode()

        return data
Ejemplo n.º 28
0
class ssh:
    def __init__(self, url, port, username, password):
        self.__ssh = SSHClient()
        self.__ssh.load_system_host_keys()
        self.__ssh.set_missing_host_key_policy(AutoAddPolicy())
        try:
            self.__ssh.connect(url,
                               port=port,
                               username=username,
                               password=password)
        except AuthenticationException:
            return False

    # Run ssh command
    def exec(self, command):
        (stdin, stdout, stderr) = self.__ssh.exec_command(command)
        return True if stdout.channel.recv_exit_status() == 0 else False

    # Put file via scp
    def put(self, source, dest):
        with SCPClient(self.__ssh.get_transport()) as scp:
            scp.put(source, dest)
        return True

    # Close connection
    def close(self):
        self.__ssh.close()
Ejemplo n.º 29
0
class Connection(object):
    def __init__(self,
                 hostname="localhost",
                 port=1247,
                 username="******",
                 password="******"):
        self.hostname = hostname
        self.port = port
        self.username = username
        self.password = password
        self.con = False
        self._conn_()
        return

    def _conn_(self):
        """ Create Conn """
        self.ssh = SSHClient()
        self.ssh.load_system_host_keys()
        self.ssh.connect(hostname=self.hostname,
                         port=self.port,
                         username=self.username,
                         password=self.password)
        self.scp = SCPClient(self.ssh.get_transport())
        self.con = True
        return

    def _close_(self):
        """ Close connection """
        if self.con:
            self.scp.close()
            self.ssh.close()
        return
Ejemplo n.º 30
0
class SshArchiver(Archiver):
    """
    Archives artifacts using ssh.
    """

    def start(self):
        """
        Opens the ssh connection.
        """
        self.ssh_client = SSHClient()
        self.ssh_client.set_missing_host_key_policy(WarningPolicy())
        self.ssh_client.connect(
            self.target.host,
            pkey=self.target.ssh_credentials.get_pkey())
        self.sftp_client = SFTPClient.from_transport(
            self.ssh_client.get_transport())

    def end(self):
        """
        Closes the ssh connection.
        """
        self.ssh_client.close()

    def archive_artifact(self, artifact_url, destination):
        """
        Uploads the artifact_url to the destination on
        the remote server, underneath the target's basedir.
        """
        destination = os.path.join(self.target.basedir, destination)
        _, stdout, _ = self.ssh_client.exec_command(
            "mkdir -p `dirname %s`" % destination)
        # TODO: raise exception if the command fails
        _ = stdout.channel.recv_exit_status()  # noqa
        artifact = urllib2.urlopen(artifact_url)
        self.sftp_client.stream_file_to_remote(artifact, destination)
Ejemplo n.º 31
0
def oxe_get_rainbow_agent_version(host, port, password):
    """Summary
    
    Args:
        host (TYPE): Description
        port (TYPE): Description
        password (TYPE): Description
    
    Returns:
        TYPE: Description
    """
    # connect OXE through SSH and execute 'rainbowagent -v'
    client = SSHClient()  # use the paramiko SSHClient
    client.set_missing_host_key_policy(
        AutoAddPolicy())  # automatically add SSH key
    try:
        client.connect(host, port, username='******', password=password)
    except AuthenticationException:
        print('*** Failed to connect to {}:{}'.format(host, port))
    command = 'rainbowagent -v'
    stdin, stdout, stderr = client.exec_command(command)
    version = {'rainbowagent version': stdout.readlines()[0].split()[2]}
    pprint(version)
    client.close()
    return version
Ejemplo n.º 32
0
def run_cmd_ssh(cmd: str) -> Response:
    """
    Run batch script using ssh client.

    Args:
        cmd: batch script to run.
    Returns:
        Response object containing stderr, stdout and exit_status.
    """
    ssh_client = SSHClient()
    ssh_client.load_system_host_keys()
    ssh_client.set_missing_host_key_policy(AutoAddPolicy())
    ssh_client.connect(username=config['user'],
                       password=config['pass'],
                       hostname=config['host'],
                       port=int(config['ssh_port']),
                       allow_agent=False,
                       look_for_keys=False)

    cmd_st = ssh_client.exec_command(cmd)

    rs = namedtuple('Response', ['std_out', 'std_err', 'status_code'])
    rs.std_out = cmd_st[1].read()
    rs.std_err = cmd_st[2].read()
    rs.status_code = cmd_st[1].channel.recv_exit_status()
    ssh_client.close()

    return rs
class RemoteBenchMarker():
    host_name = ""
    port = -1
    username = "******"
    key_filename = "empty"
    passphrase = "-1"
    client = None

    def __init__(self, host_name, port, username, key_filename, passphrase):
        self.host_name = host_name
        self.port = port
        self.username = username
        self.key_filename = key_filename
        self.passphrase = passphrase
        self.client = SSHClient()

    def exec_command(self, commands):
        print("running commands {}".format(commands))
        self.client.load_system_host_keys()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.client.connect(hostname=self.host_name,
                            port=self.port,
                            username=self.username,
                            key_filename=self.key_filename,
                            passphrase=self.passphrase)
        # wait for command completion
        for command in commands:
            stdin, stdout, stderr = self.client.exec_command(command)
            exit_status = stdout.channel.recv_exit_status()
            print("{} : {} finished at {}".format(exit_status, command,
                                                  datetime.now()))

    def close_client(self):
        self.client.close()
Ejemplo n.º 34
0
    def ssh_cmd(self):
        ssh = SSHClient()
        ssh.set_missing_host_key_policy(AutoAddPolicy())

        try:
            ssh.connect(self.host,
                        self.port,
                        self.user,
                        self.password,
                        timeout=self.timeout)
        except AuthenticationException as e:
            outList = ["ssh服务器身份验证错误,请检查核对您的用户名以及密码", str(e)]
        except NoValidConnectionsError as e:
            outList = ["ssh服务器连接错误", str(e)]
        except toe:
            outList = ["ssh服务器连接超时,请重新试试"]
        else:
            sshCmdStr = "python /etc/nginx/fprNginx.py {} {}".format(
                self.stream, self.cmd)
            _, out, __ = ssh.exec_command(sshCmdStr)
            outList = out.readlines()
            for item in outList:
                if "upstream name do not exists" == item.strip():
                    break
            else:
                outList.append("成功!")
        finally:
            ssh.close()

        return "".join(outList)
Ejemplo n.º 35
0
def vm_create_eva_cfg(host, port=22, password='******', accesses=15):
    """Summary
    
    Args:
        host (TYPE): Description
        port (int, optional): Description
        password (str, optional): Description
        accesses (int, optional): Description
    """
    data = 'callserver1=' + host + '\ncallserver2=' + '\neva=' + host + '\eva_access=' + accesses
    client = SSHClient()  # use the paramiko SSHClient
    client.set_missing_host_key_policy(
        AutoAddPolicy())  # automatically add SSH key
    try:
        client.connect(host, port, username='******', password=password)
    except AuthenticationException:
        print('*** Failed to connect to {}:{}'.format(host, port))
    channel = client.invoke_shell()
    while channel.recv_ready() is False:
        sleep(3)  # OXE is really slow on mtcl connexion
    stdout = channel.recv(4096)
    channel.send('cat >> /usr3/mao/eva.cfg << EOF\n' + data + '\nEOF\n')
    while channel.recv_ready() is False:
        sleep(0.5)
    stdout += channel.recv(1024)
    # print(stdout.decode(encoding='UTF-8'))
    channel.close()
    client.close()
Ejemplo n.º 36
0
 def start_monitor(self):
     local_name = platform.node()
     print "Starting Cassandra Monitor for %s" % self.server
     while True:
         try:
             ssh = SSHClient()
             ssh.load_system_host_keys()
             ssh.set_missing_host_key_policy(AutoAddPolicy())
             ssh.connect(self.server, timeout=5, allow_agent=False)
             stdin, stdout, stderr = ssh.exec_command(CASS_CMD)
             stdin.close()
             for line in stdout:
                 # Any line that shows up will be a downed server
                 # server datacenter rack status state load owns token
                 downed_node = line.split()
                 raise DownedNodeException(downed_node[0])
             stdout.close()
             err = stderr.read()
             if err:
                 raise Exception("Unknown error: %s" % err)
             stderr.close()
             ssh.close()
         except DownedNodeException as e:
             self.mark_error(e.node, e)
         except SSHException as e:
             self.mark_error(self.server, "%s could not connect to %s: %s" % (local_name, self.server, e))
         except Exception as e:
             self.mark_error(local_name, "Unknown error: %s" % e)
         else:
             self.clear_error()
         time.sleep(INTERVAL)
Ejemplo n.º 37
0
class SSH(object):
    """docstring for SSH"""
    def __init__(self):
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(AutoAddPolicy())
        self.client.load_system_host_keys()

    def connect(self, server, user, key):
        logging.info("Connecting to {u}@{s} [with key:{k}]".format(
            u=user, s=server, k=key
        ))
        self.client.connect(server, username=user,
                       key_filename=key, timeout=2.0)

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

    @property
    def transport(self):
        return self.client.get_transport()


    @staticmethod
    def SSHFactory(config):
        logging.debug("Creating SSH Object.")
        sClient = SSH()
        sClient.connect(config.staging,
                        config.stagingUser,
                        config.key
                        )
        return sClient
Ejemplo n.º 38
0
    def _shell(self):
        """
        Create SSHClient instance and connect to the destination host.

        :return: Connected to the remote destination host shell.
        :rtype: generator(SSHClient)
        :raise SshDestinationError: if the ssh client fails to connect.
        """
        shell = SSHClient()
        shell.set_missing_host_key_policy(AutoAddPolicy())
        try:
            LOG.debug(
                "Connecting to %s:%d as %s with key %s",
                self._host,
                self._port,
                self._user,
                self._key,
            )
            shell.connect(
                hostname=self._host,
                key_filename=self._key,
                port=self._port,
                username=self._user,
            )
            yield shell
        except FileNotFoundError:
            raise
        except (AuthenticationException, SSHException, socket.error) as err:
            # print(type(err))
            raise SshClientException(err)
        finally:
            shell.close()
Ejemplo n.º 39
0
def connect_and_retrieve(device,lock=None):
    ssh_client=SSHClient()
    ssh_client.set_missing_host_key_policy(.AutoAdd())
    try:
        ssh_client.connect(device.address,port=22,username=device.username,password=device.password)
    except AuthenticationException:
        continue
    except SSHException:
        return False
    except socket.error:
        return False
    else:
        try:
            result=better_exec("cat  /tmp/running.cfg",ssh_client)+'\n'
            result=result+(better_exec("cat  /tmp/system.cfg",ssh_client))+'\n'
            result=result+(better_exec("exit",ssh_client))+'\n'
        except paramiko.SSHException:
            lock.acquire() if not lock == None
            outfile=open('configs.txt','a')
            outfile.write(device.address+'FAILED')
            outfile.close()
            lock.release() if not lock == None
            return False
        else:
            lock.acquire() if not lock == None
            outfile=open('configs.txt','a')
            outfile.write(result)
            outfile.write('\n')
            outfile.write(device.address+'\n')
            outfile.close()
            lock.release() if not lock == None
            ssh_client.close()
            return True
Ejemplo n.º 40
0
Archivo: main.py Proyecto: hyzyla/dhavn
class RemoteClient:

    def __init__(self, host, user, remote_path):
        self.host = host
        self.user = user
        self.remote_path = remote_path
        self.client = None

    def connect(self):
        try:
            self.client = SSHClient()
            self.client.load_system_host_keys()
            self.client.set_missing_host_key_policy(AutoAddPolicy())
            self.client.connect(self.host, username=self.user, timeout=5000)

        except AuthenticationException as error:
            logger.info("Authentication failed: did you remember to create an SSH key?")
            logger.error(error)
            raise error
        finally:
            return Connection(self.client)

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

    def __enter__(self):
        return self.connect()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.disconnect()
Ejemplo n.º 41
0
def make_queen_bee(hostname, username, key_filename):

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

    client.connect(
        hostname,
        username=username,
        key_filename=key_filename
    )

    _, stdout, sterr = client.exec_command('sudo pkill shutdown')
    print(stdout.read() + sterr.read())

    boto_path = path.expanduser('~/.boto')
    command = 'scp -o "StrictHostKeyChecking=no" -i {} {} {}@{}:~/.boto'.format(
        key_filename, boto_path, username, hostname
    )

    system(command)
    print(command)

    command = 'scp -o "StrictHostKeyChecking=no" -i {} {} {}@{}:~/.ssh/'.format(
        key_filename, key_filename, username, hostname
    )

    system(command)
    print(command)

    # the royal yaass
    _, stdout, _ = client.exec_command('touch ~/.yaass')

    client.close()
    return hostname
Ejemplo n.º 42
0
def wait_for_beessh(options):

    pause = options.get('pause', 10)
    timeout = options.get('timeout', 600)

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

    wait = 0
    while wait < timeout:

        try:

            client.connect(
                options['hostname'],
                username=options['username'],
                key_filename=options['key_filename'],
                timeout=5
            )

            print('[Queen] connected to {} in {} seconds'.format(options['hostname'], wait))
            client.close()
            return {options['hostname']: {'username': options['username'], 'name': options['hostname']}}

        except (SocketError, TimeoutError) as error:
            time.sleep(pause)
            wait += pause

        except Exception as error:
            print('[Queen] error connecting to instance', options['hostname'], error)

    print('[Error] timed out while connecting over ssh to {}'.format(options['hostname']))
    return None
Ejemplo n.º 43
0
def get(nodes_file, request):
    """Return the logs"""
    args = dict(request.args.items())
    try:
        container = args.pop('container')
    except KeyError:
        raise errors.BadRequest('No container on URL arguments')
    results = dict()

    with open(nodes_file) as f:
        nodes = yaml.load(f)
        try:
            rsa_key_file = nodes.pop('rsa_key')
            pkey = RSAKey.from_private_key_file(rsa_key_file)
        except Exception as e:
            print('Failed to read RSA Key, {} {}'.format(type(e), e))
            raise
        ssh = SSHClient()
        ssh.set_missing_host_key_policy(AutoAddPolicy)
        cmd = 'docker logs {container} {args}'.format(
            container=container,
            args=' '.join(['--{}={}'.format(a, args[a]) for a in args]))

        for _, ips in nodes.items():
            for ip in ips:
                ssh.connect(hostname=ip, username='******', pkey=pkey)
                print('ssh root@{ip} {cmd}'.format(ip=ip, cmd=cmd))
                _in, _out, _err = ssh.exec_command(cmd)
                status = _out.channel.recv_exit_status()
                results[ip] = dict(
                    status=status, stdout=_out.read(), stderr=_err.read())
        ssh.close()
    return results
Ejemplo n.º 44
0
  def executeOnMaster(self, cmd):
    """
    Execute command on the current master leader
    """
    self.log.debug("Executing on master: " + cmd)

    if self._hostnameResolves(self.getManagementEndpoint()):
      ssh = SSHClient()
      ssh.load_system_host_keys()
      ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh.connect(
        self.getManagementEndpoint(),
        username = self.config.get('ACS', "username"),
        port = 2200,
        key_filename = os.path.expanduser(self.config.get('SSH', "privatekey")))
      session = ssh.get_transport().open_session()
      AgentRequestHandler(session)
      stdin, stdout, stderr = ssh.exec_command(cmd)
      stdin.close()
      
      result = ""
      for line in stdout.read().splitlines():
        self.log.debug(line)
        result = result + line.decode("utf-8") + "\n"
      for line in stderr.read().splitlines():
        self.log.error(line)
    else:
      self.log.error("Endpoint " + self.getManagementEndpoint() + " does not exist, cannot SSH into it.")
      result = "Exception: No cluster is available at " + self.getManagementEndpoint()
    ssh.close()
    return result
Ejemplo n.º 45
0
Archivo: main.py Proyecto: dobe/pypsh
 def _exec(self):
     exitcode = 0
     client = SSHClient()
     client.load_system_host_keys()
     client.set_missing_host_key_policy(WarningPolicy)
     try:
         client.connect(self.config.get('hostname'),
                        int(self.config.get('port', 22)),
                        username=self.config.get('user'))
         stdin, stdout, stderr = self.exec_command(client)
         for i, line in enumerate(stdout):
             line = line.rstrip()
             print("{0}: {1}".format(self.host, line))
         for i, line in enumerate(stderr):
             line = line.rstrip()
             print(colored("{0}: {1}".format(self.host, line), 'red'))
             exitcode = 1
     except IOError as e:
         print(colored('{0}: {1}'.format(self.host, str(e)), 'red'))
         exitcode = 1
     except (BadHostKeyException, AuthException, SSHException) as e:
         print(colored('{0}: {1}'.format(self.host, e.message)), 'red')
         exitcode = 1
     finally:
         client.close()
         return exitcode
Ejemplo n.º 46
0
def update_server(server):

    if utilities.ping(server):

        client = SSHClient()
        if utilities.ssh(server, client):
            print server.name
            command = 'python -V'
            stdin, stdout, stderr = client.exec_command(command)

            # https://bugs.python.org/issue18338
            # Apparently this is an old bug and only fixed in Python 3.4 and 2.7
            # where the version is sent to stderr

            try:
                version = stderr.readlines()[0].rstrip()
                version = re.sub('Python ', '', version)
            except:
                version = 'None'
            
            if version == 'None':
                try:
                    version = stdout.readlines()[0].rstrip()
                    version = re.sub('Python ', '', version)
                except:
                    version = 'None'

            print version

            # check existing value, if it exists, don't update
            if str(version) != str(server.python):
                utilities.log_change(server, 'python', str(server.python), str(version))

                LinuxServer.objects.filter(name=server).update(python=version, modified=timezone.now())
            client.close()
Ejemplo n.º 47
0
def send_file(local_file, remote_path, ip, username, password, logger=None):
	# Отсылает файл local_file в remote_path удаленной машины по scp
	if remote_path[len(remote_path) - 1] != '/': remote_path += '/'
	ssh = SSHClient()
	ssh.set_missing_host_key_policy(AutoAddPolicy())
	ssh.load_system_host_keys()
	if logger is not None: logger.info("SCP SEND: connecting to %s" % (ip))
	try:
		ssh.connect(ip, username=username, password=password)
	except:
		if logger is not None: logger.info("SCP SEND: failed to connect to %s" % (ip))
		return False
	else:
		if logger is not None: logger.info("SCP SEND: connected to %s" % (ip))
	try:
		if logger is not None: logger.info("SCP SEND: sending file %s" % (local_file))
		scp = SCPClient(ssh.get_transport())
		scp.put(local_file, remote_path)
	except:
		if logger is not None: logger.error("SCP SEND: error: failed to send file %s" % (local_file))
		ssh.close()
		return False
	else:
		if logger is not None: logger.info("SCP SEND: file sent to %s@%s:%s " % (username, ip, remote_path))
	ssh.close()		
	return True
Ejemplo n.º 48
0
 def startConnection(self): 
     """
     connection to remote server
     can launch a thread checking every minute your mailbox...
     """
     #if not self.directory:return
     #self.parent().close()
     self.close()
     client = SSHClient()
     client.set_missing_host_key_policy(AutoAddPolicy())
     try:
         client.connect(str(self.host.text()), username=str(self.username.text()), password=str(self.password.text()))
     except SSHException: qApp.instance().showErrorMessage("Error", "The ssh session could not be established")
     except AuthenticationException: qApp.instance().showErrorMessage("Error","Authentication failed")
     except BadHostKeyException: qApp.instance().showErrorMessage("Error", "the server's host key could not be verified")
     
     sftpCp = self.cleaningRepository(client)
     if sftpCp:
         sftp = client.open_sftp()
         self.sftpCopying(sftp)
         sftp.close()
     else:
         self.retrieveDirTree(c, out)
     self.makeRScript(client)
     self.makeShellScript(client)
     if self.interactiveSession.isChecked():
         import interactive
         print ('invoking a shell')
         chan = client.invoke_shell()
         chan.send('bash\n')
         interactive.interactive_shell(chan)
         chan.close()
     else:
         self.launchCommand(client)
     client.close()
Ejemplo n.º 49
0
 def __wait_ssh_working( self ):
     while True:
         client = SSHClient( )
         try:
             client.set_missing_host_key_policy( self.IgnorePolicy( ) )
             client.connect( hostname=self.ip_address,
                             username=self.admin_account( ),
                             timeout=a_short_time )
             stdin, stdout, stderr = client.exec_command( 'echo hi' )
             try:
                 line = stdout.readline( )
                 if line == 'hi\n':
                     return
                 else:
                     raise AssertionError( "Read unexpected line '%s'" % line )
             finally:
                 stdin.close( )
                 stdout.close( )
                 stderr.close( )
         except AssertionError:
             raise
         except KeyboardInterrupt:
             raise
         except Exception as e:
             logging.info( e )
         finally:
             client.close( )
         time.sleep( a_short_time )
Ejemplo n.º 50
0
class SFTP(Operations):
    """A simple SFTP filesystem. Requires paramiko:
            http://www.lag.net/paramiko/
            
       You need to be able to login to remote host without entering a password.
    """
    def __init__(self, host, path='.'):
        self.client = SSHClient()
        self.client.load_system_host_keys()
        self.client.connect(host)
        self.sftp = self.client.open_sftp()
        self.root = path
    
    def __del__(self):
        self.sftp.close()
        self.client.close()
    
    def __call__(self, op, path, *args):
        print '->', op, path, args[0] if args else ''
        ret = '[Unhandled Exception]'
        try:
            ret = getattr(self, op)(self.root + path, *args)
            return ret
        except OSError, e:
            ret = str(e)
            raise
        except IOError, e:
            ret = str(e)
            raise OSError(*e.args)
Ejemplo n.º 51
0
 def _uploadFiles(self, dstdir, **sshargs):
     (yield TaskOutput(u"ENTER", OutputType.NOTIFY))
     sshcli = SSHClient()
     sftpcli = None
     code = 0
     try:
         if not (yield TaskOutput(u"Conntecting to %s ..." % sshargs["hostname"])):
             raise CommandTerminated()
         sshcli.set_missing_host_key_policy(AutoAddPolicy())
         sshcli.connect(**sshargs)
         if not (yield TaskOutput(u"Connected, ready to upload ...")):
             raise CommandTerminated()
         ret = sshcli.exec_command("[ -d {0} ] && rm -rf {0}; mkdir -p {0}".format(dstdir))
         errstr = ret[2].read()
         if errstr != "":
             raise Exception(errstr)
         sftpcli = sshcli.open_sftp()
         for f in os.listdir(self.hdiff_dir):
             if f.lower().endswith(".html"):
                 localfile = os.path.join(self.hdiff_dir, f)
                 remotefile = os.path.join(dstdir, f).replace(os.sep, "/")
                 if not (yield TaskOutput(u"Uploading %s ..." % f)):
                     raise CommandTerminated()
                 sftpcli.put(localfile, remotefile)
     except CommandTerminated:
         code = -2
         (yield TaskOutput(u"Uploading Terminited", OutputType.WARN))
     except Exception as ex:
         code = -1
         (yield TaskOutput(ex.message, OutputType.ERROR))
     finally:
         if sftpcli:
             sftpcli.close()
         sshcli.close()
         (yield TaskOutput(u"EXIT %d" % code, OutputType.NOTIFY))
Ejemplo n.º 52
0
    def connect(self):
        logger.debug("Opening SSH connection to {host}:{port}".format(host=self.host, port=self.port))
        client = SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(AutoAddPolicy())

        try:
            client.connect(self.host, port=self.port, username=self.username, timeout=self.timeout)
        except ValueError, e:
            logger.error(e)
            logger.warning(
                """
Patching Crypto.Cipher.AES.new and making another attempt.

See here for the details:
http://uucode.com/blog/2015/02/20/workaround-for-ctr-mode-needs-counter-parameter-not-iv/
            """
            )
            client.close()
            import Crypto.Cipher.AES

            orig_new = Crypto.Cipher.AES.new

            def fixed_AES_new(key, *ls):
                if Crypto.Cipher.AES.MODE_CTR == ls[0]:
                    ls = list(ls)
                    ls[1] = ""
                return orig_new(key, *ls)

            Crypto.Cipher.AES.new = fixed_AES_new
            client.connect(self.host, port=self.port, username=self.username, timeout=self.timeout)
Ejemplo n.º 53
0
def sync_public_key(host, port=22, username=None, password=None):
    

	try:
		client = SSHClient()
		client.connect(hostname=host, username=username, password=password)
	except SSHException, e:
		client.close()
		client = SSHClient()
		client.set_missing_host_key_policy(AutoAddPolicy())
		client.connect(hostname=host, username=username, password=password)

		sftp_client = client.open_sftp()
		id_rsa_pub = "/home/%s/.ssh/id_rsa.pub" % LOCAL_USER_NAME
		if username == "root":
			remote_rsa_pub = "/root/.ssh/%s.pub" % (LOCAL_USER_NAME)
		else:
			remote_rsa_pub = "/home/%s/.ssh/%s.pub" % (username, LOCAL_USER_NAME)
		print remote_rsa_pub
		try:
			sftp_client.put(id_rsa_pub , remote_rsa_pub)
		except Exception, e:
			"""
			if the remote host did have .ssh dirctory
			"""
			print e
Ejemplo n.º 54
0
def get_file(remote_file, local_path, ip, username, password, logger=None):
	# Получает с удаленной машины файл remote_file с помощью scp и сохраняет его в local_path.
	if local_path[len(local_path) - 1] != '/': local_path += '/'
	ssh = SSHClient()
	ssh.set_missing_host_key_policy(AutoAddPolicy())
	ssh.load_system_host_keys()
	if logger is not None: logger.info("SCP GET: connecting to %s" % (ip))
	try:
		ssh.connect(ip, username=username, password=password)
	except:
		if logger is not None: logger.info("SCP GET: failed to connect to %s" % (ip))
		return False
	else:
		if logger is not None: logger.info("SCP GET: connected to %s" % (ip))
	try:
		if logger is not None: logger.info("SCP GET: retrieving file %s" % (remote_file))
		scp = SCPClient(ssh.get_transport())
		scp.get(remote_file, local_path)
	except:
		if logger is not None: logger.error("SCP GET: error: failed to retrieve file %s" % (remote_file))
		ssh.close()
		return False
	else:
		if logger is not None: logger.info("SCP GET: file saved to %s folder" % (local_path))
	ssh.close()
	return True
Ejemplo n.º 55
0
def verify_ssh_login(userid):
    client = SSHClient()
    client.load_system_host_keys()
    # client.set_missing_host_key_policy(WarningPolicy)
    client.set_missing_host_key_policy(AutoAddPolicy())

    # TEST ONLY
    hosts = ["india.futuregrid.org"]
    key = os.path.expanduser(os.path.join("~", ".ssh", "id_rsa"))
    print "[key: %s]" % key

    if not userid:
        userid = getpass.getuser()

    for host in hosts:
        try:
            client.connect(host, username=userid, key_filename=key)
            client.close()
            print "[%s] succeeded with %s." % (host, userid)
        except (BadHostKeyException,
                AuthenticationException,
                SSHException) as e:
            # print sys.exc_info()
            print ("[%s] %s with %s. Please check your ssh setup (e.g. key " +
                   "files, id, known_hosts)") % (host, e, userid)
Ejemplo n.º 56
0
def main():
    logging.basicConfig(level=logging.INFO)

    if isfile(get_config_path(__file__, '/esxi.ini')):
        config = ConfigurationINI(get_config_path(__file__, '/esxi.ini'))
    elif isfile('/etc/esxi.ini'):
        config = ConfigurationINI('/etc/esxi.ini')
    else:
        logging.critical('/etc/esxi.ini missing.')
        exit(0)

    logging.debug('Configuration file used : {conf}'.format(conf=get_config_path(__file__, '/esxi.ini')))

    try:
        ssh = SSHClient()
        ssh.set_missing_host_key_policy(AutoAddPolicy())
        ssh.connect(config['esxi']['hostip'], username=config['esxi']['username'], password=config['esxi']['password'])
    except SocketError as e:
        logging.critical('Host unreachable.')
        logging.critical(e.__str__())
        exit()

    logging.info('vim-cmd hostsvc/firmware/sync_config')
    ssh.exec_command('vim-cmd hostsvc/firmware/sync_config')
    logging.info('vim-cmd hostsvc/firmware/backup_config')
    stdin, stdout, stderr = ssh.exec_command('vim-cmd hostsvc/firmware/backup_config')

    for l in stdout:
        m = search('http://\*(.*)', l.strip())
        if m is not None:
            download = "http://{host}{position}".format(
                host=config['esxi']['hostip'],
                position=m.group(1)
            )
            logging.info("Downloading {0}".format(download))
            local_file = '{localpath}/backup-{host}-{date}.tgz'.format(
                host=config['esxi']['hostdns'],
                date=strftime(config['local']['dateformat']),
                localpath=config['local']['savepath']
            )
            urlretrieve(download, local_file)

            if config['webdav']['enabled']:
                logging.info("Uploading file on WebDAV")
                comediaoc = webdav_connect(
                    config['webdav']['host'],
                    username=config['webdav']['username'],
                    password=config['webdav']['password'],
                    protocol=config['webdav']['proto'],
                    verify_ssl=False
                )
                comediaoc.upload(local_file, '{0}/backup-{1}-{2}.tgz'.format(
                    config['webdav']['savepath'],
                    config['esxi']['hostdns'],
                    strftime(config['local']['dateformat'])
                ))

    ssh.close()
Ejemplo n.º 57
0
    def run(self):
        try:
            # run a command and wait for it to finish
            def run(command):
                _, stdout, _ = ssh.exec_command(command)
                stdout.channel.recv_exit_status()

            # send the webapp a textual progress update
            listener = Listener()
            def log(text):
                listener.publish(('project', self.project.name, 'deploy', 'status'), { 'text': text })

            # prevent accidentally deploying on the server itself
            # (this doesn't prevent deploying to the server's public
            # IP address, so there's still a security problem here)
            if self.host in ['127.0.0.1', '0.0.0.0']:
                raise Exception('you\'re trying to deploy to the server!')

            # create temporary build file
            path = os.path.join(tempfile.mkdtemp(), 'deploy.launch')
            xml = roslaunch_xml_for_file(self.project.project_file_path, self.remote_deploy_path)
            open(path, 'w').write(xml)
            log('Created file: deploy.launch')

            # create a ssh session
            log('Connecting to %s@%s...' % (self.user, self.host))
            ssh = SSHClient()
            ssh.load_system_host_keys()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            ssh.connect(self.host, username=self.user, password=self.password)
            log('Connected to remote machine')

            # clean the deploy location
            run('rm -fr %s' % self.remote_deploy_path)
            run('mkdir -p %s' % self.remote_deploy_path)
            log('Cleaned deploy location')

            # copy the launch file over
            scp = SCPClient(ssh.get_transport())
            scp.put(path, remote_path=self.remote_deploy_path)
            log('Copied file: deploy.launch')

            # run the file
            command = 'cd %s && roslaunch deploy.launch' % self.remote_deploy_path
            channel = ssh.invoke_shell()
            channel.send(command + '\nexit\n')
            log('Ran command: roslaunch deploy.launch')

            # wait for the command to finish
            import time
            while not self.die:
                time.sleep(0.1)
        except:
            import traceback
            log('Exception:\n' + traceback.format_exc())

        ssh.close()
        log('Connection was closed')
Ejemplo n.º 58
0
def inspect_container(server, name):
    client = SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    client.load_system_host_keys()
    client.connect(server[0], username=server[1], password=server[2])
    stdin, stdout, stderr = client.exec_command('sudo docker inspect %s' % name)
    lines = stdout.readlines()
    client.close()
    return ''.join(lines)
Ejemplo n.º 59
0
def start_ssh(host, username, password):
    ssh = SSHClient()
    try:
        ssh.set_missing_host_key_policy(AutoAddPolicy())
        ssh.connect(host, username=username, password=password)
    except SSHException:
        ssh.close()
        raise
    return ssh
Ejemplo n.º 60
0
class UbiquityBase(PDUDriver):
    client = None
    # overwrite power_count
    port_count = 0

    def __init__(self, hostname, settings):
        self.hostname = hostname
        log.debug(settings)
        self.settings = settings
        self.sshport = 22
        self.username = "******"
        self.password = "******"
        if "sshport" in settings:
            self.sshport = settings["sshport"]
        if "username" in settings:
            self.username = settings["username"]
        if "password" in settings:
            self.password = settings["password"]
        self.connect()

        super(UbiquityBase, self).__init__()

    def connect(self):
        log.info("Connecting to Ubiquity mfi %s@%s:%d",
                 self.username, self.hostname, self.sshport)
        self.client = SSHClient()
        self.client.load_system_host_keys()
        self.client.connect(hostname=self.hostname, port=self.sshport,
                            username=self.username, password=self.password)

    def port_interaction(self, command, port_number):
        log.debug("Running port_interaction from UbiquityBase")
        if port_number > self.port_count:
            raise RuntimeError("We only have ports 1 - %d. %d > maxPorts (%d)"
                               % self.port_count, port_number, self.port_count)

        if command == "on":
            command = "sh -c 'echo 1 > /proc/power/relay%d'" % port_number
        elif command == "off":
            command = "sh -c 'echo 0 > /proc/power/relay%d'" % port_number

        try:
            stdin, stdout, stderr = self.client.exec_command(command, bufsize=-1, timeout=3)
            stdin.close()
        except SSHException as exc:
            pass

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

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

    @classmethod
    def accepts(cls, drivername):
        log.debug(drivername)
        return False