Experimenting with pylibssh2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.9KB

  1. #!/usr/bin/env python
  2. #
  3. # pylibssh2 - python bindings for libssh2 library
  4. #
  5. # Copyright (C) 2010 Wallix Inc.
  6. #
  7. # This library is free software; you can redistribute it and/or modify it
  8. # under the terms of the GNU Lesser General Public License as published by the
  9. # Free Software Foundation; either version 2.1 of the License, or (at your
  10. # option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful, but WITHOUT
  13. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  14. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  15. # details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public License
  18. # along with this library; if not, write to the Free Software Foundation, Inc.,
  19. # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. #
  21. import socket
  22. import sys
  23. import libssh2
  24. DEBUG = False
  25. usage = """Do a SSH remote command with username@hostname
  26. Usage: sshcmd.py <hostname> <username> <publickey> <privatekey> <command>"""
  27. def my_print(args):
  28. if DEBUG:
  29. print(args)
  30. class SSHRemoteClient(object):
  31. def __init__(self, hostname, username, publickey, privatekey, passphrase='', port=22):
  32. self.username = username
  33. self.hostname = hostname
  34. self.publickey = publickey
  35. self.privatekey = privatekey
  36. self.passphrase = passphrase
  37. self.port = port
  38. self.session = libssh2.Session()
  39. self.session.set_banner()
  40. try:
  41. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  42. sock.connect((self.hostname, self.port))
  43. self.session.startup(sock)
  44. my_print(self.session.last_error())
  45. self.session.userauth_publickey_fromfile(
  46. self.username,
  47. self.publickey,
  48. self.privatekey,
  49. ''
  50. )
  51. my_print(self.session.last_error())
  52. except Exception, e:
  53. print str(e)
  54. raise Exception, self.session.last_error()
  55. self.channel = self.session.open_session()
  56. my_print(self.session.last_error())
  57. def execute(self, command="uname -a"):
  58. datas = []
  59. buffer = 4096
  60. rc = self.channel.execute(command)
  61. my_print(rc)
  62. while True:
  63. data = self.channel.read(buffer)
  64. if data == '' or data is None:
  65. break
  66. my_print(type(data))
  67. print data.strip()
  68. self.channel.close()
  69. def __del__(self):
  70. self.session.close()
  71. my_print(self.session.last_error())
  72. if __name__ == '__main__':
  73. try:
  74. if len(sys.argv) == 1:
  75. print usage
  76. sys.exit(1)
  77. src = SSHRemoteClient(
  78. sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], '')
  79. src.execute(sys.argv[5])
  80. except Exception, e:
  81. print str(e)
  82. except KeyboardInterrupt, e:
  83. sys.exit(1)