示例#1
0
def run(args, logging):
    """
    Runs the cmd executor

    :param args: A NameSpace object with the arguments required
    :param logging: A python logging object
    :return: An exit code
    """
    hostname = build_data.read(args.build_id, 'instance', 'public_ip_address')

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

    logging.info('Connecting to remote host "{0}"...'.format(hostname))
    client.connect(hostname, port=args.port, username=args.username,
                   key_filename=args.key_filename, timeout=args.timeout)

    logging.info('Executing command "{0}"...'.format(args.cmd))
    # pylint: disable=W0612
    stdin, stdout, stderr = client.exec_command(args.cmd)

    exit_code = stdout.channel.recv_exit_status()
    stdout = stdout.channel.makefile().read() or 'None'
    stderr = stderr.channel.makefile().read() or 'None'

    logging.info('Exit code...')
    logging.info(exit_code)
    logging.info('Standard out...')
    logging.info(stdout)
    logging.info('Standard error...')
    logging.info(stderr)
    return exit_code, stdout, stderr
示例#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()
示例#3
0
 def create_client(self):
     private = RSAKey(filename=self.get_private_key_file())
     client = SSHClient()
     client.set_missing_host_key_policy(AutoAddPolicy())
     client.load_host_keys(self.get_host_keys_file())
     client.connect(self.server.host, pkey=private, look_for_keys=False, port=self.server.port, username=self.server.user)
     return client
示例#4
0
def __startInstance( device , datatype , response , pcount ):

    processorNode = settings.PROCCESSOR_NODES[ (pcount + 1 ) % len( settings.PROCCESSOR_NODES ) ]

    logger.debug("Creating Node On " + processorNode )

    client = SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    rsa_private_key = paramiko.RSAKey.from_private_key_file( settings.SSHID )
    client.connect( processorNode , username="******" , pkey = rsa_private_key )

    logger.debug( 'docker run -d ' + datatype.processor + ' python /root/Processor.py ' + settings.MASTER_HOSTNAME + \
            ' ' + device.identifier + ' ' + datatype.identifier + ' ' + response.identifier )

    stdin, stdout, stderr = client.exec_command(
            'docker run -d ' + datatype.processor + ' python /root/Processor.py ' + settings.MASTER_HOSTNAME + \
                    ' ' + device.identifier + ' ' + datatype.identifier + ' ' + response.identifier
    )

    for i in stderr.readlines():
        logger.error(str(i))
    for i in stdout.readlines():
        logger.debug(str(i))
        identifier = i

    return { 'identifier' : identifier , 'node' : processorNode }
示例#5
0
 def connect(self):
     """Initiate an ssh connection with properties passed to constructor.
     :returns:
       Instance of the paramiko SSHClient class.
     """
     usePrivateKey = True
     for prop in self.required_props1:
         if prop not in list(self.properties.keys()):
             usePrivateKey = False
             break
     usePassword = True
     for prop in self.required_props2:
         if prop not in list(self.properties.keys()):
             usePassword = False
             break
     if not usePrivateKey and not usePassword:
         raise ShakeMapException('Either username/password must be specified, or the name of an SSH private key file.')
     
     ssh = SSHClient()
     #load hosts found in ~/.ssh/known_hosts
     ssh.load_system_host_keys() #should we not assume that the user has these configured already?
     if usePrivateKey:
         try:
             ssh.connect(self.properties['remotehost'],
                         key_filename=self.properties['privatekey'],compress=True)
         except Exception as obj:
             raise ShakeMapException('Could not connect with private key file %s' % self.properties['privatekey'])
     else:
         try:
             ssh.connect(self.properties['remotehost'],
                         username=self.properties['username'],password=self.properties['password'],
                         compress=True)
         except Exception as obj:
             raise ShakeMapException('Could not connect with private key file %s' % self.properties['privatekey'])
     return ssh
def get_file(fname):
    ssh = SSHClient()
    ssh.load_system_host_keys()
    ssh.connect(file_server)
    scp = SCPClient(ssh.get_transport())
    scp.get(fname)
    scp.close()
示例#7
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))
class SSH(object):
    """
    package for ssh_client, in this class stores the sftp handlers and the
    ssh_client
    """
    def __init__(self, host, user, password, port=22):
        self.host = host
        self.user = user
        self.password = password
        self.port = port

    def client_init(self):
        self.clt = SSHClient()
        self.clt.load_system_host_keys()
        self.clt.set_missing_host_key_policy(AutoAddPolicy())

    def ssh_connect(self, task_list, ret_map):
        try:
            self.client_init()
            self.clt.connect(hostname=self.host, username=self.user,
                             password=self.password, port=self.port,
                             timeout=3)
            self.sftp = self.clt.open_sftp()
        except Exception, e:
            self.ssh_close()
            for task in task_list:
                if len(task) > 1:
                    ret_map[task[0]] = (-1, "ssh connect error %s" %
                                        str(e))
            return False
        return True
示例#9
0
文件: data_mover.py 项目: XENON1T/cax
    def copySCP(self, datum_original, datum_destination, server, username, option_type):
        """Copy data via SCP function
        """
        util.log_to_file('ssh.log')
        ssh = SSHClient()
        ssh.load_system_host_keys()

        logging.info("connection to %s" % server)
        ssh.connect(server,
                    username=username,
                    compress=True,
                    timeout=60)

        # SCPCLient takes a paramiko transport as its only argument
        client = scp.SCPClient(ssh.get_transport())

        logging.info(option_type + ": %s to %s" % (datum_original['location'],
                                                   datum_destination['location']))

        if option_type == 'upload':
            client.put(datum_original['location'],
                       datum_destination['location'],
                       recursive=True)
        else:
            client.get(datum_original['location'],
                       datum_destination['location'],
                       recursive=True)

        client.close()
示例#10
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
示例#11
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
示例#12
0
class sshConnection():

    def __init__(self, host, user, password):
        self.host = host
        self.user = user
        self.password = password
        self.connection = None
        self.channel = None

    def connect(self):
        try:
            print "[+] Estableciendo conexion SSH con %s@%s...." % (self.user, self.host)
            self.connection = SSHClient()
            self.connection.set_missing_host_key_policy(AutoAddPolicy())
            self.connection.connect(
                self.host, 
                username = self.user, 
                password = self.password, 
                allow_agent = False, 
                look_for_keys = False
            )
        except AuthenticationException, e:
            print "[+] Error de autenticacion: " ,e
            return 1
        except SSHException, e:
            print "[+] Error en SSH: " , e
            return 2
示例#13
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
示例#14
0
    def get(self, remote_path, local_path=None, out_stream=sys.stdout, verbose=False):
        """
        Copy a file from the remote system to the local system.

        :param remote_path:
        :param local_path:
        :param out_stream:
        :param verbose:
        :return: :rtype:
        """
        if local_path is None:
            local_path = remote_path
        self.display("scp '{src}' '{dest}'".format(src=remote_path, dest=local_path),
                     out_stream=out_stream, verbose=verbose)

        names = self.run(['ls', '-1', remote_path]).split('\n')

        ssh = SSHClient()
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(Project.address, Project.port, Project.user, Project.password)
        # scp = SFTPClient.from_transport(ssh.get_transport())
        # output = scp.get(remote_path, local_path, recursive=True)

        ftp = ssh.open_sftp()
        for name in names:
            print(name)
            ftp.get(name, local_path)

        output = repr(names)
        self.display(output, out_stream=out_stream, verbose=verbose)
        return output
示例#15
0
def ssh_cmd(ip, remote_cmd, user='******', password=None, quiet=False):
    """
    @param server_ip
    @param user
    @param password
    @param remote_cmd
    @return A map based on pass / fail run info
    """
    output = StringIO()
    error = StringIO()
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(AutoAddPolicy())
    ssh.connect(ip, username=user, password=password)
    stdin, stdout, stderr = ssh.exec_command(remote_cmd)
    stdin.close()
    exit_status = None
    for line in stdout.xreadlines():
        if not quiet:
            sys.stdout.write(line)
            output.write(line)
    for line in stderr.xreadlines():
        sys.stdout.write(line)
        error.write(line)
        exit_status = stdout.channel.recv_exit_status()
    return {'success': True if exit_status == 0 else False,
            'return': output.getvalue(),
            'exit_status': exit_status,
            'error': error.getvalue()}
示例#16
0
    def rollback_using_import_policy(self, version):
        filenames = self.get_filenames(version)

        # Create the tar file of the selected JSON files
        self._generate_tar_gz(filenames, version)

        # Put the tar file on the APIC
        ssh = SSHClient()
        ssh.load_system_host_keys()
        ssh.set_missing_host_key_policy(AutoAddPolicy())
        ssh.connect(self.session.ipaddr, username=self.session.uid, password=self.session.pwd)
        sftp = ssh.open_sftp()
        sftp.chdir('/home/%s' % self.session.uid)
        sftp.put('./ce_snapback.tar.gz', 'ce_snapback.tar.gz')

        # Remove the local tar file
        os.remove('./ce_snapback.tar.gz')

        # Send the import policy to the APIC
        url = '/api/node/mo/uni/fabric.json'
        payload = {"configImportP": {"attributes": { "name": "snapback",
                                                     "fileName": "ce_snapback.tar.gz",
                                                     "adminSt": "triggered",
                                                     "importType": "replace",
                                                     "importMode": "atomic"},
                                     "children": [{"configRsImportSource": {"attributes": {"tnFileRemotePathName": "snapback"},
                                                                            "children": []
                                                                            }}]}}
        resp = self.session.push_to_apic(url, payload)
        return resp
def main(file, key, encrypt, host, username, password):
    """
    The main function. Takes multiple parameters which are prompted if not given on the command line.
    :param file: The paht of the file to encrypt or decrypt and send.
    :param key: The key to encrypt or decrypt.
    :param encrypt: Tells if the operation is an encryption or a decryption.
    :param host: The host where to send the file.
    :param username: Username on the host.
    :param password: Password if needed. If not needed '-' should be used to tell that there is no password needed.
    """
    ssh = SSHClient()
    ssh.load_system_host_keys()
    if password != "-":
        ssh.connect(host, username=username, password=password)
    else:
        ssh.connect(host, username=username)

    scp = SCPClient(ssh.get_transport())

    if encrypt:
        print("Encrypting... ", end="")
        to_send = encrypt_file(file, key)
        print("Done.")
        print("Sending to {}...".format(host), end="")
        scp.put(to_send)
        print("Done.")
    else:
        print(decrypt_file(file, key))
示例#18
0
文件: main.py 项目: 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
示例#19
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
示例#20
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)
示例#21
0
文件: base.py 项目: keikhara/acs-cli
  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
示例#22
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)
示例#23
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)
示例#24
0
def get_proxmox_ssh(proxmox):
    proxmox_ssh = SSHClient()
    proxmox_ssh.set_missing_host_key_policy(WarningPolicy())
    proxmox_ssh.connect(proxmox['host'],
                        username=proxmox['user'].split('@')[0])

    return proxmox_ssh
示例#25
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
示例#26
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
示例#27
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()         
示例#28
0
文件: box.py 项目: adderan/cgcloud
 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 )
示例#29
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()
示例#30
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()
示例#31
0
POOL = os.environ['POOL_DIR']
TRASH = os.environ['TRASH_DIR']
DEPLOY = os.environ['DEPLOY_DIR']
SUPERVISOR_USER = os.environ['SUPERVISOR_USER']
SUPERVISOR_IP = os.environ['SUPERVISOR_IP']
ENVVAR_PATH = os.environ['ENVVAR_PATH']
ENVVAR_NAME = os.environ['ENVVAR_NAME']
POST_INSTALL_PATH = os.environ['POST_INSTALL_PATH']
POST_INSTALL_NAME = os.environ['POST_INSTALL_NAME']
APACHE_ENVVARS = '/etc/apache2/envvars'

# Define ssh connection to supervisor
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect(SUPERVISOR_IP,username=SUPERVISOR_USER)

# SCPCLient takes a paramiko transport as its only argument
scp = SCPClient(ssh.get_transport())

# Make log file
logging.basicConfig(filename='debug.log', level=logging.DEBUG)

# Helper functions

def post_install():
    """Execute post installation script"""
    log = ''
    if POST_INSTALL_NAME:
        scp.get(POST_INSTALL_PATH, ROOT)
        log += subprocess.check_output(os.path.join(ROOT, POST_INSTALL_NAME), executable='/bin/bash', shell=True)
示例#32
0
    def _execute(self, func, **kwargs):
        """
        Do processing after connected to sftp server

        Args:
            func (fucntion): function which processing is written.
            kwargs (dict): arguments when function is executed

        Raises:
            ValueError: argumets are empty
            IOError: failed to get data
        """

        if not func:
            raise ValueError("Function must not be empty.")

        for _ in range(self._retryTimes):
            ssh = None
            sftp = None

            try:
                ssh = SSHClient()
                ssh.set_missing_host_key_policy(AutoAddPolicy())
                if self._password:
                    ssh.connect(
                        hostname=self._host,
                        username=self._user,
                        password=self._password,
                        port=self._port,
                        timeout=self._timeout,
                    )
                else:
                    ssh.connect(
                        hostname=self._host,
                        username=self._user,
                        key_filename=self._key,
                        port=self._port,
                        timeout=self._timeout,
                    )

                sftp = ssh.open_sftp()

                # call the argument function
                ret = func(sftp=sftp, **kwargs)

                return ret

            except Exception as e:
                self._logger.warning(e)
                self._logger.warning(kwargs)

            finally:
                if sftp is not None:
                    sftp.close()
                if ssh is not None:
                    ssh.close()

            self._logger.warning(
                "Unexpected error occurred. Retry will start in 10 sec.")
            sleep(10)

        raise IOError(errno.ENOENT, "SFTP failed.")
示例#33
0
from pynput.mouse import Listener
from scp import SCPClient
from paramiko import SSHClient
import logging
import pyautogui

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('ex.com', 22, "USERNAME", "PASSWORD")
scpCl = SCPClient(ssh.get_transport())

count = 0


def on_click(x, y, button, pressed):
    if pressed:
        screensht = pyautogui.screenshot()
        scpCl.put(screensht, "test" + str(count) + ".png")
        #screensht.save("test"+str(count)+".png")
        global count
        count += 1


with Listener(on_click=on_click) as listener:
    listener.join()
    scpCl.close()
示例#34
0
class ssh(ClientBase, Commands):
    def __init__(self, sessions, options):
        """
            Initialize the SSH Bee, and the Base classes.

        :param sessions: A dict which is updated every time a new session is created.
        :param options: A dict containing all options
        """
        ClientBase.__init__(self, sessions, options)
        Commands.__init__(self)
        self.client = SSHClient()
        self.client.set_missing_host_key_policy(AutoAddPolicy())
        self.comm_chan = None

    def start(self):
        """
            Launches a new SSH client session on the server taken from the `self.options` dict.

        :param my_ip: IP of this Client itself
        """
        username = self.options['username']
        password = self.options['password']
        server_host = self.options['server']
        server_port = self.options['port']
        honeypot_id = self.options['honeypot_id']

        session = self.create_session(server_host, server_port, honeypot_id)

        self.sessions[session.id] = session
        logger.debug(
            'Sending %s bait session to {0}:{1}. (bait id: %s)'.format(
                'ssh', server_host, server_port, session.id))
        try:
            self.connect_login()
            session.did_connect = True
            # TODO: Handle failed login
            session.add_auth_attempt('plaintext',
                                     True,
                                     username=username,
                                     password=password)
            session.did_login = True
        except (SSHException, AuthenticationFailed) as err:
            logger.debug('Caught exception: {0} ({1})'.format(
                err, str(type(err))))
        else:
            command_count = 0
            command_limit = random.randint(6, 11)
            while command_count < command_limit:
                command_count += 1
                self.sense()
                comm, param = self.decide()
                self.act(comm, param)
                gevent.sleep(random.uniform(0.4, 5.6))
            self.logout()
            session.did_complete = True
        finally:
            session.alldone = True

    def send_command(self, cmd):
        """
            Send a command to the remote SSH server.

        :param cmd: The command to send
        """
        logger.debug('Sending {0} command.'.format(cmd))
        self.comm_chan.sendall(cmd + '\n')

    def get_response(self):
        """
            Get the response from the server. *This may not return the full response*

        :return: Response data
        """
        while not self.comm_chan.recv_ready():
            time.sleep(0.5)
        return self.comm_chan.recv(2048)

    def connect_login(self):
        """
            Try to login to the Remote SSH Server.

        :return: Response text on successful login
        :raise: `AuthenticationFailed` on unsuccessful login
        """
        self.client.connect(self.options['server'], self.options['port'],
                            self.options['username'], self.options['password'])
        self.comm_chan = self.client.invoke_shell()
        time.sleep(1)  # Let the server take some time to get ready.
        while not self.comm_chan.recv_ready():
            time.sleep(0.5)
        login_response = self.comm_chan.recv(2048)
        if not login_response.endswith('$ '):
            raise AuthenticationFailed
        return login_response

    def logout(self):
        """
            Logout from the remote server
        """
        self.send_command('exit')
        self.get_response()
        self.comm_chan.close()
示例#35
0
#!/usr/bin/python

import scp
import json
from paramiko import SSHClient
from scp import SCPClient
from avi.sdk.avi_api import ApiSession

host = "10.91.55.11"
user = "******"
password = "******"

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(host, username=user, password=password)

scp = SCPClient(ssh.get_transport())
scp.put('../controller.17.2.11.pkg', remote_path='/tmp')

session = ssh.get_transport().open_session()
session.set_combine_stderr(True)
session.get_pty()
session.exec_command(
    'sudo cp /tmp/controller.17.2.11.pkg /var/lib/avi/upgrade_pkgs/controller.pkg'
)
stdin = session.makefile('wb', -1)
stdout = session.makefile('rb', -1)
stdin.write(password + '\n')
stdin.flush()
print(stdout.read().decode("utf-8"))
session.close()
示例#36
0
class RemoteClient:
    '''Client for connecting to remote host via SSH & SCP'''
    def __init__(self, host, user, key='id_rsa', remote_path='~'):
        self.host = host
        self.user = user
        self.remote_path = remote_path
        self.key_file = str(Path.home()) + '/.ssh/' + key
        self.client = None
        self.scp = None
        self.conn = None

    def _get_ssh_key(self, pword=None):
        '''Fetch locally stored SSH key'''
        try:
            self.ssh_key = RSAKey.from_private_key_file(self.key_file)
            log.debug(f'Found RSA key at {self.key_file}')
        except PasswordRequiredException:
            pword = getpass('Enter SSH key password:'******'Found passworded RSA key at {self.key_file}')
        except SSHException as e:
            log.error(e)

    def _connect(self):
        '''Open connection to remote host'''
        if self.conn is None:
            try:
                self._get_ssh_key()
                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,
                    pkey=self.ssh_key,
                    look_for_keys=True,
                    timeout=5000
                )
                self.scp = SCPClient(self.client.get_transport())
            except (AuthenticationException, SSHException) as e:
                log.error(f'Connection failed: {e}')
                raise e
        return self.client

    def set_loglevel(self, level="ERROR"):
        log.setLevel(level)
        return log.getLevelName()

    def disconnect(self):
        '''Close ssh connection'''
        if self.client:
            self.client.close()
        if self.scp:
            self.scp.close()

    def execute(self, command):
        '''
        Execute command on remote host.

        :param command: Unix command as a single string.
        :type command: str
        '''
        self.conn = self._connect()
        stdin, stdout, stderr = self.client.exec_command(command)
        stdout.channel.recv_exit_status()
        for error in stderr.readlines():
            log.error(f'INPUT: {command} | ERR: {error}')
        return stdout.readlines()

    def upload(self, file, r=False):
        '''
        Upload file to a remote directory.

        :param files: filename as string.
        :type files: str
        '''
        self.conn = self._connect()
        try:
            self.scp.put(file, recursive=r, remote_path=self.remote_path)
        except SCPException as e:
            log.error(f'File transfer error: {e}')
            raise e
        log.info(f'Uploaded {self.remote_path}/{file} on {self.host}')

    def download(self, file, r=False):
        '''
        Download file from remote directory.

        :param files: filename as string
        :type files: str
        '''
        self.conn = self._connect()
        try:
            self.scp.get(file, recursive=r)
        except SCPException as e:
            log.error(f'File transfer error: {e}')
            raise e
        log.info(f'Pulled {self.remote_path}/{file} from {self.host}')
示例#37
0
from userpass import Userpass
import os, sys

if len(sys.argv) < 3:
    print "usage: {} <path_to_authfile> <router>".format(sys.argv[0])
    exit()

#create ssh handler and read in keys
ssh = SSHClient()
keypath = os.path.expanduser('~/.ssh/known_hosts')
ssh.load_host_keys(keypath)

#load userpass file
userpass = Userpass(sys.argv[1])

#connect
host = sys.argv[2]
ssh.connect( host, username=userpass.user, password=userpass.passwd )

stdin, stdout, stderr = ssh.exec_command( 'show version' )
for line in stdout:
  print '... ' + line.strip('\n')

print "==="

stdin, stdout, stderr = ssh.exec_command( 'show chassis hardware' )
for line in stdout:
  print '... ' + line.strip('\n')

ssh.close()
示例#38
0
# local_path = './video_org/delivery_place_delivery.avi'
# remote_path = '/home/aucsie07/aucsie01_backup/webcam/video_org/delivery_place_delivery.avi'

# local_path = './video_org/retailer_sell.avi'
# remote_path = '/home/aucsie07/aucsie01_backup/webcam/video_org/retailer_sell.avi'

local_path = './video_org/buyer_buy.avi'
remote_path = '/home/aucsie07/aucsie01_backup/webcam/video_org/buyer_buy.avi'

out = cv2.VideoWriter(local_path, fourcc, fps, frame_size_tuple)
#mp4 cannot work
# out = cv2.VideoWriter('./video_org/output_yieldy1.mp4', fourcc, 1, (640, 360))

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect('192.168.1.133', 22, 'aucsie07', '1234')
scp = SCPClient(ssh.get_transport())

stage_task = [
    'adjust_brightness',
]

mqtt_service_channel = [
    "sensor_login",
    "sensor_data",
    "sensor_logout",
]


# Called when connect to Broker
def on_VLCConnect(client, userdata, flags, rc):
from paramiko import SSHClient
from scp import SCPClient
import sys
from string import ascii_lowercase

# Define progress callback that prints the current percentage completed for the file
def progress(filename, size, sent):
    sys.stdout.write("%s\'s progress: %.2f%%   \r" % (filename, float(sent)/float(size)*100) )

for i in ['a', 'b', 'c']:
	for j in ascii_lowercase:
		print(i, j)
		if not (i=='a'): #and (j in ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])):
			ssh = SSHClient()
			ssh.load_system_host_keys()
			ssh.connect(hostname='datascience5.cs.man.ac.uk', username='******', password='******')
			scp = SCPClient(ssh.get_transport(), progress=progress)
			
			print('project_dataset.zip.part'+i+j)
			
			try:
				scp.put('/home/deadman/Uni/project/dataset/project_dataset.zip.part'+i+j, '/local/home/n82471zk/project_dataset/project_dataset.zip.part'+i+j)
			except:
				print('fail')
				scp.close()
				ssh.close()
				continue
				
				
			
			scp.close()
示例#40
0
"""
This file grabs a log from the roboRIO and copies it a local directory.

@author Arvind
"""

# Imports
from paramiko import SSHClient
from scp import SCPClient

# Declartion
ssh = SSHClient()

# Initialization
ssh.load_system_host_keys()
ssh.connect("172.22.11.2", "lvuser")

# Get log
with SCPClient(ssh.get_transport()) as scp:
    scp.get("~/log/test-log.log",
            "C:/Users/BUZZ-175/Development/RoboRIOLogs/test-log.log")
示例#41
0
#file_in = "make_prediction.py"
file_in = "predict.csv"
if (len(sys.argv) == 1):
    file_in = "../dataset/predict.csv"
else:
    file_in = sys.argv[1]

file_out = os.path.splitext(file_in)[0] + "Complet" + os.path.splitext(
    file_in)[1]

# on fait la connection SSH avec la machine AWS
ssh = SSHClient()
ssh.set_missing_host_key_policy(AutoAddPolicy())
ssh.connect("ec2-18-232-97-24.compute-1.amazonaws.com",
            username="******",
            key_filename="bigDataProject.pem")
#on ouvre la connection SFTP
ftp = ssh.open_sftp()

# on envoie le fichier
ftp.put(file_in, "files/" + os.path.basename(file_in))

# on execute le script
ssh.exec_command("python make_prediction.py files/" +
                 os.path.basename(file_in) + " files/" +
                 os.path.basename(file_out))

# on recupere le nouveau fichier
time.sleep(0.5)
示例#42
0
class SSH:
    def __init__(self,hostname, username):
        self._ssh = SSHClient()
        self._ssh.load_system_host_keys()
        self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self._ssh.connect(hostname=hostname, username=username, timeout=10)

    def __del__(self):
        """ Called when the instance is about to be destroyed. 
        The connection has to be closed here.
        """
        if self._ssh:
            self._ssh.close()


    def cmd_disk_usage(self):
        cmd_str = 'df -hT'
        stdin,stdout,stderr = self._ssh.exec_command(cmd_str)
        if stderr.channel.recv_exit_status() != 0:
            print(stderr.read())
        else:
            output_cmd = stdout.read()
            return df.parse(output_cmd.decode("utf-8"))

    def cmd_users_connected(self):
        cmd_str = 'who'
        stdin,stdout,stderr = self._ssh.exec_command(cmd_str)
        if stderr.channel.recv_exit_status() != 0:
            print(stderr.read())
        else:
            output_cmd = stdout.read()
            return who.parse(output_cmd.decode("utf-8"))


    def cmd_proccess_running(self):
        cmd_str = 'ps -aux'
        stdin,stdout,stderr = self._ssh.exec_command(cmd_str)
        if stderr.channel.recv_exit_status() != 0:
            print(stderr.read())
        else:
            output_cmd = stdout.read()
            return ps.parse(output_cmd.decode("utf-8"))

    def cmd_cpu_usage(self):
        cmd_str = "top -b -n 2 -d1 | grep \"Cpu(s)\" | awk '{print $2}' | awk -F. '{print $1}'"
        stdin,stdout,stderr = self._ssh.exec_command(cmd_str)
        if stderr.channel.recv_exit_status() != 0:
            print(stderr.read())
        else:
            output_cmd = stdout.readlines()
            output_for_format = []
            for index, out in enumerate(output_cmd, start=0):
                cpu_name = 'cpu_' + str(index)
                output_for_format.append({'cpu': cpu_name ,'value_percent': out.replace('\n','')})
        return output_for_format


    def mem_usage(self):
        cmd_str = "free | grep Mem |awk '{print $3/$2 * 100.0}'"
        stdin,stdout,stderr = self._ssh.exec_command(cmd_str)
        if stderr.channel.recv_exit_status() != 0:
            print(stderr.read())
        else:
            output_cmd = stdout.read().decode("utf-8")
            return {'mem_usage_percent': output_cmd.replace('\n','')}


    def exec_cmd(self,cmd):
        stdin,stdout,stderr = self._ssh.exec_command(cmd)
        if stderr.channel.recv_exit_status() != 0:
            print(stderr.read())
        else:
            return stdout.read()
示例#43
0
warnings.filterwarnings("ignore")

jsonpost = {'token_auth': creds, 'file_version': '2'}

result = requests.post(url, data=json.dumps(jsonpost))
web_data = json.loads(result.text)
web_data_final = result.text
split_web_data = web_data_final.splitlines()

alertBaseFile = 'AlertBase-CVP.json'
logFile = 'bugalertUpdate.log'

ssh = SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(cvp, username="******", password=rootpw)
ssh_shell = ssh.invoke_shell()
ssh_shell.send("rpm -qi cvp-base\n")
time.sleep(2)
output = ssh_shell.recv(8000)
output = output.decode("utf-8")
output = remove_last_line_from_string(output)
output = remove_last_line_from_string(output)
output = remove_last_line_from_string(output)
output = output[(output.index('Name')):]
Dict = dict(
    (x.strip(), y.strip())
    for x, y in (element.split(': ') for element in output.split('\r\n')))
cvp_main_version = Dict["Version"][0:4]

log = open(logFile, 'a+')
示例#44
0
    lines = f.readlines()
    server_ips = [l.strip().split(":") for l in lines]
    sorted_ips = natsorted(server_ips, key=operator.itemgetter(0))
    print(sorted_ips)
    # For first server, send over dummy json, (can do it in here)
    # Retrieve {server_name}suffix from the server, via SCP. Store string data,
    # send it over to the next server via SCP. And so on
    ssh = SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_system_host_keys()
    counter = 0
    prevKey = None
    for server in sorted_ips:
        name = server[0]
        ip = server[1]
        ssh.connect(ip, username="******")
        print("Connected to", name, "with ip", server)
        scp = SCPClient(ssh.get_transport(), progress=progress)
        if name == "cepa0":
            fl = io.BytesIO()
            fl.write(b'test')
            fl.seek(0)
            print("Put dummy key")
            scp.putfo(fl, repo_dir + dummy)
        else:
            # Push the last server key
            prev_server_key = prefix + str(counter - 1) + suffix
            print("Put " + prev_server_key)
            scp.put(prev_server_key, input_key) # Catch exception
            ssh.exec_command("mv " + input_key + " " + repo_dir)
示例#45
0
class SecureShell(AbstractRemoteShell):
    def __init__(self,
                 hostname,
                 username,
                 password=None,
                 port=22,
                 check_xc=False,
                 check_err=False,
                 wait=True,
                 log_level=CRITICAL,
                 **kwargs):
        super(SecureShell, self).__init__(hostname,
                                          check_xc=check_xc,
                                          check_err=check_err,
                                          wait=wait,
                                          log_level=log_level,
                                          **kwargs)
        self._hostname = hostname
        self._port = port
        self._username = username
        self._password = password
        self.connect()

    def do_connect(self):
        self._client = SSHClient()
        self._client.load_system_host_keys()
        self._client.set_missing_host_key_policy(AutoAddPolicy())
        self._client.connect(hostname=self._hostname,
                             port=self._port,
                             username=self._username,
                             password=self._password)
        self._scp_client = SCPClient(self._client.get_transport())

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

    def execute_command(self,
                        command,
                        env={},
                        wait=True,
                        check_err=False,
                        cwd=None):
        for var, val in env.items():
            command = "%s=%s; " % (var, val) + command
        chan = self._client.get_transport().open_session()
        chan.exec_command((("cd \"%s\"; " % cwd) if cwd else "") + command)
        queue = Queue()
        StandardStreamReader(chan.makefile("r"), 1, queue)
        StandardStreamReader(chan.makefile_stderr("r"), 2, queue)

        def post_process_exit_code():
            queue.put((0, chan.recv_exit_status()))
            queue.put((0, None))

        Thread(target=post_process_exit_code).start()
        return ShellResult(self, command, queue, wait, check_err)

    def do_pull(self, local_path, remote_path):
        self._scp_client.get(remote_path, local_path)

    def do_push(self, local_path, remote_path):
        self._scp_client.put(local_path, remote_path)

    def do_reboot(self):
        self("reboot > /dev/null 2>&1 &")
        sleep(.3)
示例#46
0
class App:
    def __init__(self):
        self.root = tk.Tk()
        self.root.geometry("300x100")
        self.root.title('bridge utility')
        self.startCarBtn = tk.Button(self.root,
                                     text="start with car config",
                                     command=self.startCar)
        self.startCarBtn.pack()
        self.startToolBtn = tk.Button(self.root,
                                      text="start with tool config",
                                      command=self.startTool)
        self.checkToolBtn.pack()
        self.checkToolBtn = tk.Button(self.root,
                                      text="check tool status",
                                      command=self.checkTool)
        self.startToolBtn.pack()
        self.stopBtn = tk.Button(self.root, text="start", command=self.stop)
        self.stopBtn['state'] = 'disabled'
        self.stopBtn.pack()
        self.ssh = SSHClient()
        self.ssh.set_missing_host_key_policy(AutoAddPolicy)

    def startCar(self):
        self.ssh.connect(hostname=ssh_ip,
                         username=ssh_uname,
                         password=ssh_pass)
        self.ssh.exec_command(
            '/usr/bin/python3.8 dev/node-can-bridge/enable_can.py')
        self.ssh.exec_command(
            f'/home/ubuntu/.nvm/versions/node/v10.20.1/bin/node dev/node-can-bridge/client.js car',
            get_pty=True)
        self.stopBtn['state'] = 'enabled'
        self.startCarBtn['state'] = 'disabled'
        self.startToolBtn['state'] = 'disabled'

    def startTool(self):
        self.ssh.connect(hostname=ssh_ip,
                         username=ssh_uname,
                         password=ssh_pass)
        self.ssh.exec_command(
            '/usr/bin/python3.8 dev/node-can-bridge/enable_can.py')
        self.ssh.exec_command(
            f'/home/ubuntu/.nvm/versions/node/v10.20.1/bin/node dev/node-can-bridge/client.js tool',
            get_pty=True)
        self.stopBtn['state'] = 'enabled'
        self.startCarBtn['state'] = 'disabled'
        self.startToolBtn['state'] = 'disabled'

    def checkTool(self):
        self.ssh.connect(hostname=ssh_ip,
                         username=ssh_uname,
                         password=ssh_pass)
        self.ssh.exec_command(
            '/usr/bin/python3.8 dev/node-can-bridge/check.py')

    def stop(self):
        self.ssh.close()
        self.ssh = SSHClient()
        self.stopBtn['state'] = 'disabled'
        self.startCarBtn['state'] = 'enabled'
        self.startToolBtn['state'] = 'enabled'
示例#47
0
    def handle(self, *args, **options):
        """
        Gets the appropriate settings from Django and deploys the repository
        to the target servers for the instance selected.
        """
        # Grab the quiet settings and unique stamp
        quiet = options["quiet"]
        stamp = options["stamp"]
        no_confirm = options["no_confirm"]

        # Check to ensure the require setting is in Django's settings.
        if hasattr(settings, "DEPLOYER_INSTANCES"):
            instances = settings.DEPLOYER_INSTANCES
        else:
            raise CommandError(
                "You have not configured DEPLOYER_INSTANCES in your Django settings."
            )

        # Grab the instance settings if they're properly set
        if options["instance"] in instances:
            instance = instances[options["instance"]]
        else:
            raise CommandError(
                "The instance name you provided ('{instance}') is not configured in "
                "your settings DEPLOYER_INSTANCES. Valid instance names are: "
                "{instances}".format(
                    instance=options["instance"],
                    instances=", ".join(list(instances.keys())),
                ))

        print(
            "We are about to deploy the instance '{instance}' to the following "
            "servers: {servers}.".format(instance=options["instance"],
                                         servers=", ".join(
                                             instance["servers"])))
        if no_confirm:
            verify = "yes"
        else:
            verify = input(
                "Are you sure you want to do this (enter 'yes' to proceed)? ")

        if verify.lower() != "yes":
            print("You did not type 'yes' - aborting.")
            return

        # Variables for directory locations and symlinks
        deployer_clone_dir_format = getattr(
            settings,
            "DEPLOYER_CLONE_DIR_FORMAT",
            "{name}-{instance}",
        )
        deployer_clone_dir = deployer_clone_dir_format.format(
            instance=options["instance"],
            name=instance["name"],
            branch=instance["branch"],
            server_user=instance["server_user"],
        )
        git_dir_stamp = "{deployer_clone_dir}-{stamp}".format(
            deployer_clone_dir=deployer_clone_dir, stamp=stamp)
        install_code_path = os.path.join(
            instance["code_path"],
            deployer_clone_dir,
        )
        install_code_path_stamp = os.path.join(
            instance["code_path"],
            git_dir_stamp,
        )

        # On our first iteration, we clone the repository, build the virtual
        # environment, and collect static files; this may take some time.
        for index, server in enumerate(instance["servers"]):
            # Open an SSH connection to the server
            ssh = SSHClient()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            ssh.connect(server, username=instance["server_user"])

            print("Cloning code and preparing venv on node: {}...".format(
                server))

            # Make sure the code_path directory exists
            stdin, stdout, stderr = ssh.exec_command("""
                mkdir -p {directory}
                """.format(directory=instance["code_path"]))
            self.command_output(stdout, stderr, quiet)

            stdin, stdout, stderr = ssh.exec_command("""
                cd {code_path}
                git clone --recursive --verbose -b {branch} {repository} {git_dir_stamp}
                """.format(
                code_path=instance["code_path"],
                branch=instance["branch"],
                repository=instance["repository"],
                git_dir_stamp=git_dir_stamp,
            ))
            self.command_output(stdout, stderr, quiet)

            print("Preparing the installation...")

            if instance.get("upgrade_pip", True):
                pip_upgrade_text = "pip install -U pip"
            else:
                pip_upgrade_text = ""
                print("pip will NOT be upgraded.")

            if instance.get("collectstatic", True):
                collectstatic_text = (
                    "python manage.py collectstatic --noinput --settings={s}".
                    format(s=settings, ))
            else:
                collectstatic_text = ""
                print("Static files will NOT be collected.")

            stdin, stdout, stderr = ssh.exec_command("""
                cd {install_code_path_stamp}
                {venv_python_path} -m venv venv
                . venv/bin/activate
                {pip_upgrade_text}
                pip install -U wheel
                pip install --ignore-installed -r {requirements}
                {collectstatic_text}
                """.format(
                install_code_path_stamp=install_code_path_stamp,
                venv_python_path=instance["venv_python_path"],
                pip_upgrade_text=pip_upgrade_text,
                requirements=instance["requirements"],
                collectstatic_text=collectstatic_text,
            ))
            self.command_output(stdout, stderr, quiet)

            if "selinux" in instance and instance["selinux"]:
                print(
                    "Setting security context for RedHat / CentOS SELinux...")

                stdin, stdout, stderr = ssh.exec_command("""
                    chcon -Rv --type=httpd_sys_content_t {install_code_path_stamp} > /dev/null
                    find {install_code_path_stamp}/venv/ \( -name "*.so" -o -name "*.so.*" \) -exec chcon -Rv --type=httpd_sys_script_exec_t {{}} \; > /dev/null
                    """.format(
                    install_code_path_stamp=install_code_path_stamp))
                self.command_output(stdout, stderr, quiet)

        # On our second iteration, we update symlinks, keep only recent deployments,
        # run any additional commands, and run migrations. This should be fast.
        for index, server in enumerate(instance["servers"]):
            # Open an SSH connection to the server
            ssh = SSHClient()
            ssh.set_missing_host_key_policy(AutoAddPolicy())
            ssh.connect(server, username=instance["server_user"])

            print("")
            print(
                "Updating symlinks and running any additional defined commands on "
                "node: {}...".format(server))

            stdin, stdout, stderr = ssh.exec_command("""
                ln -sfn {install_code_path_stamp} {install_code_path}
                """.format(
                install_code_path_stamp=install_code_path_stamp,
                install_code_path=install_code_path,
            ))

            self.command_output(stdout, stderr, quiet)

            if int(instance.get("save_deploys", 0)) > 0:
                print(
                    "Keeping the {} most recent deployments, and deleting the rest on "
                    "node: {}".format(instance["save_deploys"], server))

                stdin, stdout, stderr = ssh.exec_command("""
                    ls -1trd {install_code_path}* | head -n -{save_deploys} | xargs -d '\\n' rm -rf --
                    """.format(
                    install_code_path=install_code_path,
                    save_deploys=instance["save_deploys"] + 1,
                ))
                self.command_output(stdout, stderr, quiet)

            if "additional_commands" in instance:
                print("Performing defined additional commands...")
                for additional_command in instance["additional_commands"]:
                    print("Running '{}'...".format(additional_command))
                    stdin, stdout, stderr = ssh.exec_command(
                        additional_command)
                    self.command_output(stdout, stderr, quiet)

            # Only run migrations when we're on the last server.
            if index + 1 == len(instance["servers"]) and instance.get(
                    "migrate", True):
                print("Finally, running migrations...")
                stdin, stdout, stderr = ssh.exec_command("""
                    cd {install_code_path_stamp}
                    . venv/bin/activate
                    python manage.py migrate --noinput --settings={settings}
                    """.format(
                    install_code_path_stamp=install_code_path_stamp,
                    settings=instance["settings"],
                ))
                self.command_output(stdout, stderr, quiet)
            else:
                print("Not running migrations; migrate is set to False.")

        print("All done!")
示例#48
0
class SshClient(object):
    """
    @Desc : SSH Library for Marvin.
    Facilitates SSH,SCP services to marvin users
    @Input: host: Host to connect
            port: port on host to connect
            user: Username to be used for connecting
            passwd: Password for connection
            retries and delay applies for establishing connection
            timeout : Applies while executing command
    """

    def __init__(self, host, port, user, password, retries=60, delay=10, key_pair_files=None, timeout=10.0):
        self.host = None
        self.port = 22
        self.user = user
        self.passwd = password
        self.keyPairFiles = key_pair_files
        self.ssh = SSHClient()
        self.ssh.set_missing_host_key_policy(AutoAddPolicy())
        self.retryCnt = 0
        self.delay = 0
        self.timeout = 3.0
        self.logger = MarvinLog('ssh').get_logger()

        # Check invalid host value and raise exception
        # At least host is required for connection
        if host is not None and host != '':
            self.host = host
        if retries is not None and retries > 0:
            self.retryCnt = retries
        if delay is not None and delay > 0:
            self.delay = delay
        if timeout is not None and timeout > 0:
            self.timeout = timeout
        if port is not None and port >= 0:
            self.port = port
        if self.create_connection() == FAILED:
            raise internalError("Connection Failed")

    def execute(self, command):
        stdin, stdout, stderr = self.ssh.exec_command(command)
        output = stdout.readlines()
        errors = stderr.readlines()
        results = []
        if output is not None and len(output) == 0:
            if errors is not None and len(errors) > 0:
                for error in errors:
                    results.append(error.rstrip())

        else:
            for strOut in output:
                results.append(strOut.rstrip())
        self.logger.debug("Executing command via host %s: %s Output: %s" % (str(self.host), command, results))
        return results

    def create_connection(self):
        """
        @Name: createConnection
        @Desc: Creates an ssh connection for
               retries mentioned,along with sleep mentioned
        @Output: SUCCESS on successful connection
                 FAILED If connection through ssh failed
        """
        ret = FAILED
        while self.retryCnt >= 0:
            try:
                self.logger.debug("Trying SSH Connection to host %s on port %s as user %s. RetryCount: %s" %
                                 (self.host, str(self.port), self.user, str(self.retryCnt)))
                if self.keyPairFiles is None:
                    self.ssh.connect(hostname=self.host,
                                     port=self.port,
                                     username=self.user,
                                     password=self.passwd,
                                     timeout=self.timeout)
                else:
                    self.ssh.connect(hostname=self.host,
                                     port=self.port,
                                     username=self.user,
                                     password=self.passwd,
                                     key_filename=self.keyPairFiles,
                                     timeout=self.timeout,
                                     look_for_keys=False
                                     )
                self.logger.debug("Connection to host %s on port %s is SUCCESSFUL" % (str(self.host), str(self.port)))
                ret = SUCCESS
                break
            except BadHostKeyException as e:
                self.logger.debug("Failed to create connection: %s" % e)
            except AuthenticationException as e:
                self.logger.debug("Failed to create connection: %s" % e)
            except SSHException as e:
                self.logger.debug("Failed to create connection: %s" % e)
            except socket.error as e:
                self.logger.debug("Failed to create connection: %s" % e)
            except Exception as e:
                self.logger.debug("Failed to create connection: %s" % e)
            finally:
                if self.retryCnt == 0 or ret == SUCCESS:
                    break
                self.retryCnt -= 1
                time.sleep(self.delay)
        return ret

    def run_command(self, command):
        """
        @Name: runCommand
        @Desc: Runs a command over ssh and
               returns the result along with status code
        @Input: command to execute
        @Output: 1: status of command executed.
                 SUCCESS : If command execution is successful
                 FAILED    : If command execution has failed
                 2: stdin,stdout,stderr values of command output
        """
        ret = {"status": FAILED, "stdin": None, "stdout": None,
               "stderr": INVALID_INPUT}
        if command is None or command == '':
            return ret
        try:
            stdin, stdout, stderr = self.ssh.exec_command(command, timeout=self.timeout)
            if stdout is not None:
                status_check = stdout.channel.recv_exit_status()
                if status_check == 0:
                    ret["status"] = SUCCESS
                ret["stdout"] = stdout.readlines()
                if stderr is not None:
                    ret["stderr"] = stderr.readlines()
        except Exception as e:
            self.logger.debug("Failed to run command: %s" % e)
        finally:
            self.logger.debug("Connection to host %s on port %s is SUCCESSFUL" % (str(self.host), command))
            return ret

    def scp(self, src_file, dest_path):
        transport = Transport((self.host, int(self.port)))
        transport.connect(username=self.user, password=self.passwd)
        sftp = SFTPClient.from_transport(transport)
        try:
            sftp.put(src_file, dest_path)
        except IOError as e:
            raise e

    def __del__(self):
        self.close()

    def close(self):
        if self.ssh is not None:
            self.ssh.close()
            self.ssh = None
示例#49
0
from paramiko import SSHClient
from scp import SCPClient
import paramiko
import time


host = '192.168.1.2'
user = '******'
secret = '12345678'
port = 22
localpath = 'C:/scripts/Ubnt/car/system.cfg'
#localpath1 = 'C:/scripts/Ubnt/default.cfg'
remotepath = '/tmp/'
ssh = SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
    ssh.connect(hostname=host, username=user, password=secret, port=port)
    with SCPClient(ssh.get_transport()) as scp:
        scp.put(localpath, remote_path=remotepath)
#        scp.get(remote_path='/etc/default.cfg', local_path=localpath1)
        stdin, stdout, stderr = ssh.exec_command('cfgmtd -w -f /tmp/system.cfg')
        time.sleep(5)
        ssh.exec_command('reboot')
        data = stdout.read() + stderr.read()
except paramiko.SSHException:
    print("Connection Failed")
    quit()
print(data)
import random
import sys
import os
import select
import paramiko
from paramiko import SSHClient
import subprocess
import time

client_master = SSHClient()
client_master.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client_master.load_system_host_keys()
client_master.connect("10.10.1.89", username="******", password="******")
client_slave1 = SSHClient()
client_slave1.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client_slave1.load_system_host_keys()
client_slave1.connect("10.10.1.90", username="******", password="******")
client_slave2 = SSHClient()
client_slave2.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client_slave2.load_system_host_keys()
client_slave2.connect("10.10.1.51", username="******", password="******")


def write_stop(file, diff, misconfig_code, misconfig_node, client, write_dir):
    file_lines = []
    ftp = client.open_sftp()
    remote_file = ftp.open("/home/ubuntu/Neha_Shreya/" + file)
    for line in remote_file:
        file_lines.append(line)
    file_lines.append("TIME:" + str(diff) + ",CODE:" + str(misconfig_code) +
                      "\n")
示例#51
0
from paramiko import SSHClient
client = SSHClient()
client.load_system_host_keys()
client.connect("hostname", username="******")
stdin, stdout, stderr = client.exec_command('program')
print "stderr: ", stderr.readlines()
print "pwd: ", stdout.readlines()
示例#52
0
port = int((config.get("ssh", "port")))
localPath = (config.get("targets", "localdir"))
remotePath = (config.get("targets", "remotedir"))

keyCheck = config.has_option("ssh", "keyfile")
passCheck = config.has_option("ssh", "password")

client = SSHClient()
client.load_system_host_keys()

if keyCheck == True and passCheck == False:
    print("Connecting to " + host)
    print("Authenticating with key...")
    print("\n")
    key = (config.get("ssh", "keyfile"))
    client.connect(host, username=user, key_filename=key, port=port)

elif passCheck == True and keyCheck == False:
    print("Connecting to " + host)
    print("Authenticating with password...")
    print("\n")
    passwd = (config.get("ssh", "password"))
    client.connect(host, username=user, password=passwd, port=port)

elif passCheck == True and keyCheck == True:
    sys.exit(
        "Cannot authenticate. Key and password both provided. Check RetroSort.conf and try again"
    )

else:
    sys.exit("No authentication provided. Check RetroSort.conf and try again")
示例#53
0
    def verify_connection_raw(self, node_sndr, node_rcvl, counter):

        # Data required to  verify connection
        # On sender side: Interface to Send packet, Destination IP, Source IP, Destination MAC
        # On receiver side: Interface to listen for tcpdump
        # print device_w_con_problem
        # print node_sndr + '  ' + node_rcvl
        try:
            try:
                if [node_sndr, node_rcvl] in device_w_con_problem or [
                        node_rcvl, node_sndr
                ] in device_w_con_problem:
                    sys.exit()
                cl_s = SSHClient()
                cl_s.set_missing_host_key_policy(AutoAddPolicy())
                cl_s.connect(dev_connect[node_sndr][0],
                             port=int(dev_connect[node_sndr][1]),
                             username='******',
                             password='******')

                cl_r = SSHClient()
                cl_r.set_missing_host_key_policy(AutoAddPolicy())
                cl_r.connect(dev_connect[node_rcvl][0],
                             port=int(dev_connect[node_rcvl][1]),
                             username='******',
                             password='******')
                # print 'dev sender: {0}, dev receiver {1}'.format(dev_connect[node_sndr][1], dev_connect[node_rcvl][1])
            except:
                pass
            for el in hosts_w_macs[node_sndr].keys():
                if el in hosts_w_macs[node_rcvl].keys():
                    link = el

            exec_command_r = 'sudo tcpdump -i {0} -c 10 '.format(
                hosts_w_macs[node_rcvl][link][0])
            cl_r.exec_command('sudo ifconfig {0} up'.format(
                hosts_w_macs[node_rcvl][link][0]))
            # print 'node is {0}, receive command is {1}'.format(hosts_w_macs[node_rcvl], exec_command_r)

            stdin_r, stdout_r, stderr_r = cl_r.exec_command(exec_command_r)

            stdin_s, stdout_s, stderr_s = \
                cl_s.exec_command('sudo ip addr flush dev {0}'.format(hosts_w_macs[node_sndr][link][0]))
            cl_s.exec_command('sudo ifconfig {0} up'.format(
                hosts_w_macs[node_sndr][link][0]))
            cl_s.exec_command('sudo chmod 755 scripts/send-raw')

            exec_command_s = 'sudo ./scripts/send-raw -i {0} -s {1} -d {2} -m {3}' \
                .format(hosts_w_macs[node_sndr][link][0], '255.255.255.254', '255.255.255.255',
                        hosts_w_macs[node_rcvl][link][1])

            # print 'node is {0}, send command is \n {1}'.format(hosts_w_macs[node_sndr], exec_command_s)

            for k in range(0, 30):

                cl_s.exec_command(exec_command_s)
                k += 1

            # Debug section
            # print 'Link between devices is ' + link
            # print 'sudo ifconfig {0} up'.format(hosts_w_macs[node_sndr][link][0])
            # print 'sudo ip addr flush dev {0}'.format(hosts_w_macs[node_sndr][link][0])
            # print exec_command_s
            # print exec_command_r

            if len([
                    m.start() for m in re.finditer(
                        'IP 255.255.255.254 > 255.255.255.255: ICMP echo request',
                        stdout_r.read())
            ]) >= 5:
                print '   Link  {0} --->  {1} '.format(
                    node_sndr, node_rcvl) + u'\u2713'.encode('utf8')
                # print 'Link  {0} --->  {1} established'.format(node_sndr, node_rcvl)
            else:
                print 'Sorry, there is some problem'
                device_w_con_problem.append([node_sndr, node_rcvl])

            cl_s.close()
            cl_r.close()
        except:
            # print '{0} try'.format(counter)
            counter += 1
            if counter < 5:
                self.verify_connection_raw(node_sndr, node_rcvl, counter)
            else:
                if [node_rcvl, node_sndr] not in device_w_con_problem:
                    device_w_con_problem.append([node_sndr, node_rcvl])
                print "There was 3 tries to establish link. Moving on"
            pass
示例#54
0
from jobengine import clusters
from paramiko import SSHClient
from scp import SCPClient, SCPException

#cluster, shell = clusters.Clusters().get_cluster("arcus-gpu")
cluster = clusters.Clusters().clusters["arcus-gpu"]()
print cluster

if False:

    ssh = SSHClient()
    ssh.load_system_host_keys()
    print cluster.hostname
    ssh.connect(cluster.hostname, username=cluster.username)

if True:
    shell = cluster.connect()

scp = SCPClient(shell.get_transport())
scp.put("dummy", "/tmp", recursive=True)
示例#55
0
from paramiko import SSHClient
from getpass import getpass

message = input("Enter message: ")
user_name = input("Username: "******"ssh.ocf.berkeley.edu", username=user_name, password=passwd)
sftp = ssh.open_sftp()
sftp.put("sam_was_here.txt", "sam_was_here.txt")
sftp.close()
ssh.exec_command("echo {0} > message.txt".format(message))
ssh.close()
示例#56
0
    def verify_connection_udp(self):
        fine_links = []
        # print 'dev con'
        # print dev_connect
        # Netmask:   255.255.255.252 = 30
        # Network:   10.0.0.220/30
        # Broadcast: 10.0.0.223
        # HostMin:   10.0.0.221
        # HostMax:   10.0.0.222
        # Hosts/Net: 2
        # print self.graph_nx.adj

        for k, v in self.adj_with_names.iteritems():
            """
            Every node can be connected to few nodes.
            So we need to verify nested loop over here. v - ['server1', 'server2']
            """
            # print k, v
            for l in v:
                receiver_id = find(topo_yaml['nodes'], 'name', l)
                receiver = topo_yaml['nodes'][receiver_id]
                sender_id = find(topo_yaml['nodes'], 'name', k)
                sender = topo_yaml['nodes'][sender_id]
                link_num = find_connected_link(receiver, sender)
                # print receiver['interfaces']
                # print link_num
                receiver_dev = find(receiver['interfaces'], 'link-name',
                                    link_num)
                # print receiver_dev
                receiver_interface = topo_yaml['nodes'][receiver_id][
                    'interfaces'][0]['interface']

                # print dev_connect[l]
                # print 'connected_link {0}'.format(link_num)

                udp_receiv = SSHClient()
                udp_receiv.set_missing_host_key_policy(AutoAddPolicy())
                udp_receiv.connect(dev_connect[l][0],
                                   port=int(dev_connect[l][1]),
                                   username='******',
                                   password='******')

                input_command = 'ifconfig {0}'.format(receiver_interface). \
                    replace('GigabitEthernet', 'Gi').replace('/', '_')
                s_in, s_out, s_err = udp_receiv.exec_command(input_command)
                # print 'input ' + input_command

                # print s_out.read()
                receiver_ip = re.search(r'inet addr:(\S+)',
                                        s_out.read()).group(1)

                # print "\n*** DEBug Message\n" \
                #       "dev sender {0} connection {1} \n" \
                #       "dev receiver {2} connection {3}  \n" \
                #       "connected link {4}\n" \
                #       "Receiver interface - {5}  IP - {6}\n" \
                #       "***".format(sender['name'], dev_connect[k],
                #                    receiver['name'], dev_connect[l],
                #                    link_num, receiver_interface, receiver_ip)

                r_in, r_out, r_err = udp_receiv.exec_command(
                    'python ~/scripts/sock_receiver.py ' + receiver_ip)

                udp_sender = SSHClient()
                udp_sender.set_missing_host_key_policy(AutoAddPolicy())
                udp_sender.connect(dev_connect[k][0],
                                   port=int(dev_connect[k][1]),
                                   username='******',
                                   password='******')
                s_in, s_out, s_err = udp_sender.exec_command(
                    'python ~/scripts/sock_sender.py ' + receiver_ip)
                if 'Checking connectivity' in r_out.read():
                    fine_links.append(sender['name'] + ' ' + receiver['name'])
                    # print s_out.read()
        print 'UDP Connection verified for following links:'
        for f_l in fine_links:
            print f_l + u' \u2713'.encode('utf8')
示例#57
0
class S_Host(object):
    def __init__(self, host="127.0.0.1", password="", user="******", port=22, key_file=""):
        self.host = host
        self.user = user
        self.password = password
        self.port = port
        self.key_file = key_file

    def do_connect(self):
        self.ssh = SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        if self.key_file:
            self.ssh.connect(username=self.user, password=self.password, port=self.port, hostname=self.host, key_filename=self.key_file)
        else:
            self.ssh.connect(username=self.user, password=self.password, port=self.port, hostname=self.host)
        self.scp = SCPClient(self.ssh.get_transport())
        return True

    def do_disconnect(self):
        try:
            self.scp.close()
            self.ssh.close()
        finally:
            return True
    def do_put(self, source_file, target_file, single_call=True):
        try:
            if single_call:
                self.do_connect()
            if os.path.exists(source_file):
                self.scp.put(source_file, target_file)
            else:
                raise ValueError("Source file %s is not exists." % source_file, "")
        except Exception as e:
            print("Transport file from %s to %s failed." % (source_file, target_file))
            print("Error reasonn: %s" % e)
            return False
        finally:
            if single_call:
                self.do_disconnect()
        return True

    def do_get(self, source_file, target_file, single_call=True):
        try:
            if single_call:
                self.do_connect()
            if not os.path.isfile(target_file):
                self.scp.get(source_file, target_file)
            else:
                raise ValueError("Target file %s is exists." % target_file)
        except Exception as e:
            print("Transport file from %s to %s failed." % (source_file, target_file))
            print("Error reasonn: %s" % e)
            return False
        finally:
            if single_call:
                self.do_disconnect()
        return True

    def do_put_files(self, source_file_list, target_dir):
        if not hasattr(source_file_list, "__getitem__"):
            raise ValueError("Input value %s cannot iterable." % source_file_list)
        try:
            self.do_connect()
            for file in source_file_list:
                file_name = os.path.basename(file)
                target_file_path = os.path.join(target_dir, file_name)
                try:
                    self.do_put(file, target_file_path, single_call=False)
                except:
                    continue
        finally:
            self.do_disconnect()
        return True

    def do_get_files(self, source_file_list, target_dir):
        pass
        if not hasattr(source_file_list, "__getitem__"):
            raise ValueError("Input value %s cannot iterable." % source_file_list)
        try:
            self.do_connect()
            for file in source_file_list:
                file_name = os.path.basename(file)
                target_file_path = os.path.join(target_dir, file_name)
                try:
                    self.do_get(file, target_file_path, single_call=False)
                except:
                    continue
        finally:
            self.do_disconnect()
        return True
示例#58
0
class RemoteClient:
    """Client to interact with a remote host via SSH & SCP."""
    def __init__(self, host, user, ssh_key_filepath, remote_path):
        self.host = host
        self.user = user
        self.ssh_key_filepath = ssh_key_filepath
        self.remote_path = remote_path
        self.client = None
        self.scp = None
        self.conn = None
        self.__upload_ssh_key()

    def __get_ssh_key(self):
        """
        Fetch locally stored SSH key.
        """
        try:
            self.ssh_key = RSAKey.from_private_key_file(self.ssh_key_filepath)
            logger.info(f'Found SSH key at self {self.ssh_key_filepath}')
        except SSHException as error:
            logger.error(error)
        return self.ssh_key

    def __upload_ssh_key(self):
        try:
            system(
                f'ssh-copy-id -i {self.ssh_key_filepath} {self.user}@{self.host}>/dev/null 2>&1'
            )
            system(
                f'ssh-copy-id -i {self.ssh_key_filepath}.pub {self.user}@{self.host}>/dev/null 2>&1'
            )
            logger.info(f'{self.ssh_key_filepath} uploaded to {self.host}')
        except FileNotFoundError as error:
            logger.error(error)

    def __connect(self):
        """
        Open connection to remote host.
        """
        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,
                                key_filename=self.ssh_key_filepath,
                                look_for_keys=True,
                                timeout=5000)
            self.scp = SCPClient(self.client.get_transport())
        except AuthenticationException as error:
            logger.info(
                'Authentication failed: did you remember to create an SSH key?'
            )
            logger.error(error)
            raise error
        finally:
            return self.client

    def disconnect(self):
        """
        Close ssh connection.
        """
        self.client.close()
        self.scp.close()

    def bulk_upload(self, files):
        """
        Upload multiple files to a remote directory.

        :param files: List of strings representing file paths to local files.
        """
        if self.client is None:
            self.client = self.__connect()
        uploads = [self.__upload_single_file(file) for file in files]
        logger.info(
            f'Finished uploading {len(uploads)} files to {self.remote_path} on {self.host}'
        )

    def __upload_single_file(self, file):
        """Upload a single file to a remote directory."""
        try:
            self.scp.put(file, recursive=True, remote_path=self.remote_path)
        except SCPException as error:
            logger.error(error)
            raise error
        finally:
            logger.info(f'Uploaded {file} to {self.remote_path}')

    def download_file(self, file):
        """Download file from remote host."""
        if self.conn is None:
            self.conn = self.connect()
        self.scp.get(file)

    def execute_commands(self, commands):
        """
        Execute multiple commands in succession.

        :param commands: List of unix commands as strings.
        """
        if self.client is None:
            self.client = self.__connect()
        for cmd in commands:
            stdin, stdout, stderr = self.client.exec_command(cmd)
            stdout.channel.recv_exit_status()
            response = stdout.readlines()
            for line in response:
                logger.info(f'INPUT: {cmd} | OUTPUT: {line}')
示例#59
0
class StashSSH(object):
    def __init__(self):
        self.ssh_running = False
        self.screen = pyte.screens.DiffScreen(100, 60)
        self.screen.dirty.clear()
        #self.screen.set_mode(pyte.modes.DECTCEM)
        self.stream = pyte.Stream()
        self.stream.attach(self.screen)
        self.pause_output = False

    def connect(self, host='', passwd=None, port=22):
        print 'Connecting...'
        self.user, self.host = self.parse_host(host)
        self.stash = globals()['_stash']
        self.passwd = passwd
        self.port = port
        self.ssh = SSHClient()
        self.ssh.set_missing_host_key_policy(AutoAddPolicy())
        try:
            print 'Looking for SSH keys...'
            self.ssh.connect(self.host,
                             username=self.user,
                             password=self.passwd,
                             port=self.port,
                             key_filename=self.find_ssh_keys())
        except:
            try:
                print 'No SSH key found. Trying password...'
                self.ssh.connect(self.host,
                                 username=self.user,
                                 password=self.passwd,
                                 port=self.port)
            except:
                print '*Auth Error*'
                return False
        self.ssh_running = True
        return True

    def find_ssh_keys(self):
        files = []
        APP_DIR = os.environ['STASH_ROOT']
        for file in os.listdir(APP_DIR + '/.ssh'):
            if '.' not in file:
                files.append(APP_DIR + '/.ssh/' + file)
        return files

    def parse_host(self, arg):
        user, host = arg.split('@')
        #host, path = temp.split(':')
        return user, host

    def stdout_thread(self):
        while self.ssh_running:
            if self.chan.recv_ready() and not self.pause_output:
                #output += self.chan.recv(1024)
                rcv = self.chan.recv(1024)
                self.stream.feed(u'%s' % rcv)
            if self.screen.dirty:
                self.screen.dirty.clear()
                self.update_screen()

    def update_screen(self):
        count = len(self.screen.display)
        for item in reversed(self.screen.display):
            if str(item) != ' ' * 100:
                break
            count -= 1
        text = '\n'.join(self.screen.display[:count]).rstrip() + ' '
        self.stash.term.truncate(0, flush=False)
        self.stash.term.write(text)

    def single_exec(self, command):
        sin, sout, serr = self.ssh.exec_command(command)
        for line in sout.readlines():
            line = line.replace('\n', '')
        for line in serr.readlines():
            print line.replace('\n', '')

    def get_remote_path(self):
        self.pause_output = True
        self.chan.send('pwd \n')
        while True:
            if self.chan.recv_ready():
                rcv = self.chan.recv(1024)
                #print ' '.join([str((ord(a),a)) for a in rcv])
                #print ' '.join([a for a in rcv])
                res = re.search(r'pwd ?\r\n(.*)\r\n', rcv)
                path = res.group(1)
                self.pause_output = False
                break
        return path + '/'

    def get_file(self, remote, local):
        sftp = self.ssh.open_sftp()
        path = self.get_remote_path()
        sftp.get(path + remote, local)
        sftp.close()
        print 'File transfered.'

    def put_file(self, local, remote):
        sftp = self.ssh.open_sftp()
        path = self.get_remote_path()
        sftp.put(local, path + remote)
        sftp.close()
        print 'File transfered.'

    def edit_file(self, remote_file):
        import tempfile
        import runpy
        import editor

        try:
            temp = tempfile.NamedTemporaryFile(
                dir=os.path.expanduser('~/Documents'), suffix='.py')
            cur_path = editor.get_path()
            sftp = self.ssh.open_sftp()
            path = self.get_remote_path()
            res = sftp.getfo(path + remote_file, temp)
            #editor.open_file(temp.name)
            temp.seek(0)
            print '***When you are finished editing the file, you must come back to console to confirm changes***'
            editor.open_file(temp.name)
            time.sleep(1.5)
            console.hide_output()
            input = raw_input('Save Changes? Y,N: ')
            editor.open_file(cur_path)
            if input == 'y' or input == 'Y':
                with open(temp.name, 'r') as f:
                    sftp.putfo(f, path + remote_file)
                    print 'File transfered.'
        except Exception, e:
            print e
        finally:
示例#60
0
文件: admin.py 项目: Ryuchen/CAPEv2
    files = list()

    if args.debug:
        logging.getLogger("paramiko.transport").setLevel(logging.DEBUG)

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

    if JUMP_BOX_USERNAME and args.jump_box:
        jumpbox_used = True

        jumpbox = SSHClient()

        jumpbox.set_missing_host_key_policy(AutoAddPolicy())
        jumpbox.connect(JUMP_BOX, username=JUMP_BOX_USERNAME, look_for_keys=True, allow_agent=True)
    if args.debug:
        log.setLevel(logging.DEBUG)

    if args.static_server_list:
        servers = SERVERS_STATIC_LIST
    else:
        try:
            http = urllib3.PoolManager()
            r = http.request("GET", CAPE_DIST_URL)
            if r.status == 200:
                res = json.loads(r.data.decode("utf-8")).get("nodes", [])
                servers = [res[server]["url"].split("://")[1].split(":")[0] for server in res] + [MASTER_NODE]
        except (urllib3.exceptions.NewConnectionError, urllib3.exceptions.MaxRetryError):
            sys.exit("Can't retrieve list of servers")
    if args.continues_integration: