CMDB-(paramiko模块 -- 实现ssh连接)

import paramiko # 实现ssh功能的模块

ssh = paramiko.SSHClient() # 实例化对象

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允许连接不在know_hosts文件中的主机

ssh.connect(hostname='192.168.6.202',port=22,username='root',password='111111') # 连接主机

'''linux用命令安装软件的时候,会询问y/n,这个stdin就是用来接收输入y还是n | stuout命令执行后的结果,是一个管道,需要用read函数读取 | stderr执行错误信息'''
stdin,stdout,stderr = ssh.exec_command('ifconfig') # 向服务器执行命令,返回的结果有3种

result = stdout.read()

print(result)

ssh.close() # 关闭ssh

2.5版本的报错信息

CryptographyDeprecationWarning: encode_point has been deprecated on EllipticCurvePublicNumbers and will be removed in a future version. Please use EllipticCurvePublicKey.public_bytes to obtain both compressed and uncompressed point encoding.

原因:paramiko 2.4.2 依赖 cryptography,而最新的cryptography==2.5里有一些弃用的API

解决:删掉cryptography 2.5,安装2.4.2,就不会报错了

pip uninstall cryptography==2.5
pip install cryptography==2.4.2 

PS:paramiko是python原生的,还有衍生的ansible和fabric,这两个底层核心的代码就是paramiko模块的代码,又重新做了一个封装

原文地址:https://www.cnblogs.com/shizhengquan/p/10749374.html