什么是paramiko
paramiko是一个基于ssh用于做远程控制的模块,使用该模块可以对远程服务器进行命令或文件操作
1、paramiko远程密码连接
1 import paramiko
# 导入模块
2 client = paramiko.SSHClient()
# 创建一个ssh对象,使用变量client 接收
3 client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 该步操作用于解决当是首次连接莫一台主机的时候,需要输入yes完成认证,这步自动选择yes
client.connect(
hostname='172.25.0.101',
username='root',
password='redhat'
)
4 #连接主机,参数分别为主机ip,连接的用户,以及用户的密码
5 client.exec_command(要执行的命令)
# 通过ssh连接成功之后,在被连接的主机中执行命令。这个操作返回一个元组,元组存在三个元素,分别为标准输入,标准输出,错误输出。
stdin,stdout,stderr = client.exec_command(命令)
#使用三个变量归该部分分别进行接收
6 stdout.read().decode('utf-8')
# 对标准输出结果进行获取,decode('utf-8')
主要是解决返回结果中存在中文能够正常显示
7 client.close()
# 最后必须关闭连接对象
示例:
import paramiko
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
hostname='172.25.47.104',
username='root',
password='redhat'
)
stdin,stdout,stderr = client.exec_command('find /etc -name passwd')
print(stdout.read().decode('utf-8'))
#输出结果:
/etc/passwd
/etc/pam.d/passwd
2、paramiko批量远程密码连接
当需要处理的主机比较多的时候,可以使用批量连接的方式。将需要连接的主机ip,用户名称以及密码等信息,按照固定的格式保存在文件中,通过读取文件中的内容实现批量自动连接进行操作。
主要步骤:
1 将上述的远程密码连接主机进行操作,定义成一个函数。
2 将需要批量连接处理的主机信息,写在文件中,文件对象也是一个可迭代的对象,使用循环遍历对所有的主机进行连接操作。
示例:
#函数需要四个参数:需要执行的命令,主机ip,用户名称,用户密码
def connect(cmd,hostname,username,passwd):
import paramiko #导入模块
client = paramiko.SSHClient() # 创建ssh连接对象
#解决初次连接主机需要输入yes进行确认问题
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#连接主机
client.connect(
hostname=hostname,
username=username,
password=passwd
)
#执行命令并对其使用变量进行接收
stdin, stdout, stderr = client.exec_command(cmd)
#返回输出结果
return (stdout.read().decode('utf-8'))
if __name__ == '__main__':
with open('hosts.txt')as f: #打开文件
for line in f: # 使用循环遍历文件的每一行
#172.25.254.1:root:westos 文件每行格式
#将每行的内容进行分离得到,主机,用户以及密码信息
hostname,username,passwd = line.strip().split(':')
res = connect('pwd',hostname,username,passwd)
print(res)
3、paramiko基于公钥和私钥的连接
由于 paramiko是基于ssh进行的连接,所以可以使用公钥和私钥使指定的用户对指定主机的指定用户进行免密连接。
连接的前提是,需要提前将公钥放置被连接主机的指定用户~/.ssh
目录中。将私钥放置在客户端主机的指定用户~/.ssh 中 设置ssh服务公钥以及私钥
连接操作步骤与第一个所以密码连接基本一致,值守在连接主机的时候,不在使用密码,而是使用生成的私钥对象。
示例:
import paramiko # 导入模块
client = paramiko.SSHClient() # 生成ssh连接对象
private_key = paramiko.RSAKey.\
from_private_key_file('/home/kiosk/.ssh/id_rsa') #生成一个私钥对象
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
hostname='172.25.47.104',
username='root',
pkey=private_key # 连接的时候使用私钥对象进行验证
)
stdin,stdout,stderr = client.exec_command('ifconfig eth0')
print(stdout.read().decode('utf-8'))
输出结果:
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.47.104 netmask 255.255.255.0 broadcast 172.25.47.255
inet6 fe80::5054:ff:fe09:86e1 prefixlen 64 scopeid 0x20<link>
ether 52:54:00:09:86:e1 txqueuelen 1000 (Ethernet)
RX packets 423 bytes 85234 (83.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 493 bytes 95248 (93.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4、paramiko基于用户和密码的文件上传和下载
1 上传
import paramiko # 导入模块
tarnsport = paramiko.Transport('172.25.47.104',22) #建立与远程主机的通道
#验证用户名和密码是否正确,连接SSH服务端
tarnsport.connect(username='root',password='redhat')
#获取SFTP 对象
sftp = paramiko.SFTPClient.from_transport(tarnsport)
#执行上传动作 本地文件路径,远程文件路径
sftp.put('/etc/passwd','/mnt/172.25.47.250')
#关闭通道
tarnsport.close()
2 下载
import paramiko # 导入模块
tarnsport = paramiko.Transport('172.25.47.104',22) #建立与远程主机的通道
#验证用户名和密码是否正确,连接SSH服务端
tarnsport.connect(username='root',password='redhat')
#获取SFTP 对象
sftp = paramiko.SFTPClient.from_transport(tarnsport)
#执行上传动作,远程文件路径 , 本地文件路径
sftp.get('/mnt/172.25.47.104' , '172.25.47.104')
#关闭通道
tarnsport.close()