how to connect to ssh using python


SUBMITTED BY: alemotta

DATE: March 15, 2017, 6:17 p.m.

FORMAT: Text only

SIZE: 3.1 kB

HITS: 399

  1. how to connect to ssh using python
  2. What is pxssh?
  3. Pxssh is based on pexpect. It's class extends pexpect.spawn to specialize setting
  4. up SSH connections. I use pxssh frequently for making ssh connections in python.
  5. Module documentation
  6. Open up a terminal and type in the following commands to get help about the module
  7. import pxssh
  8. help(pxssh)
  9. Help on module pxssh:
  10. NAME
  11. pxssh
  12. FILE
  13. /usr/lib/python2.7/dist-packages/pxssh.py
  14. DESCRIPTION
  15. This class extends pexpect.spawn to specialize setting up SSH connections.
  16. This adds methods for login, logout, and expecting the shell prompt.
  17. $Id: pxssh.py 513 2008-02-09 18:26:13Z noah $
  18. CLASSES
  19. pexpect.ExceptionPexpect(exceptions.Exception)
  20. ExceptionPxssh
  21. pexpect.spawn(__builtin__.object)
  22. pxssh
  23. You can also see the help here http://pexpect.sourceforge.net/pxssh.html
  24. Methods and login process
  25. Pxssh adds methods for login, logout, and expecting the shell prompt.
  26. It does various tricky things to handle many situations in the SSH login process.
  27. For example, if the session is your first login, then pxssh automatically accepts
  28. the remote certificate; or if you have public key authentication setup then pxssh
  29. won't wait for the password prompt.
  30. How does pxssh works?
  31. pxssh uses the shell prompt to synchronize output from the remote host.
  32. In order to make this more robust it sets the shell prompt to something more
  33. unique than just $ or #. This should work on most Borne/Bash or Csh style
  34. shells.
  35. Example
  36. This example runs a few commands on a remote server and prints the result.
  37. First we import the modules that we need. (pxssh and getpass)
  38. We import the getpass module, which will prompt the user for a password,
  39. without echoing what they type to the console.
  40. import pxssh
  41. import getpass
  42. try:
  43. s = pxssh.pxssh()
  44. hostname = raw_input('hostname: ')
  45. username = raw_input('username: ')
  46. password = getpass.getpass('password: ')
  47. s.login (hostname, username, password)
  48. s.sendline ('uptime') # run a command
  49. s.prompt() # match the prompt
  50. print s.before # print everything before the prompt.
  51. s.sendline ('ls -l')
  52. s.prompt()
  53. print s.before
  54. s.sendline ('df')
  55. s.prompt()
  56. print s.before
  57. s.logout()
  58. except pxssh.ExceptionPxssh, e:
  59. print "pxssh failed on login."
  60. print str(e)
  61. Run a command on a remote SSH server
  62. Let's show one more example. To run a command ('uptime') and to print the output,
  63. you need to do something like that :
  64. import pxssh
  65. s = pxssh.pxssh()
  66. if not s.login ('localhost', 'myusername', 'mypassword'):
  67. print "SSH session failed on login."
  68. print str(s)
  69. else:
  70. print "SSH session login successful"
  71. s.sendline ('uptime')
  72. s.prompt() # match the prompt
  73. print s.before # print everything before the prompt.
  74. s.logout()
  75. #We can also execute multiple command like this:
  76. s.sendline ('uptime;df -h')

comments powered by Disqus