当前位置: 首页 > news >正文

paramiko 3

import paramiko
import concurrent.futuresdef execute_remote_command(hostname, username, password, command):try:# 创建SSH客户端client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())# 使用密码认证连接远程主机client.connect(hostname, username=username, password=password)# 执行指定的Shell命令stdin, stdout, stderr = client.exec_command(command, timeout=10)  # 设置超时时间为10秒# 输出命令执行结果和主机IP地址print(f"在主机 {hostname} 上执行命令: {command}")print("--------")for line in stdout:print(line.strip())print("--------")return hostname, Trueexcept paramiko.AuthenticationException:print(f"无法连接到主机: {hostname},认证失败。")return hostname, Falseexcept paramiko.ssh_exception.SSHException as e:if "timed out" in str(e):print(f"在主机 {hostname} 上执行命令超时。")else:print(f"在主机 {hostname} 上执行命令时出现错误: {str(e)}")return hostname, Falsefinally:if client:client.close()# 从文件中读取服务器信息
def read_servers_from_file(file_path):servers = []with open(file_path, 'r') as file:for line in file:fields = line.strip().split(',')if len(fields) == 3:server = {"hostname": fields[0],"username": fields[1],"password": fields[2]}servers.append(server)return servers# 要执行的Shell命令
command = "ls -l"# 从文件中读取服务器列表
servers = read_servers_from_file("servers.txt")# 调整并发线程池的大小为10
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:futures = {executor.submit(execute_remote_command,server["hostname"],server["username"],server["password"],command): server["hostname"] for server in servers}# 获取任务结果results = {}for future in concurrent.futures.as_completed(futures):hostname = futures[future]try:result = future.result()results[hostname] = result[1]except Exception as e:print(f"在主机 {hostname} 上执行命令时出现错误: {str(e)}")# 打印连接失败和超时的主机
print("\n连接失败或超时的主机:")
for hostname, success in results.items():if not success:print(hostname)

servers.txt的文本文件中

172.16.20.108,root,123456
172.16.20.90,root,123qwe
http://www.lryc.cn/news/179193.html

相关文章:

  • 基于Dlib训练自已的人脸数据集提高人脸识别的准确率
  • Git 详细安装教程(详解 Git 安装过程的每一个步骤
  • kafka伪集群部署,使用KRAFT模式
  • 【双指针遍历】N数之和问题
  • Qt的QObject类
  • 【图论C++】链式前向星(图(树)的存储)
  • 16.PWM输入捕获示例程序(输入捕获模式测频率PWMI模式测频率和占空比)
  • pip version 更新
  • Oracle - 多区间按权重取值逻辑
  • 本次CTF·泰山杯网络安全的基础知识部分(二)
  • MyBatis 映射文件(Mapper XML):配置与使用
  • 基于 SpringBoot 的大学生租房网站
  • BL808学习日志-0-概念理解
  • CISSP学习笔记:业务连续性计划
  • .NET Nuget包推荐安装
  • 【文献阅读】Pocket2Mol : 基于3D蛋白质口袋的高效分子采样 + CrossDocked数据集说明
  • TrustRadius 评论:为什么 Splashtop 优于 LogMeIn
  • 【动态规划】动态规划经典例题 力扣牛客
  • 统计模型----决策树
  • C# List 复制之深浅拷贝
  • 论<script> 标签可以直接写在 HTML 文件中的哪些位置?(可以将 <script> 标签直接插入到 HTML 文件的任何位置)
  • 【MySQL进阶】--- 存储引擎的介绍
  • self-XSS漏洞SRC挖掘
  • 1859. 将句子排序
  • 普通学校,普通背景,普通公司,不普通总结。
  • Flink之Watermark生成策略
  • 提升API文档编写效率,Dash for Mac是你的不二之选
  • 无人注意,新安装的 Ubuntu 23.04 不支持安装 32 位应用
  • 全面横扫:dlib Python API在Linux和Windows的配置方案
  • 30种编程语言写国庆节快乐,收藏后改改留着拜年用