spermspace

Untitled

Sep 4th, 2018
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. Python 3:
  2. sudo apt-get install build-essential libssl-dev libffi-dev python3-dev
  3. sudo pip3 install cryptography
  4. sudo pip3 install paramiko
  5. $ python3
  6. Python 3.5.2 (default, Nov 17 2016, 17:05:23)
  7. [GCC 5.4.0 20160609] on linux
  8. Type "help", "copyright", "credits" or "license" for more information.
  9. >>> import paramiko
  10. >>>
  11. The Paramiko overview
  12. Let's look at a quick example using the Python 3 interactive shell:
  13. >>> import paramiko, time
  14. >>> connection = paramiko.SSHClient()
  15. >>> connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  16. >>> connection.connect('172.16.1.20', username='cisco', password='cisco',
  17. look_for_keys=False, allow_agent=False)
  18. >>> new_connection = connection.invoke_shell()
  19. >>> output = new_connection.recv(5000)
  20. >>> print(output)
  21. b"rn***********************************************************************
  22. ***rn* IOSv is strictly limited to use for evaluation, demonstration and
  23. IOS *rn* education. IOSv is provided as-is and is not supported by Cisco's
  24. *rn* Technical Advisory Center. Any use or disclosure, in whole or in part,
  25. *rn* of the IOSv Software or Documentation to any third party for any *rn*
  26. purposes is expressly prohibited except as otherwise authorized by *rn*
  27. Low-Level Network Device Interactions
  28. [ 59 ]
  29. Cisco in writing.
  30. *rn************************************************************************
  31. **rniosv-1#"
  32. >>> new_connection.send("show version | i Vn")
  33. 19
  34. >>> time.sleep(3)
  35. >>> output = new_connection.recv(5000)
  36. >>> print(output)
  37. b'show version | i VrnCisco IOS Software, IOSv Software (VIOSADVENTERPRISEK9-M),
  38. Version 15.6(2)T, RELEASE SOFTWARE (fc2)rnProcessor
  39. board ID 9MM4BI7B0DSWK40KV1IIRrniosv-1#'
  40. >>> new_connection.close()
  41. >>>
  42. The time.sleep() function inserts time delay to ensure that all the
  43. outputs were captured. This is particularly useful on a slower network
  44. connection or a busy device. This command is not required but
  45. recommended depending on your situation.
  46. Even if you are seeing the Paramiko operation for the first time, the beauty of Python and
  47. its clear syntax means that you can make a pretty good educated guess at what the program
  48. is trying to do:
  49. >>> import paramiko
  50. >>> connection = paramiko.SSHClient()
  51. >>> connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  52. >>> connection.connect('172.16.1.20', username='cisco', password='cisco',
  53. look_for_keys=False, allow_agent=False)
  54. The first four lines create an instance of the SSHClient class from Paramiko. The next line
  55. sets the policy that the client should use when the SSH server's hostname, in this case
  56. iosv-1, is not present in either the system host keys or the application's keys. In this case,
  57. we will just automatically add the key to the application's HostKeys object. At this point, if
  58. you log onto the router, you will see the additional login session from Paramiko:
  59. iosv-1#who
  60. Line User Host(s) Idle Location
  61. *578 vty 0 cisco idle 00:00:00 172.16.1.1
  62. 579 vty 1 cisco idle 00:01:30 172.16.1.173
  63. Interface User Mode Idle Peer Address
  64. iosv-1#
  65. The next few lines invokes a new interactive shell from the connection and a repeatable
  66. pattern of sending a command and retrieves the output. Finally we close the connection.
Add Comment
Please, Sign In to add comment