@csrf_exempt
def execute_multi_remote_command(request):
# request_dict = json.loads(request.body)
# cmd = request_dict.get("cmd")
ip = '192.168.1.14'
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=ip, port=22, username='root', password='111111')
commands = ['ls', 'who', 'uname -a']
results = {}
for cmd in commands:
# 執行命令
stdin, stdout, stderr = ssh.exec_command(cmd)
# 獲取命令結果
result = stdout.read().decode('utf-8')
error = stderr.read().decode('utf-8')
# 如果有錯誤資訊,新增到結果中
#if error:
# results[cmd] = error
#else:
# results[cmd] = result
results[cmd] = result
ssh.close()
print(results)
#return HttpResponse(results, content_type="text/plain")
return JsonResponse(results, safe=False)