wssh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. if __name__ == '__main__':
  3. from wssh import client
  4. import os
  5. import sys
  6. import argparse
  7. import getpass
  8. import urllib2
  9. parser = argparse.ArgumentParser(
  10. description='wssh - SSH Over WebSockets Client')
  11. parser.add_argument('--host', '-H',
  12. help='WSSH server host (default: 127.0.0.1)',
  13. default='127.0.0.1')
  14. parser.add_argument('--port', '-P',
  15. help='WSSH server port (default: 5000)',
  16. type=int,
  17. default=5000)
  18. parser.add_argument('--password', '-p',
  19. nargs='?',
  20. const='',
  21. help='Password-based authentication. ' \
  22. 'If no password is provided you will be prompted for one')
  23. parser.add_argument('--key', '-k',
  24. nargs='?',
  25. const='',
  26. help='Private key authentication. ' \
  27. 'Selects a file from which the private key ' \
  28. 'for RSA or DSA authentication is read. ' \
  29. 'The default is ~/.ssh/id_rsa and ~/.ssh/id_dsa.')
  30. parser.add_argument('--key-passphrase', '-K',
  31. nargs='?',
  32. const='',
  33. help='Provide a passphrase for encrypted private key files.')
  34. parser.add_argument('--ssh-port', '-s',
  35. type=int,
  36. default=22,
  37. help='Ssh to server host"s port')
  38. parser.add_argument('destination',
  39. help='[user@]hostname')
  40. parser.add_argument('command',
  41. nargs='*',
  42. help='optional command to be executed')
  43. args = parser.parse_args()
  44. if '@' in args.destination:
  45. (username, hostname) = args.destination.split('@', 1)
  46. else:
  47. (username, hostname) = (getpass.getuser(), args.destination)
  48. if args.password == '':
  49. password = getpass.getpass('Password: ')
  50. else:
  51. password = args.password
  52. if args.key_passphrase == '':
  53. key_passphrase = getpass.getpass('Enter passphrase for private key: ')
  54. else:
  55. key_passphrase = args.key_passphrase
  56. key = None
  57. if args.key == '':
  58. key_files = ['~/.ssh/id_rsa', '~/.ssh/id_dsa']
  59. for path in key_files:
  60. path = os.path.expanduser(path)
  61. if os.path.exists(path):
  62. key = file(path).read()
  63. break
  64. if key is None:
  65. print >> sys.stderr, 'Error: Unable to locate identity file {0}' \
  66. .format(' or '.join(key_files))
  67. sys.exit(1)
  68. elif args.key is not None:
  69. if not os.path.exists(args.key):
  70. print >> sys.stderr, 'Error: Identity file "{0}" does not exist' \
  71. .format(args.key)
  72. sys.exit(1)
  73. key = file(args.key).read()
  74. params = {
  75. 'password': password,
  76. 'port': str(args.ssh_port),
  77. 'private_key': key,
  78. 'key_passphrase': key_passphrase,
  79. 'run': ' '.join(args.command) if args.command else None,
  80. }
  81. # Filter empty parameters
  82. params = dict(filter(lambda (k, v): v is not None, params.iteritems()))
  83. endpoint = 'ws://{serv_host}:{serv_port}/wssh/{host}/{user}?{params}' \
  84. .format(
  85. serv_host=args.host,
  86. serv_port=args.port,
  87. host=urllib2.quote(hostname),
  88. user=urllib2.quote(username),
  89. params='&'.join(['{0}={1}'.format(k, urllib2.quote(v))
  90. for (k, v) in params.iteritems()]))
  91. try:
  92. client.invoke_shell(endpoint)
  93. except client.ConnectionError as e:
  94. print >>sys.stderr, 'wssh: {0}'.format(e.message or 'Connection error')
  95. else:
  96. print >>sys.stderr, 'Connection to {0} closed.'.format(hostname)