paramiko是基于Python实现的SSH2远程安全连接,支持认证及密钥方式。可以实现远程命令执行、文件传输、中间SSH代理等功能,相对于Pexpect,封装的层次更高,更贴近SSH协议的功能

官网地址:http://www.paramiko.org/installing.html

       http://docs.paramiko.org/en/2.4/

       https://pypi.org/project/paramiko/

二、paramiko安装

root@localhost ~]# pip3 install paramiko

简单实现远程SSH运行命令示例

import paramiko

hostname = '192.168.1.5'
username = 'root'
password = '123123'
paramiko.util.log_to_file('syslogin.log')     #发送paramiko日志到syslogin.log文件

ssh = paramiko.SSHClient()          #创建一个SSH客户端client对象
ssh.load_system_host_keys()         #获取客户端host_keys,默认~/.ssh/known_hosts,非默认路径需指定ssh.load_system_host_keys(/xxx/xxx) 
ssh.connect(hostname=hostname,username=username,password=password)    #创建SSH连接
stdin,stdout,stderr = ssh.exec_command('free -h')      #调用远程执行命令方法exec_command()
print(stdout.read().decode('utf-8'))        #打印命令执行结果,得到Python列表形式,可以使用stdout_readlines()
ssh.close()

 

程序运行结果如下图所示:自动化运维之paramiko详解

DEB [20190809-13:44:34.792] thr=1   paramiko.transport: starting thread (client mode): 0x54a13f28
DEB [20190809-13:44:34.792] thr=1   paramiko.transport: Local version/idstring: SSH-2.0-paramiko_2.6.0
DEB [20190809-13:44:34.792] thr=1   paramiko.transport: Remote version/idstring: SSH-2.0-OpenSSH_7.4
INF [20190809-13:44:34.792] thr=1   paramiko.transport: Connected (version 2.0, client OpenSSH_7.4)
DEB [20190809-13:44:34.792] thr=1   paramiko.transport: kex algos:['curve25519-sha256', 'curve25519-sha256@libssh.org', 'ecdh-sha2-nistp256', 'ecdh-sha2-nistp384', 'ecdh-sha2-nistp521', 'diffie-hellman-group-exchange-sha256', 'diffie-hellman-group16-sha512', 'diffie-hellman-group18-sha512', 'diffie-hellman-group-exchange-sha1', 'diffie-hellman-group14-sha256', 'diffie-hellman-group14-sha1', 'diffie-hellman-group1-sha1'] server key:['ssh-rsa', 'rsa-sha2-512', 'rsa-sha2-256', 'ecdsa-sha2-nistp256', 'ssh-ed25519'] client encrypt:['chacha20-poly1305@openssh.com', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com', 'aes128-cbc', 'aes192-cbc', 'aes256-cbc', 'blowfish-cbc', 'cast128-cbc', '3des-cbc'] server encrypt:['chacha20-poly1305@openssh.com', 'aes128-ctr', 'aes192-ctr', 'aes256-ctr', 'aes128-gcm@openssh.com', 'aes256-gcm@openssh.com', 'aes128-cbc', 'aes192-cbc', 'aes256-cbc', 'blowfish-cbc', 'cast128-cbc', '3des-cbc'] client mac:['umac-64-etm@openssh.com', 'umac-128-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha1-etm@openssh.com', 'umac-64@openssh.com', 'umac-128@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] server mac:['umac-64-etm@openssh.com', 'umac-128-etm@openssh.com', 'hmac-sha2-256-etm@openssh.com', 'hmac-sha2-512-etm@openssh.com', 'hmac-sha1-etm@openssh.com', 'umac-64@openssh.com', 'umac-128@openssh.com', 'hmac-sha2-256', 'hmac-sha2-512', 'hmac-sha1'] client compress:['none', 'zlib@openssh.com'] server compress:['none', 'zlib@openssh.com'] client lang:[''] server lang:[''] kex follows?False
DEB [20190809-13:44:34.807] thr=1   paramiko.transport: Kex agreed: curve25519-sha256@libssh.org
DEB [20190809-13:44:34.807] thr=1   paramiko.transport: HostKey agreed: ssh-rsa
DEB [20190809-13:44:34.807] thr=1   paramiko.transport: Cipher agreed: aes128-ctr
DEB [20190809-13:44:34.807] thr=1   paramiko.transport: MAC agreed: hmac-sha2-256
DEB [20190809-13:44:34.807] thr=1   paramiko.transport: Compression agreed: none
DEB [20190809-13:44:34.807] thr=1   paramiko.transport: kex engine KexCurve25519 specified hash_algo <built-in function openssl_sha256>
DEB [20190809-13:44:34.807] thr=1   paramiko.transport: Switch to new keys ...
DEB [20190809-13:44:34.839] thr=1   paramiko.transport: userauth is OK
INF [20190809-13:44:44.883] thr=1   paramiko.transport: Authentication (password) successful!
DEB [20190809-13:44:44.883] thr=2   paramiko.transport: [chan 0] Max packet in: 32768 bytes
DEB [20190809-13:44:44.883] thr=1   paramiko.transport: Received global request "hostkeys-00@openssh.com"
DEB [20190809-13:44:44.883] thr=1   paramiko.transport: Rejecting "hostkeys-00@openssh.com" global request from server.
DEB [20190809-13:44:44.946] thr=1   paramiko.transport: [chan 0] Max packet out: 32768 bytes
DEB [20190809-13:44:44.946] thr=1   paramiko.transport: Secsh channel 0 opened.
DEB [20190809-13:44:44.946] thr=1   paramiko.transport: [chan 0] Sesch channel 0 request ok
DEB [20190809-13:44:45.102] thr=1   paramiko.transport: [chan 0] EOF received (0)
DEB [20190809-13:44:45.102] thr=1   paramiko.transport: [chan 0] EOF sent (0)
syslogin.log

相关文章: