import os,json,sys from MySSH import MySSH
def ping(group): with open("config.json" , "r" ,encoding="utf-8") as read_config_ptr: config_load = json.loads(read_config_ptr.read()) ptr = config_load.get(group) print("-" * 120) print("{0:15} \t {1:6} \t {2:5}".format("IP地址", "用户名", "状态")) print("-" * 120)
for x in ptr: ssh = MySSH(x[0],x[1],x[2],22) ssh.Init() ref = ssh.GetPing() if ref == True: print("{0:15} \t {1:6} \t {2:5}".format(x[0],x[1],"已连接")) else: print("{0:15} \t {1:6} \t {2:5}".format(x[0], x[1], "未连接")) print("\n")
def run(group,command): with open("config.json" , "r" ,encoding="utf-8") as read_config_ptr: config_load = json.loads(read_config_ptr.read()) ptr = config_load.get(group) for x in ptr: ssh = MySSH(x[0],x[1],x[2],22) ssh.Init() ref = ssh.BatchCMD(command) print("\n") print("-" * 120) print("执行IP: {0:15} ".format(x[0])) print("-" * 120) print(ref)
def memory(group): with open("config.json" , "r" ,encoding="utf-8") as read_config_ptr: config_load = json.loads(read_config_ptr.read()) ptr = config_load.get(group) print("-" * 120) print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format("IP地址", "总内存", "剩余内存", "利用率(百分比)")) print("-" * 120)
for x in ptr: ssh = MySSH(x[0],x[1],x[2],22) ssh.Init() ref = ssh.GetAllMemSpace() if ref != None: print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format(x[0],ref["Total"],ref["Free"],ref["Percentage"]))
def disk(group): with open("config.json" , "r" ,encoding="utf-8") as read_config_ptr: config_load = json.loads(read_config_ptr.read()) ptr = config_load.get(group)
for x in ptr: ssh = MySSH(x[0],x[1],x[2],22) ssh.Init() print("-" * 120) print("IP地址: {0:15} \t".format(x[0])) print("-" * 120)
ref = ssh.GetAllDiskSpace() if len(ref) !=0: for k,v in ref.items(): print("磁盘路径: {0:30} \t 磁盘利用率: {1:8}".format(k,v))
def cpu(group): with open("config.json" , "r" ,encoding="utf-8") as read_config_ptr: config_load = json.loads(read_config_ptr.read()) ptr = config_load.get(group) print("-" * 120) print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format("IP地址", "用户态", "内核态", "空闲率(百分比)")) print("-" * 120)
for x in ptr: ssh = MySSH(x[0],x[1],x[2],22) ssh.Init() ref = ssh.GetCPUPercentage() if len(ref)!=0: print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format(x[0], ref["us"], ref["sys"], ref["idea"]))
def load_avg(group): with open("config.json" , "r" ,encoding="utf-8") as read_config_ptr: config_load = json.loads(read_config_ptr.read()) ptr = config_load.get(group) print("-" * 120) print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format("IP地址", "一分钟负载", "五分钟负载", "十五分钟负载")) print("-" * 120)
for x in ptr: ssh = MySSH(x[0],x[1],x[2],22) ssh.Init() ref = ssh.GetLoadAVG() if len(ref)!=0: print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format(x[0], ref["1avg"], ref["5avg"], ref["15avg"]))
def checkproc(group,proc): with open("config.json" , "r" ,encoding="utf-8") as read_config_ptr: config_load = json.loads(read_config_ptr.read()) ptr = config_load.get(group) print("-" * 120) print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format("IP地址", "PID号","CPU占用率", "内存占用率")) print("-" * 120)
for x in ptr: ssh = MySSH(x[0],x[1],x[2],22) ssh.Init() ref = ssh.CheckProcessName(proc) if len(ref): print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format(x[0], ref["PID"], ref["CPU"], ref["Mem"])) else: print("{0:15} \t {1:7} \t {2:7} \t {3:5} \t".format(x[0], 0, 0, 0))
def put_group(group,src,dst): with open("config.json" , "r" ,encoding="utf-8") as read_config_ptr: config_load = json.loads(read_config_ptr.read()) ptr = config_load.get(group) print("-" * 120) print("{0:15} \t {1:7} \t {2:7} \t ".format("IP地址", "源文件","传输到")) print("-" * 120)
for x in ptr: ssh = MySSH(x[0],x[1],x[2],22) ssh.Init() ref = ssh.PutLocalFile(src,dst) if ref: print("{0:15} \t {1:7} \t {2:7} \t ".format(x[0], src,dst))
if __name__ == '__main__': while True: try: cmd = str(input("[LyShark Shell] # ")).split() cmd_len = len(cmd) if (cmd == ""): continue elif (cmd[0] == "exit"): exit(1)
elif (cmd[0] == "ping"): if (cmd_len - 1 >= 1): arg = cmd[1].split("=")[1] ping(arg)
elif (cmd[0] == "run"): if (cmd_len - 1 >= 2): arg1 = cmd[1].split("=")[1] arg2 = cmd[2].split("=")[1] run(arg1,arg2)
elif (cmd[0] =="memory"): if (cmd_len - 1 >= 1): arg1 = cmd[1].split("=")[1] memory(arg1)
elif (cmd[0] =="disk"): if (cmd_len - 1 >= 1): arg1 = cmd[1].split("=")[1] disk(arg1)
elif (cmd[0] =="cpu"): if (cmd_len - 1 >= 1): arg1 = cmd[1].split("=")[1] cpu(arg1)
elif (cmd[0] =="load"): if (cmd_len - 1 >= 1): arg1 = cmd[1].split("=")[1] load_avg(arg1)
elif (cmd[0] =="checkproc"): if (cmd_len - 1 >= 1): arg1 = cmd[1].split("=")[1] arg2 = cmd[2].split("=")[1] checkproc(arg1,arg2)
elif (cmd[0] =="put_group"): if (cmd_len - 1 >= 3): arg1 = cmd[1].split("=")[1] arg2 = cmd[2].split("=")[1] arg3 = cmd[3].split("=")[1] put_group(arg1,arg2,arg3) else: print("[-] error version 1.0") except Exception: continue
|