def eq_add(request): result = {"state": "error", "data": ""} if request.method == "POST": data = request.POST ip = data.get("ip") port = data.get("port") user = data.get("username") password = data.get("password") if ip and port and user and password: eq = Equipment() eq.IP = ip eq.User = user eq.Password = password try: trans = paramiko.Transport(ip, port) trans.connect(username=user, password=password) sftp = paramiko.SFTPClient.from_transport(trans) ssh = paramiko.SSHClient() ssh._transport = trans stdin, stdout, stderr = ssh.exec_command("mkdir CMDBClient") sftp.put("sftpDir/getData.py", "/root/CMDBClient/getData.py") sftp.put("sftpDir/sendData.py", "/root/CMDBClient/sendData.py") sftp.put("sftpDir/main.py", "/root/CMDBClient/main.py") stdin, stdout, stderr = ssh.exec_command( "python /root/CMDBClient/main.py") trans.close() result["state"] = "success" result["data"] = "操作成功!" eq.Statue = "True" except Exception as e: eq.Statue = "False" result["data"] = "远程连接错误:%s" % e finally: eq.save() else: result["data"] = "IP、port、user、password不能为空,请检查" else: result["data"] = "请求错误,请刷新重试" return JsonResponse(result)
def add_eq(request): for i in range(100): e = Equipment() e.hostname = "localhost_%s" % i e.IP = "192.168.1.%s" % (i + 2) e.System = random.choice([ "win7_32", "win7_64", "centos.6_32", "centos.7", ]) e.Statue = random.choice(["True", "False"]) e.Mac = random.choice(["00:0c:29:92:85:4e", "00:0c:29:5b:2a:a1"]) e.user = "******" e.Password = "******" e.save() return JsonResponse({"statue": "ahh"})