手动下载paramiko库的安装包。在PyPi库中查找便可,可是不到是个人电脑问题仍是网络问题,2.0.0以上版本我都安装不了,所以我本身是安装的paramiko 1.17.0版本,此版本通过测试是能够用的python
windows手动安装,挺简单的,只需找到解压后安装包的目录,而后使用下面两条命令便可shell
python setup.py build python setup.py install
pip安装,直接执行下面的命令,若是是虚拟环境,请先切换到虚拟环境后在安装windows
pip install paramiko==1.17.0
使用paramiko库登陆设备极其简单并且能力很强,一般状况下,使用secureCRT可以登陆到设备,就可以使用paramiko库成功登陆。示例代码以下:服务器
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/2/23 11:05 下午 # @Author : paul # @Site : # @File : test.py import paramiko if __name__ == '__main__': hostname = '127.0.0.1' # 主机名 username = 'root' # 用户名 password = '123456' # 密码 # ssh login ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=hostname, username=username, password=password, timeout=10) # 关闭链接 ssh.close()
此段代码经过我本身的服务器CentOS6.5进行测试,可使用。网络
登陆成功以后,固然是但愿能够输入命令来进行交互,以实现获取系统数据。这里总结了三种交互方式。ssh
如小标题所示,此中状况为只须要输入一个命令,例如只是要获取系统的某个参数。此时能够采用相似SSHClient下的exec_command函数进行输入,命令以字符串的形式表示。示例代码以下:函数
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/2/23 11:17 下午 # @Author : paul # @Site : # @File : test2.py import paramiko if __name__ == '__main__': hostname = '127.0.0.1' # 主机名 username = 'root' # 用户名 password = '123456' # 密码 # ssh login ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=hostname, username=username, password=password, timeout=10) # 执行命令 stdin , stdout , stderr = ssh.exec_command(cmd) # the stdin, stdout, and stderr of the executing command, as a 3-tuple # stdin : 输入内容 # stdout : 输出内容 # stderr : 错误信息 print(stdout.read()) ssh.close()
固然,这里须要搞清楚,exec_command()方法也不是说只能执行一条命令,其实也能执行多条命令,可是这多条命令须要用&&符号拼接成一条命令,可是此种方式只能适用于命令提示符不变的状况下:测试
cmd1 = 'cd /' cmd2 = 'ls -l' cmd = cmd1 + "&&" + cmd2 # 执行命令 stdin , stdout , stderr = ssh.exec_command(cmd)
此种方式能够实现多个命令的方式,经过模拟输入/输出文件流与系统交互。ui
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/2/23 11:31 下午 # @Author : paul # @Site : # @File : test3.py import paramiko if __name__ == '__main__': hostname = '127.0.0.1' # 主机名 username = 'root' # 用户名 password = '123456' # 密码 # ssh login ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=hostname, username=username, password=password, timeout=10) channel = ssh.invoke_shell() stdin = channel.makefile('wb') # 输入流 stdout = channel.makefile('rb') # 输出流 stdin.write('cd /home\rls\rexit\r') # 输入须要加入回车符号 print(stdout.read()) ssh.close()
输入命令有stdin.write(command)来完成,须要注意的是,这种交互方式进行交互的话,须要一次性将命令写完,包括退出命令exit(不一样系统有不一样的退出命令),每一个命令用\r(回车符)表示结束。code
1.类channel中的send(command)函数为屡次交互提供了便利
2.命令以字符串的形式输入,须要使用"\r"做为结尾,可是不须要系统登出命令做为全部命令的结尾。
3.能够实现命令交互,可是须要结合recv_read()方法进行,即当容许读的时候再进行读取。
以下提供了一个多命令交互的示例。await_command(channel)为等待是否可以读取的函数,receive_data(channel)为能够读取以后,读取输入命令以后的数据。
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020/2/23 11:49 下午 # @Author : paul # @Site : # @File : test4.py import paramiko import time def await_command(channel): while True: if channel.recv_ready(): return True else: time.sleep(0.5) return False def receive_data(channel): all_data = str() while True: tmp_data = channel.recv(1024) all_data += tmp_data # 这里的结束循环的条件能够根据实际状况来设定 if "#" in all_data: break return all_data if __name__ == '__main__': hostname = '127.0.0.1' # 主机名 username = 'root' # 用户名 password = '123456' # 密码 try: # ssh login ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(hostname=hostname, username=username, password=password, timeout=10) # 建立一个面板用来显示交互 channel = ssh.invoke_shell() # 发送命令,接收数据 channel.send('cd /home\r') await_command(channel) print(receive_data(channel)) channel.send('ls -l\r') await_command(channel) print(receive_data(channel)) ssh.close() except Exception as e: raise Exception('connect error')
若是须要进行一行一行的读取时,也能够将makefile应用过来,以下:
def receive_data(channel): reader = channel.makefile() while True: line = reader.readline() print(line) if '#' in line: break return line
parmiko库是一个功能很强大的库,为咱们提供了不少的交互方式,可是实际上咱们须要根据本身的应用选择合适的交互方式,合适的才是最好的。