写了一个Python脚本内容如下:
#-*- coding:utf-8 -*- __author__ = 'kai' import paramiko import sys,os host = sys.argv[1] user = 'zk' password = '123' cmd = sys.argv[2] s = paramiko.SSHClient() #绑定实例 s.load_system_host_keys() #加载本机HOST主机文件 s.set_missing_host_key_policy(paramiko.AutoAddPolicy()) s.connect(host,22,user,password,timeout=5) #连接远程主机 stdin,stdout,stderr = s.exec_command(cmd) #执行命令 cmd_result = stdout.read(),stderr.read() #读取命令结果 for line in cmd_result: print line, s.close()
主要是练习用的,执行之后报错:
#python paramkio.py localhost df No handlers could be found for logger "paramiko.transport" Traceback (most recent call last): File "paramkio.py", line 16, in <module> s.connect(host,22,user,password,timeout=5) #连接远程主机 File "build/bdist.linux-x86_64/egg/paramiko/client.py", line 296, in connect File "build/bdist.linux-x86_64/egg/paramiko/transport.py", line 458, in start_client paramiko.SSHException: Incompatible ssh peer (no acceptable kex algorithm)
产生的原因是因为安装的paramiko模块版本太低
查看当前的paramiko版本:pip freeze
lxml==3.4.2
MySQL-python==1.2.5
ndg-httpsclient==0.3.2
oauthlib==0.6.1
oneconf==0.3.7
PAM==0.4.2
paramiko==1.7.6
Pillow==2.7.0
piston-mini-client==0.7.5
pyasn1==0.1.7
pyasn1-modules==0.0.5
pycrypto==2.6.1
pycups==1.9.72
然后直接升级模块版本就行了:pip install paramiko --upgrade
paramiko==1.15.2
重新执行脚本后:
# python paramkio.py localhost df 文件系统 1K-块 已用 可用 已用% 挂载点 udev 1004980 0 1004980 0% /dev tmpfs 203228 5300 197928 3% /run /dev/sda1 18447100 3839844 13647156 22% / tmpfs 1016124 156 1015968 1% /dev/shm tmpfs 5120 4 5116 1% /run/lock tmpfs 1016124 0 1016124 0% /sys/fs/cgroup cgmfs 100 0 100 0% /run/cgmanager/fs tmpfs 203228 40 203188 1% /run/user/1000 /dev/sr0 1123872 1123872 0 100% /media/zk/Ubuntu 15.04 amd64
搞定