python中paramiko远程密码连接

1.什么是paramiko是什么?
基于ssh用于连接远程服务器做操作:
远程执行命令
上传文件

import paramiko 导入模块

#ssh username@ip
#创建一个ssh对象
client = paramiko.SSHClient() 创建一个ssh对象,使用变量client 接收
#解决的问题:如果之前没有连接过的ip(主机),会出现
"""
The authenticity of host '172.25.0.101 (172.25.0.101)' can't be established.
ECDSA key fingerprint is 9d:37:08:8e:a4:ad:45:b5:eb:69:6f:d2:88:d3:da:8c.
Are you sure you want to continue connecting (yes/no)? yes
"""
#自动选择yes
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#连接服务器
client.connect(
hostname='172.25.0.101',
username='root',
password='redhat'
)
#执行操作
#标准输入 标准输出 标准错误输出
stdin,stdout,stderr = client.exec_command('')
#获取命令的执行结果
print(stdout.read().decode('utf-8'))

#关闭连接
client.close()

2.paramiko批量远程密码连接

当需要处理的主机比较多的时候,可以使用批量连接的方式。将需要连接的主机ip,用户名称以及密码等信息,按照固定的格式保存在文件中,通过读取文件中的内容实现批量自动连接进行操作。

主要步骤:
1 将上述的远程密码连接主机进行操作,定义成一个函数。
2 将需要批量连接处理的主机信息,写在文件中,文件对象也是一个可迭代的对象,使用循环遍历对所有的主机进行连接操作。

from paramiko.ssh_exception import\
NoValidConnectionsError,AuthenticationException

def connect(cmd,hostname,user,password):
import paramiko
#创建一个ssh对象
client = paramiko.SSHClient()
#解决的问题:如果之前没有连接过的ip(主机),会出现
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:

连接的服务器

    client.connect(
        hostname=hostname,
        username=user,
        password=password
    )
except NoValidConnectionsError as e:
    return '主机%s连接失败' %(hostname)
except AuthenticationException as e:
    return '主机%s密码错误' %(hostname)
except Exception as e:
    return '未知错误:',e

#执行操作
stdin,stdout,stderr = client.exec_command('hostname')
#获取命令的执行结果
print(stdout.read().decode('utf-8'))

#关闭连接
client.close()

if name == 'main':
with open('hosts') as f:
for line in f:

172.25.0.250:root:dd

        hostname,username,password = line.strip().split(':')
        res = connect('pwd',hostname,username,password)
        print(hostname.center(50,'*'))
        print('主机名:',res)

3.paramiko基于公钥和私钥的连接

要注意的是:我们当前用户(执行此程序的用户)要免秘连接的一台主机、
用户拿到的是私钥 要连接的主机要挂载公钥

连接的前提是,需要提前将公钥放置被连接主机的指定用户~/.ssh 目录中。将私钥放置在客户端主机的指定用户~/.ssh 中 设置ssh服务公钥以及私钥

连接操作步骤与第一个所以密码连接基本一致,值守在连接主机的时候,不在使用密码,而是使用生成的私钥对象。

import paramiko
client = paramiko.SSHClient()
#创建一个私钥对象
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.0.250',
username='root',
pkey=private_key
)
#执行操作
#标准输入 标准输出 标准错误输出
stdin,stdout,stderr = client.exec_command('')
#获取命令的执行结果
print(stdout.read().decode('utf-8'))
#关闭连接
client.close()

4.paramiko基于用户和密码的上船和下载文件

import paramiko
from paramiko import AuthenticationException,SSHException

def put(hostname,password,source_name,target_name):
try:

类似于ssh+ftp命令

    #建立与远程主机的通道
    tarnsport = paramiko.Transport((hostname,22))
    #验证用户名和密码是否正确
    tarnsport.connect(username='root',password=password)
    #根据创建并验证成功的通道
    sftp = paramiko.SFTPClient.from_transport(tarnsport)
except AuthenticationException as e:
    return '主机%s密码错误' %(hostname)
except Exception as e:
    return '未知错误:',e
else:
    # 上传文件
    sftp.put(source_name,target_name)
    #下载文件
    #sftp.get('/mnt/name')

finally:
    # 关闭两台主机建立的通道
    tarnsport.close()

put('172.25.254.250','dd','/etc/passwd','/mnt/172.25.254.250')

转载于:https://blog.51cto.com/12893781/2408379

猜你喜欢

转载自blog.csdn.net/weixin_34258838/article/details/92716220