最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

paramiko远程连接以及批量远程连接

互联网 admin 3浏览 0评论

paramiko远程连接以及批量远程连接

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

import paramiko
#ssh root@172.25.254.250
创建一个ssh对象;
client = paramiko.SSHClient()
2. 解决问题:如果之前没有;连接过的ip, 会出现
#Are you sure you want to continue connecting (yes/no)? yes
自动选择yes
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())3. 连接服务器
client.connect(hostname='172.25.254.1',username='root',password='westos'
)
4. 执行操作
stdin, stdout, stderr = client.exec_command('hostname')
5. 获取命令的执行结果;
print(stdout.read().decode('utf-8'))
6. 关闭连接
client.close()

批量连接

from paramiko.ssh_exception import NoValidConnectionsError, AuthenticationException
def connect(cmd, hostname, user, password):import paramiko# ssh root@172.25.254.250# 创建一个ssh对象;client = paramiko.SSHClient()# 2. 解决问题:如果之前没有;连接过的ip, 会出现# Are you sure you want to continue connecting (yes/no)? yes# 自动选择yesclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:# 3. 连接服务器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  "未知错误: ", eelse:# 4. 执行操作stdin, stdout, stderr = client.exec_command('hostname')# 5. 获取命令的执行结果;res = stdout.read().decode('utf-8')# 6. 关闭连接client.close()return  resif __name__ == '__main__':with open('doc/hosts.txt') as f:for line in f:# 172.25.254.1:root:westoshostname, username, password = line.strip().split(":")res = connect('hostname', hostname, username, password )print(hostname.center(50, '*'))print("主机名:", res)

paramiko远程连接以及批量远程连接

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

import paramiko
#ssh root@172.25.254.250
创建一个ssh对象;
client = paramiko.SSHClient()
2. 解决问题:如果之前没有;连接过的ip, 会出现
#Are you sure you want to continue connecting (yes/no)? yes
自动选择yes
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())3. 连接服务器
client.connect(hostname='172.25.254.1',username='root',password='westos'
)
4. 执行操作
stdin, stdout, stderr = client.exec_command('hostname')
5. 获取命令的执行结果;
print(stdout.read().decode('utf-8'))
6. 关闭连接
client.close()

批量连接

from paramiko.ssh_exception import NoValidConnectionsError, AuthenticationException
def connect(cmd, hostname, user, password):import paramiko# ssh root@172.25.254.250# 创建一个ssh对象;client = paramiko.SSHClient()# 2. 解决问题:如果之前没有;连接过的ip, 会出现# Are you sure you want to continue connecting (yes/no)? yes# 自动选择yesclient.set_missing_host_key_policy(paramiko.AutoAddPolicy())try:# 3. 连接服务器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  "未知错误: ", eelse:# 4. 执行操作stdin, stdout, stderr = client.exec_command('hostname')# 5. 获取命令的执行结果;res = stdout.read().decode('utf-8')# 6. 关闭连接client.close()return  resif __name__ == '__main__':with open('doc/hosts.txt') as f:for line in f:# 172.25.254.1:root:westoshostname, username, password = line.strip().split(":")res = connect('hostname', hostname, username, password )print(hostname.center(50, '*'))print("主机名:", res)
发布评论

评论列表 (0)

  1. 暂无评论