Advertisement
Guest User

Untitled

a guest
Apr 28th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. def configure(request):
  2. if request.method == "POST":
  3. select_device_id = request.POST.getlist('device')
  4. cisco_command = request.POST['cisco_command'].splitlines()
  5. other_command = request.POST['other_command'].splitlines()
  6. for x in select_device_id:
  7. try:
  8. dev = get_object_or_404(Device, pk=x)
  9. ssh_client = paramiko.SSHClient()
  10. ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  11. ssh_client.connect(hostname=dev.ip_address, username=dev.username, password=dev.password)
  12. if dev.vendor.lower() == 'cisco':
  13. conn = ssh_client.invoke_shell()
  14. conn.send("conf t\n")
  15. for cmd in cisco_command:
  16. conn.send(cmd + "\n")
  17. time.sleep(1)
  18. else:
  19. for cmd in other_command:
  20. ssh_client.exec_command(cmd)
  21. log = Log(target=dev.ip_address, action="configure", status="Success", time=datetime.now(), messages="No Error")
  22. log.save()
  23. except Exception as e:
  24. # pesan = re.findall("respond\Z", e)
  25. # if (pesan):
  26. # log = Log(target=dev.ip_address, action="configure", status="Success", time=datetime.now(), messages="unrecheable")
  27. # else:
  28. # log = Log(target=dev.ip_address, action="configure", status="Success", time=datetime.now(), messages=e)
  29. log = Log(target=dev.ip_address, action="configure", status="Error", time=datetime.now(), messages=e)
  30. log.save()
  31. return redirect('home')
  32.  
  33. else:
  34. devices = Device.objects.all()
  35. context = {
  36. 'devices' : devices,
  37. 'mode' : 'Configure'
  38. }
  39. return render(request, 'config.html', context)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement