-
Notifications
You must be signed in to change notification settings - Fork 33
/
jrc
executable file
·96 lines (80 loc) · 1.99 KB
/
jrc
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
#!/usr/bin/env bash
# @AUTHOR: Chun-Jie Liu
# @CONTACT: chunjie.sam.liu.at.gmail.com
# @DATE: 2019-07-12 00:18:08
# @DESCRIPTION: jrocker connect through jump Guo4
# IP LIST
ipfile=${HOME}/.ssh/known_iplist
[[ ! -s ${ipfile} ]] && {
echo "Error: ${ipfile} does not exist."
exit 1
}
# check the mode of known_iplist
[[ $(stat -f "%OLp" ${ipfile}) -ne 600 ]] && {
echo "Warning: The mode of file ${ipfile} is not 700. chmod 600 for ${ipfile}."
chmod 600 ${ipfile}
}
# load ipmaps
declare -a ipmaps
declare -a servernames
readarray known_ips < ${ipfile}
param=$#
server=$1
remote_port=${2:-8686}
local_port=${3:-8686}
function fn_errorinfo {
echo "Usage:"
echo " jrc [server] [remote_port|local_port]"
echo "For example1:"
echo " jrc 1 ."
echo " example2:"
echo " jrc 1 8686"
echo " example3:"
echo " jrc 1 8686 9696."
}
function fn_usage {
[[ ${param} -lt 1 || ${param} -gt 3 ]] && {
echo "Error: Parameters should be in 1, 2, 3, ."
echo "****************************************************"
fn_errorinfo
exit 1
}
[[ ! (${server} -eq 1 || ${server} -eq 10) ]] && {
echo "Error: Server must be 1 or 10."
echo "****************************************************"
fn_errorinfo
exit 1
}
}
function fn_load_iplist {
for line in ${known_ips[@]};
do
line=${line//\~/ }
line=(${line})
# save ips
ipmaps[${line[1]}]=${line[0]}
# save server names
servernames[${line[1]}]=${line[2]}
done
}
function fn_parse_ip {
local ip_str="$1"
line=${ip_str//\`/ }
echo $line
}
function fn_connect {
cmd="sshpass -p '${loginfo[3]}' ssh -p ${jump_proxy[2]} -N -f -L localhost:${local_port}:${loginfo[1]}:${remote_port} ${jump_proxy[0]}@${jump_proxy[1]}"
echo ${cmd}
echo "****************************************************"
eval ${cmd}
exit 0
}
function main {
fn_usage
# load ip list
fn_load_iplist
jump_proxy=($(fn_parse_ip ${ipmaps[4]}))
loginfo=($(fn_parse_ip ${ipmaps[$server]}))
fn_connect
}
main