• 判断远程主机是否存在某个文件


    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 "33[32;1m****使用说明:该脚本可以判断文件,不能判断目录。****33[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),"33[31;1m上存在该文件33[0m"
    65                     exist[i]=host_dic.get(i)
    66             #print host_dic.get(i),"33[33;1m上存在该文件33[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),"33[33;1m上不存在该文件33[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 "33[31;1m已复制到33[0m",host_dic.get(j)
    90                 not_exist = {}
    91     else:
    92         print "感谢使用,再见"
    93         exit()
  • 相关阅读:
    [转] torch损失函数
    [转] EM算法
    [转] 先验概率and后验概率
    [转] 协方差矩阵
    系统安全管理
    deeplearing4j学习以及踩过的坑
    ES 应用
    springboot使用多数据源以及配置
    SparkStreaming+Kafa+HBase
    使用IDEA2017在Windows下编程并测试Hadoop2.7+Spark2.2+Azkaban
  • 原文地址:https://www.cnblogs.com/liruixin/p/6002259.html
Copyright © 2020-2023  润新知