import os import paramiko # 定义函数,用于执行Show命令并将结果写入文件 def execute_show_command_and_save_to_file(ssh, command, directory): # 执行Show命令并获取输出 stdin, stdout, stderr = ssh.exec_command(command) output = stdout.read() # 将结果写入文件 filename = os.path.join(directory, f"{command.replace(' ', '_')}.txt") with open(filename, "w") as f: f.write(output.decode()) # 打开存储IP地址的文件 with open("ip_addresses.txt", "r") as f: ip_addresses = f.readlines() # 循环读取IP地址 for ip_address in ip_addresses: ip_address = ip_address.strip() # 去除行末的换行符 # 创建SSH客户端并登录 ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect(ip_address, username="admin", password="password", timeout=1) # 根据IP地址创建目录并进入 directory = os.path.join(os.path.abspath("."), ip_address.replace('.', '_')) os.makedirs(directory, exist_ok=True) ssh.exec_command(f"cd {directory}") # 从文件中读取Show命令
网络设备自动化数据采集Python脚本
最新推荐文章于 2024-12-01 15:44:02 发布