python实现ssh客户端

发布时间:2019-09-11 07:44:06编辑:auto阅读(1609)

    以下程序均来自《Python.UNIX和Linux系统管理指南》

    ssh有两种登陆方式,一种是使用用户名、密码方式,另一种是使用加密,不再使用密码


    使用用户名、密码方式,前提是曾经ssh登陆过远程机器,在用户目录的.ssh目录下的known_hosts有记录

    ssh.py
    #!/usr/bin/env python
    import paramiko
    hostname = '192.168.137.3'
    port = 22
    username = "root"
    password = "123456"
    if __name__ == "__main__":
            paramiko.util.log_to_file('paramiko.log')
            s = paramiko.SSHClient()
            s.load_system_host_keys()
            s.connect(hostname, port, username, password)
            stdin, stdout, stderr = s.exec_command('ifconfig')
            print stdout.read()
            s.close()

    运行结果:

    [root@centos python]# python ssh.py

    eth0      Link encap:Ethernet  HWaddr 00:0C:29:1B:C2:CD  

             inet addr:192.168.137.3  Bcast:192.168.137.255  Mask:255.255.255.0

             UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

             RX packets:976 errors:0 dropped:0 overruns:0 frame:0

             TX packets:958 errors:0 dropped:0 overruns:0 carrier:0

             collisions:0 txqueuelen:1000

             RX bytes:104727 (102.2 KiB)  TX bytes:125765 (122.8 KiB)

             Interrupt:67 Base address:0x2000


    lo        Link encap:Local Loopback  

             inet addr:127.0.0.1  Mask:255.0.0.0

             UP LOOPBACK RUNNING  MTU:16436  Metric:1

             RX packets:2340 errors:0 dropped:0 overruns:0 frame:0

             TX packets:2340 errors:0 dropped:0 overruns:0 carrier:0

             collisions:0 txqueuelen:0

             RX bytes:3564169 (3.3 MiB)  TX bytes:3564169 (3.3 MiB)

    sftp

    sftp.py
    #!/usr/bin/env python
    import paramiko
    import os
    hostname = '192.168.137.3'
    port = 22
    username = 'root'
    password = '123456'
    dir_path = '/usr/local/get'
    if __name__ == '__main__':
            t = paramiko.Transport((hostname, port))
            t.connect(username=username, password=password)
            sftp = paramiko.SFTPClient.from_transport(t)
            files = sftp.listdir(dir_path)
            for f in files:
                    print 'Retrieving', f
                    sftp.get(os.path.join(dir_path, f), f)
            t.close()

    运行结果:

    [root@centos python]# python sftp.py

    Retrieving c

    Retrieving a

    Retrieving b

    Retrieving d


    使用RSA加密方式,前提是已经设置好加密登陆方式

    ssh加密登陆方法:

    [root@centos .ssh]# ssh-keygen -t rsa  #其他的直接回车就行

    Generating public/private rsa key pair.

    Enter file in which to save the key (/root/.ssh/id_rsa):

    Enter passphrase (empty for no passphrase):

    Enter same passphrase again:

    Your identification has been saved in /root/.ssh/id_rsa.

    Your public key has been saved in /root/.ssh/id_rsa.pub.

    The key fingerprint is:

    74:64:97:4d:af:52:c0:8c:98:ae:83:7e:e7:25:3a:6e root@centos

    ssh

    [root@centos .ssh]# cp id_rsa.pub authorized_keys

    [root@centos .ssh]# scp authorized_keys root@192.168.137.3:/root/.ssh

    #拷贝到ssh的那天机器上


    ssh

    ssh_rsa.py
    #!/usr/bin/env python
    import paramiko
    hostname = '192.168.137.3'
    port = 22
    username = "root"
    password = "123456"
    pkey_file = "/root/.ssh/id_rsa"
    if __name__ == "__main__":
            key = paramiko.RSAKey.from_private_key_file(pkey_file)
            s = paramiko.SSHClient()
            s.load_system_host_keys()
            s.connect(hostname, port, pkey=key)
            stdin, stdout, stderr = s.exec_command('ifconfig')
            print stdout.read()
            s.close()

    运行结果同上ssh

    sftp

    sftp_rsa.py
    #!/usr/bin/env python
    import paramiko
    import os
    hostname = '192.168.137.3'
    port = 22
    username = 'root'
    password = '123456'
    dir_path = '/usr/local/get'
    pkey_file = '/root/.ssh/id_rsa'
    if __name__ == '__main__':
            key = paramiko.RSAKey.from_private_key_file(pkey_file)
            t = paramiko.Transport((hostname, port))
            t.connect(username=username, pkey=key)
            sftp = paramiko.SFTPClient.from_transport(t)
            files = sftp.listdir(dir_path)
            for f in files:
                    print 'Retrieving', f
                    sftp.get(os.path.join(dir_path, f), f)
            t.close()

    运行结果同上sftp

关键字