-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh-tun-vpn.sh
executable file
·106 lines (92 loc) · 2.38 KB
/
ssh-tun-vpn.sh
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
#!/bin/bash
########################
# script does not work #
########################
max_tun_num=100
tun_if_num=
root_id=0
remote_ip=
remote_tun_ip=192.168.100.1 local_tun_ip=192.168.100.2
local_router=
default_iface=
function get_router_to_remote()
{
declare lacal router_ip=$(traceroute --max-hops=1 ${remote_ip} 2> /dev/null | tail -n 1 | egrep -o '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | tail -n 1)
echo "${router_ip}"
}
function get_inet_iface()
{
declare local iface=$(ip route show 2> /dev/null | grep default | awk '{print $5}')
echo "${iface}"
}
function valid_ip()
{
OIFS="${IFS}" IFS='.' ip=($1) IFS="${OIFS}"
if (( ip[0] > 255 || ip[0] < 1 || ip[1] > 255 || ip[1] < 0 || ip[2] > 255 || ip[2] < 0 || ip[3] > 255 || ip[3] < 0 ))
then
return 1
else
return 0
fi
}
function setup_local_connection()
{
sleep 10
ip link set dev tun${tun_if_num} up
ip addr add ${local_tun_ip}/24 dev tun${tun_if_num}
ip route add ${remote_ip}/32 via ${local_router} dev ${default_iface}
ip route del default via ${local_router} dev ${default_iface}
ip route add default via ${remote_tun_ip} dev tun${tun_if_num}
}
function make_ssh_connection()
{
#ssh "[email protected]" -w 100:100 << EOF
ssh root@${1} -o Tunnel=yes -o IdentityFile=/home/ivr/.ssh/id_ed25519.key -w ${2}:${2} -p${3} << EOF
sleep 1
ip link set dev tun${2} up
ip addr add ${remote_tun_ip}/24 dev tun${2}
echo 1 > /proc/sys/net/ipv4/ip_forward
EOF
#ip link set dev "tun${2}" up
#ip addr add "${remote_tun_ip}/24" dev "tun${2}"
#iptables -t nat -A POSTROUTING -s "${local_tun_ip}" -j SNAT --to-source "${1}"
}
#ENTRY POINT
if [[ $# < 2 ]]
then
echo "usage $0: remote_ssh_server_ip remote_ssh_port"
exit 1
fi
if ! valid_ip $1
then
echo "ip address $1 is not valid"
exit 1
fi
if [[ $UID != $root_id ]]
then
echo "must have root privileges"
exit 1
fi
#RANDOM=$(date +%s)
#tun_if_num=$(( "$RANDOM" % "$max_tun_num" ))
tun_if_num=100
remote_ip="${1}"
local_router=$(get_router_to_remote)
if [ "${local_router}" = "" ]
then
echo "cant get local router"
exit 1
fi
default_iface=$(get_inet_iface)
if [ "${default_iface}" = "" ]
then
echo "cant get default iface"
exit 1
fi
echo "tun iface is: tun${tun_if_num}"
echo "remote ip is: ${remote_ip}"
echo "local router is: ${local_router}"
echo "default iface is: ${default_iface}"
make_ssh_connection "${remote_ip}" "${tun_if_num}" ${2} &
setup_local_connection "${remote_ip}" "${tun_if_num}"
exit 0