more simple and clear and smart
[hombase.git] / other-sh / mt.sh
blob34cdc0d9e86d58a06e32f19c25b073e2f6dabca9
1 #!/bin/sh
3 #mt.sh-- _describe_
4 #@Author: LiFan
5 #@Created: 2011-01-22-15:20
7 PATH=/bin:/usr/bin:/usr/local/bin
9 error()
11 echo "$@" 1>&2
12 usage_and_exit 1
15 usage()
17 echo "Usage: $PROGRAM [--list] [--all|-a] [--athome|-m] [--proxy|-x] [--help] [--version] [host-name]"
18 echo -e "for example:"
19 echo -e "\tmt.sh --list"
20 echo -e "\tmt.sh qapc29"
21 echo -e "\tmt.sh -x 192.168.79.xx tu98"
22 echo -e "\tmt.sh x103-22"
25 usage_and_exit()
27 usage
28 exit $1
31 version()
33 echo "$PROGRAM version $VERSION"
36 warning()
38 echo "$@" 1>&2
39 EXITCODE=$((EXITCODE + 1))
40 #EXITCODE=`expr $EXITCODE + 1`
44 EXITCODE=0
45 PROGRAM=`basename $0`
46 VERSION=1.0
47 HOMEPROXY=192.168.1.105
48 SECONDPROXY=192.168.72.103
49 THIRDPROXY=
50 QAPROXY=qaproxy
51 all=no
52 athome=no
54 hostname=`hostname`
55 if [[ $hostname = "football12" ]]
56 then
57 athome=yes
60 option=conn
62 while test $# -gt 0
64 case $1 in
65 --all | -a)
66 all=yes
68 --help | -h)
69 usage_and_exit 0
71 --version | -v)
72 version
73 exit 0
75 --list | -l)
76 option=list
78 --athome| -m)
79 athome=yes
81 --proxy| -x)
82 shift
83 THIRDPROXY=$1
85 -*)
86 error "Unrecognized option: $1"
89 break
91 esac
92 shift
93 done
95 #----------- end of phrase argument ---------------#
97 # make tunnel from local to destination by ssh server
98 # $1 local port
99 # $2 remote host name
100 # $3 remote port
101 # $4 sshd server
102 # $5 record string for sed
103 mt()
105 echo " "
106 echo "connect from local port $1 to $4 for $2:$3 ..."
107 ssh -N -f -L *:$1:$2:$3 $4
109 pid=`ps -ef|grep ssh$|tail -1|awk '{print $2}'`
110 echo -e "\t$2:$3 $pid"
111 if grep -q $5 $HOME/tunnel.list
112 then
113 echo -e "\tyes, $5 in tunnel.list"
114 sed -e "s/$5.*$/$5 $pid/g" $HOME/tunnel.list -i
115 else
116 echo "\tno, $5 not in tunnel.list, add it"
117 echo $5 $pid >> $HOME/tunnel.list
122 # make tunnel from local to destination by ssh server
123 # $1:local port $2:remote host name $3:remote port
124 maketunnel()
126 if [ $athome = "yes" ]
127 then
128 echo "nothing for $2:$3 now"
129 #mt $1 $SECONDPROXY $1 $HOMEPROXY "$2:$3"
130 else
131 mt $1 $2 $3 $QAPROXY "$2:$3"
136 listtunnel()
138 cat $HOME/tunnel.list | while read line
140 if ps -ef|grep `echo $line|cut -d " " -f2` |grep -q ssh
141 then
142 echo $line
143 else
144 echo $line |cut -d " " -f1
146 done
149 if [ $option = "list" ]
150 then
151 listtunnel
152 exit 0
155 if [ $# -eq 0 -a $all != "yes" ]
156 then
157 usage_and_exit 0
161 if [ $all = "yes" ]
162 then
163 echo "use all setting, means connet to qasox02, qapc29 and others ..."
164 tunnel="qasox02 qapc29 others"
165 else
166 echo "connet to $* ..."
167 tunnel=$*
170 for i in $tunnel
172 case $i in
173 qasox02)
174 maketunnel 10002 "qasox02.grid.datasynapse.com" 22
176 qasox06)
177 maketunnel 10006 "qasox06.grid.datasynapse.com" 22
179 qa51)
180 maketunnel 10051 "qa51.grid.datasynapse.com" 22
182 qapc42)
183 maketunnel 9942 "qapc42.grid.datasynapse.com" 3389
185 qapc42-22)
186 maketunnel 9242 "qapc42.grid.datasynapse.com" 22
188 qapc45)
189 maketunnel 9945 "qapc45.grid.datasynapse.com" 3389
191 qapc29)
192 maketunnel 9929 "qapc29.grid.datasynapse.com" 3389
194 qapc29-22)
195 maketunnel 9229 "qapc29.grid.datasynapse.com" 22
197 qapc59)
198 maketunnel 9959 "qapc59.grid.datasynapse.com" 3389
200 tu58)
201 mt 20058 "192.168.1.58" 3389 $THIRDPROXY "192.168.1.58:3389"
203 tu58-22)
204 mt 22058 "192.168.1.58" 22 $THIRDPROXY "192.168.1.58:22"
206 tu98)
207 mt 20098 "192.168.1.98" 3389 $THIRDPROXY "192.168.1.98:3389"
209 tu98-22)
210 mt 22098 "192.168.1.98" 443 $THIRDPROXY "192.168.1.98:443"
212 x103)
213 mt 30103 "192.168.72.103" 3389 $HOMEPROXY "192.168.72.103:3389"
215 x103-22)
216 mt 32103 "192.168.72.103" 22 $HOMEPROXY "192.168.72.103:22"
218 others)
219 if [ $athome = "yes" ]
220 then
221 mt 30195 192.168.67.195 3389 $HOMEPROXY "192.168.67.195:3389"
222 mt 32195 192.168.67.195 22 $HOMEPROXY "192.168.67.195:22"
223 else
224 maketunnel 9965 "qapc65.grid.datasynapse.com" 3389
225 maketunnel 9968 "qapc68.grid.datasynapse.com" 3389
226 maketunnel 9973 "qapc73.grid.datasynapse.com" 3389
229 esac
230 done