test_linux.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. from netmiko import ConnectHandler
  4. def main():
  5. try:
  6. hostname = raw_input("Enter remote host to test: ")
  7. username = raw_input("Enter remote username: ")
  8. except NameError:
  9. hostname = input("Enter remote host to test: ")
  10. username = input("Enter remote username: ")
  11. linux_test = {
  12. 'username': username,
  13. 'use_keys': True,
  14. 'ip': hostname,
  15. 'device_type': 'ovs_linux',
  16. 'key_file': '/home/{}/.ssh/test_rsa'.format(username),
  17. 'verbose': False}
  18. net_connect = ConnectHandler(**linux_test)
  19. print()
  20. print(net_connect.find_prompt())
  21. # Test enable mode
  22. print()
  23. print("***** Testing enable mode *****")
  24. net_connect.enable()
  25. if net_connect.check_enable_mode():
  26. print("Success: in enable mode")
  27. else:
  28. print("Fail...")
  29. print(net_connect.find_prompt())
  30. net_connect.exit_enable_mode()
  31. print("Out of enable mode")
  32. print(net_connect.find_prompt())
  33. # Test config mode
  34. print()
  35. print("***** Testing config mode *****")
  36. net_connect.config_mode()
  37. if net_connect.check_config_mode():
  38. print("Success: in config mode")
  39. else:
  40. print("Fail...")
  41. print(net_connect.find_prompt())
  42. net_connect.exit_config_mode()
  43. print("Out of config mode")
  44. print(net_connect.find_prompt())
  45. # Test config mode (when already at root prompt)
  46. print()
  47. print("***** Testing config mode when already root *****")
  48. net_connect.enable()
  49. if net_connect.check_enable_mode():
  50. print("Success: in enable mode")
  51. else:
  52. print("Fail...")
  53. print(net_connect.find_prompt())
  54. print("Test config_mode while already at root prompt")
  55. net_connect.config_mode()
  56. if net_connect.check_config_mode():
  57. print("Success: still at root prompt")
  58. else:
  59. print("Fail...")
  60. net_connect.exit_config_mode()
  61. # Should do nothing
  62. net_connect.exit_enable_mode()
  63. print("Out of config/enable mode")
  64. print(net_connect.find_prompt())
  65. # Send config commands
  66. print()
  67. print("***** Testing send_config_set *****")
  68. print(net_connect.find_prompt())
  69. output = net_connect.send_config_set(['ls -al'])
  70. print(output)
  71. print()
  72. if __name__ == "__main__":
  73. main()