當前位置: 首頁>>代碼示例>>Python>>正文


Python paramiko.Transport方法代碼示例

本文整理匯總了Python中paramiko.Transport方法的典型用法代碼示例。如果您正苦於以下問題:Python paramiko.Transport方法的具體用法?Python paramiko.Transport怎麽用?Python paramiko.Transport使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在paramiko的用法示例。


在下文中一共展示了paramiko.Transport方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: ssh_edit_file

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def ssh_edit_file(adress, user, passw, remotefile, regex, replace):
        client = paramiko.SSHClient()
        client.load_system_host_keys()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        trans = paramiko.Transport((adress, 22))
        trans.connect(username=user, password=passw)
        sftp = paramiko.SFTPClient.from_transport(trans)
        f_in = sftp.file(remotefile, "r")
        c_in = f_in.read()
        pattern = re.compile(regex, re.MULTILINE | re.DOTALL)
        c_out = pattern.sub(replace, c_in)
        f_out = sftp.file(remotefile, "w")
        f_out.write(c_out)
        f_in.close()
        f_out.close()
        sftp.close()
        trans.close() 
開發者ID:dsp-jetpack,項目名稱:JetPack,代碼行數:19,代碼來源:ssh.py

示例2: main

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def main():
    target_ip = etss_range(ip)
    for i in target_ip:
        try:
            t = paramiko.Transport((i, port))
            t.connect(username=user, password=passwd)
            sftp = paramiko.SFTPClient.from_transport(t)
            #Download files
            filepath = '/scripts/test.txt'
            localpath = 'test.txt'
            #dirlist = scp.listdir(filepath)
            #print dirlist
            #sftp.get(filepath, localpath)
            #Upload files
            sftp.put(localpath, filepath)
            time.sleep(15)
            print "SFTP connection established to %s" % (i)
            print "File(s) being uploaded ..."
        except:
            sftp.close()
            t.close()
            print "Problem connecting with ip: %s" % (i)
            continue 
開發者ID:networkingdvi,項目名稱:HPN-Scripting,代碼行數:25,代碼來源:etss_scp_in.py

示例3: get_remote_server_key

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def get_remote_server_key(ip: str, port: int=22) -> paramiko.pkey.PKey:
    """
    Returns PKey for given server
    :param ip: IP or Hostname
    :param port: Port
    :return: Returns PKey
    """

    my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    my_socket.settimeout(10)
    my_socket.connect((ip, port))

    my_transport = paramiko.Transport(my_socket)
    my_transport.start_client()
    ssh_key = my_transport.get_remote_server_key()

    my_transport.close()
    my_socket.close()
    return ssh_key 
開發者ID:Salamek,項目名稱:gitlab-tools,代碼行數:21,代碼來源:crypto.py

示例4: __init__

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def __init__(self, local_port, remote_address, remote_port, transport):
        """
        type: local_port: int
        param: local_port: local tunnel endpoint ip binding
        type: remote_address: str
        param: remote_address: Remote tunnel endpoing ip binding
        type: remote_port: int
        param: remote_port: Remote tunnel endpoint port binding
        type: transport: :py:class:`paramiko.Transport`
        param: transport: Paramiko ssh transport
        """
        super(Forward, self).__init__()
        self.local_port = local_port
        self.remote_address = remote_address
        self.remote_port = remote_port
        self.transport = transport 
開發者ID:ThreatResponse,項目名稱:margaritashotgun,代碼行數:18,代碼來源:ssh_tunnel.py

示例5: handle_tcp_ssh

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def handle_tcp_ssh(socket, dstport):
	try:
		t = paramiko.Transport(socket)
		t.local_version = 'SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2'
		t.load_server_moduli() # It can be safely commented out if it does not work on your system

		t.add_server_key(host_key_rsa)
		t.add_server_key(host_key_dss)

		server = Server(socket.getpeername())
		t.start_server(server=server)

		t.join()

	except Exception as err:
		#print(traceback.format_exc())
		pass

	try:
		print("-- SSH TRANSPORT CLOSED --")
		t.close()
	except:
		pass

	socket.close() 
開發者ID:fabio-d,項目名稱:honeypot,代碼行數:27,代碼來源:tcp_ssh.py

示例6: __init__

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def __init__(self, sock, addr):
        super(SshServer, self).__init__()

        # tweak InternalApi and RootCmd for non-bgp related commands
        self.api = InternalApi(log_handler=logging.StreamHandler(sys.stderr))
        setattr(self.api, 'sshserver', self)
        self.root = RootCmd(self.api)
        self.root.subcommands['help'] = self.HelpCmd
        self.root.subcommands['quit'] = self.QuitCmd

        transport = paramiko.Transport(sock)
        transport.load_server_moduli()
        host_key = self._find_ssh_server_key()
        transport.add_server_key(host_key)
        self.transport = transport
        transport.start_server(server=self) 
開發者ID:OpenState-SDN,項目名稱:ryu,代碼行數:18,代碼來源:ssh.py

示例7: get_key_file

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def get_key_file(ssh_key):
    '''根據key內容臨時生成一個key文件'''
    #file_path = '/tmp/%s'%shortuuid.uuid()
    file_path = '/tmp/codo_cmdb_id_rsa'
    cmd = 'echo "{}" > {} && chmod 600 {}'.format(ssh_key,file_path,file_path)
    ret,stdout = exec_shell(cmd)
    if ret == 0:
        return file_path
    else:
        return None


# def remote_upload_file(ip,user,pwd,cmd,local_file,remote_file,port=22):
#     '''ssh上傳並執行bash for pwd'''
#     t = paramiko.Transport((ip,port))
#     t.connect(username=user, password=pwd)
#     sftp = paramiko.SFTPClient.from_transport(t)
#     sftp.put(local_file,remote_file)
#     ssh = paramiko.SSHClient()
#     ssh._transport = t
#     stdin, stdout, stderr = ssh.exec_command(cmd)
#     show_log = stdout.read().decode('utf-8').strip()
#     t.close()
#     return show_log 
開發者ID:opendevops-cn,項目名稱:codo-cmdb,代碼行數:26,代碼來源:common.py

示例8: __init__

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def __init__(self, host, username, password, remote_dir, port=None, keeps=None):
        super(SFTPBackend, self).__init__(keeps)
        self.host = host
        self.port = port or 22
        self.username = username
        self.password = password
        self.remote_dir = remote_dir

        # 這一步會建立連接
        try:
            self.transport = paramiko.Transport((self.host, self.port))
            # 登錄驗證
            self.transport.connect(username=self.username, password=self.password)
            self.sftp = paramiko.SFTPClient.from_transport(self.transport)
        except:
            # 這個的關閉,並不會關閉transport的連接
            if self.sftp:
                self.sftp.close()

            # 所以如果登錄失敗,要記得關閉連接
            if self.transport:
                self.transport.close()

            t, v, tb = sys.exc_info()
            reraise(t, v, tb) 
開發者ID:dantezhu,項目名稱:yunbk,代碼行數:27,代碼來源:sftp.py

示例9: start_server

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def start_server(host, port, keyfile, level):
    paramiko_level = getattr(paramiko.common, level)
    paramiko.common.logging.basicConfig(level=paramiko_level)


    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
    server_socket.bind((host, port))
    server_socket.listen(BACKLOG)
    print host,port
    while True:
        conn, addr = server_socket.accept()

        host_key = paramiko.RSAKey.from_private_key_file(keyfile)
        transport = paramiko.Transport(conn)
        transport.add_server_key(host_key)
        transport.set_subsystem_handler(
            'sftp', paramiko.SFTPServer, StubSFTPServer)

        server = StubServer()
        transport.start_server(server=server)

        channel = transport.accept()
        while transport.is_active():
            time.sleep(1) 
開發者ID:tintinweb,項目名稱:pub,代碼行數:27,代碼來源:__init__.py

示例10: _start_sftp_server

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def _start_sftp_server():
    """Start the SFTP local server."""
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.setblocking(False)
    sock.bind(('localhost', 2222))
    sock.listen(10)

    reads = {sock}
    others = set()

    while not event.is_set():
        ready_to_read, _, _ = select.select(reads, others, others, 1)

        if sock in ready_to_read:
            client_socket, address = sock.accept()
            ts = paramiko.Transport(client_socket)

            host_key = paramiko.RSAKey.from_private_key_file(t_path('server_id_rsa'))
            ts.add_server_key(host_key)
            server = StubServer()
            ts.set_subsystem_handler('sftp', paramiko.SFTPServer, StubSFTPServer)
            ts.start_server(server=server)

    sock.close() 
開發者ID:unbit,項目名稱:sftpclone,代碼行數:27,代碼來源:test_sftpclone.py

示例11: __repr__

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def __repr__(self):
        """
        Returns a string representation of this object, for debugging.

        @rtype: str
        """
        out = '<paramiko.Transport at %s' % hex(long(id(self)) & 0xffffffffL)
        if not self.active:
            out += ' (unconnected)'
        else:
            if self.local_cipher != '':
                out += ' (cipher %s, %d bits)' % (self.local_cipher,
                                                  self._cipher_info[self.local_cipher]['key-size'] * 8)
            if self.is_authenticated():
                out += ' (active; %d open channel(s))' % len(self._channels)
            elif self.initial_kex_done:
                out += ' (connected; awaiting auth)'
            else:
                out += ' (connecting)'
        out += '>'
        return out 
開發者ID:iopsgroup,項目名稱:imoocc,代碼行數:23,代碼來源:transport.py

示例12: __repr__

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def __repr__(self):
        """
        Returns a string representation of this object, for debugging.
        """
        out = '<paramiko.Transport at %s' % hex(long(id(self)) & xffffffff)
        if not self.active:
            out += ' (unconnected)'
        else:
            if self.local_cipher != '':
                out += ' (cipher %s, %d bits)' % (self.local_cipher,
                                                  self._cipher_info[self.local_cipher]['key-size'] * 8)
            if self.is_authenticated():
                out += ' (active; %d open channel(s))' % len(self._channels)
            elif self.initial_kex_done:
                out += ' (connected; awaiting auth)'
            else:
                out += ' (connecting)'
        out += '>'
        return out 
開發者ID:iopsgroup,項目名稱:imoocc,代碼行數:21,代碼來源:transport.py

示例13: print_remote_cpu_info

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def print_remote_cpu_info(hostname, port, username, password):
    client = paramiko.Transport((hostname, port))
    client.connect(username=username, password=password)
    
    stdout_data = []
    stderr_data = []
    session = client.open_channel(kind='session')
    session.exec_command(COMMAND)
    while True:
        if session.recv_ready():
            stdout_data.append(session.recv(RECV_BYTES))
        if session.recv_stderr_ready():
            stderr_data.append(session.recv_stderr(RECV_BYTES))
        if session.exit_status_ready():
            break
    
    print ('exit status: ', session.recv_exit_status())
    print (b''.join(stdout_data))
    print (b''.join(stderr_data))
    
    session.close()
    client.close() 
開發者ID:PacktPublishing,項目名稱:Python-Network-Programming-Cookbook-Second-Edition,代碼行數:24,代碼來源:6_3_print_remote_cpu_info.py

示例14: sftp_conn

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def sftp_conn(self):
        """"""
        try:
            self.transport = paramiko.Transport((self.host, self.port)) 
            self.transport.connect(username = self.username, password = self.password) 
            self.sftp = paramiko.SFTPClient.from_transport(self.transport)
            return True
        except paramiko.AuthenticationException:
            G.log(G.ERROR, "SFTP Authentication failed when connecting to %s" % self.host)
            return False
        except:
            G.log(G.ERROR, "SFTP Could not SSH to %s, waiting for it to start" % self.host)
            return False

    
    #---------------------------------------------------------------------- 
開發者ID:alibaba,項目名稱:iOSSecAudit,代碼行數:18,代碼來源:minisftp.py

示例15: ssh_command

# 需要導入模塊: import paramiko [as 別名]
# 或者: from paramiko import Transport [as 別名]
def ssh_command(ip, user, passwd, command):
    transport = paramiko.Transport(ip)
    try:
        transport.start_client()
    except Exception as e:
        print(e)
    
    try:
        transport.auth_password(username=user,password=passwd)
    except Exception as e:
        print(e)
        
    if transport.is_authenticated():
        print(transport.getpeername())
        channel = transport.opem_session()
        channel.exec_command(command)
        response = channel.recv(1024)
        print('Command %r(%r)-->%s' % (command,user,response)) 
開發者ID:PacktPublishing,項目名稱:Mastering-Python-for-Networking-and-Security,代碼行數:20,代碼來源:SSH_transport.py


注:本文中的paramiko.Transport方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。