-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh_connector
executable file
·51 lines (43 loc) · 1.43 KB
/
ssh_connector
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
#!/bin/sh
# SSH Connector
# Detects if there is a direct connection to host and if not tries to use proxy.
# Usage:
# 1. place the file in ~/bin/ssh_connector
# 2. Edit your ~/.ssh/config and add:
# Host example.com
# ProxyCommand ~/bin/ssh_connector %h %p
netcat=/bin/nc
timeout=4
desthost=$1
destport=$2
detect_proxy() {
# TODO: use multiple proxies
proxyhost=`echo ${http_proxy} | cut -d "/" -f 3 | cut -d ":" -f 1`
proxyport=`echo ${http_proxy} | cut -d "/" -f 3 | cut -d ":" -f 2`
proxymeth=`echo ${http_proxy} | cut -d ":" -f 1`
[ "x${proxymeth}" = "xhttp" ] && proxymeth=connect
[ "x${proxymeth}" = "xsocks*" ] && proxymeth=5
[ "x${proxymeth}" = "xsocks4" ] && proxymeth=4
[ -n "$debug" ] && echo "Detected proxy:" >&2
[ -n "$debug" ] && echo " - host: ${proxyhost}" >&2
[ -n "$debug" ] && echo " - port: ${proxyport}" >&2
[ -n "$debug" ] && echo " - method: ${proxymeth}" >&2
}
direct_connection() {
if ${netcat} -w ${timeout} -z ${desthost} ${destport}
then
[ -n "$debug" ] && echo "Using direct connection." >&2
return 0
fi
[ -n "$debug" ] && echo "Direct connection failed, trying proxy." >&2
return 1
}
if direct_connection
then
# use direct connection if possible (via netcat)
exec ${netcat} ${desthost} ${destport}
else
# use netcat with proxy when direct connection is not possible
detect_proxy
exec ${netcat} ${desthost} ${destport} -X ${proxymeth} -x ${proxyhost}:${proxyport}
fi