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