判断远程主机是否存在某个文件
1.
1 # -*- coding:utf-8 -*- 2 3 import os,sys,paramiko,time 4 #*******判断所有测试环境是否有某个文件,*********** 5 s7 = "10.10.123.96" 6 s3 = "10.10.39.70" 7 host_dic = {"10.10.154.77":"s1", 8 "10.10.83.96":"s2", 9 "10.10.39.70":"s3", 10 "10.10.46.120":"s4", 11 "10.10.15.128":"s5", 12 "10.10.84.48":"s6", 13 "10.10.123.96":"s7", 14 "10.10.105.91":"qa1", 15 "10.10.92.48":"qa2", 16 "10.10.50.30":"qa3", 17 "10.10.40.136":"qa4", 18 "10.10.189.139":"qa5" 19 } 20 exist ={} 21 not_exist = {} 22 port =22 23 user = "root" 24 passwd = "B^Dc%4LSBvhZZK3B" 25 26 def ssh_cmd(ip,port,cmd,user,passwd): 27 result = "" 28 try: 29 ssh = paramiko.SSHClient() 30 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 31 ssh.connect(ip,port,user,passwd) 32 stdin, stdout, stderr = ssh.exec_command(cmd) 33 result = stdout.read() 34 print result 35 ssh.close() 36 except: 37 print "ssh_cmd err." 38 return result 39 while True: 40 raw = raw_input("按a批量执行命令: 按b批量判断文件是否存在: 按q退出:") 41 if raw =="a": 42 cmd = raw_input("请输入要执行的命令:") 43 for i in host_dic: 44 print host_dic.get(i) 45 ssh_cmd(i,port,cmd, user,passwd) 46 elif raw =="b": 47 while True: 48 print "\033[32;1m****使用说明:该脚本可以判断文件,不能判断目录。****\033[0m" 49 choice = raw_input("请输入要查询远端服务器上文件的绝对路径: 按q退出") 50 if choice =="q": 51 print "感谢使用,再见" 52 exit() 53 v = "ls" 54 c = v + " " +choice 55 #print c 56 for i in host_dic: 57 ssh = paramiko.SSHClient() 58 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 59 ssh.connect(i,port,user,passwd) 60 stdin, stdout, stderr = ssh.exec_command(c) 61 result = stdout.read() 62 #print result 63 if result != "": 64 print host_dic.get(i),"\033[31;1m上存在该文件\033[0m" 65 exist[i]=host_dic.get(i) 66 #print host_dic.get(i),"\033[33;1m上存在该文件\033[0m" 67 ssh.close() 68 #print exist 69 for i in host_dic: 70 if i not in exist: 71 not_exist[i]=host_dic.get(i) 72 print host_dic.get(i),"\033[33;1m上不存在该文件\033[0m" 73 exist = {} 74 #print not_exist 75 choice1 = raw_input("按任意键继续查询,按t拷贝本地一个文件到不存在该文件的机器上,按q退出:") 76 if choice1 =="q": 77 print "感谢使用,再见" 78 print exit() 79 elif choice1 == "t": 80 local_file = raw_input("请输入本地文件的绝对路径:") 81 remote_file = raw_input("请输入要复制到远端机器的绝对路径:") 82 for j in not_exist: 83 scp = paramiko.SSHClient() 84 scp.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 85 scp.connect(j,port,user,passwd) 86 sftp = paramiko.SFTPClient.from_transport(scp.get_transport()) 87 sftp = scp.open_sftp() 88 sftp.put(local_file,remote_file) 89 print "\033[31;1m已复制到\033[0m",host_dic.get(j) 90 not_exist = {} 91 else: 92 print "感谢使用,再见" 93 exit()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义