-
Notifications
You must be signed in to change notification settings - Fork 0
/
2clab
executable file
·187 lines (174 loc) · 3.72 KB
/
2clab
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
#set -x
function usage {
cat << endl
Description:
This tool is use to login to clould lab with root user, and send your ssh key to remote
node. Then you do not have to input password for the second time.
usage:
`basename $0` [-l|-f] [clould lab id] [node number]
Example:
1. 2clab 559 4 #login to clould lab 559 node4
2. 2clab -f 559 4 ./test.txt #send the test.txt file to clab559node04 /var/tmp
endl
}
function init {
LOGIN_ADDR="";
host="";
node="";
PUTTY_ON="FALSE";
ABS=$(readlink -f $0)
DIRNAME=$(dirname ${ABS})
}
function getAddress {
host="clab$1"
getNode $2
LOGIN_ADDR="root@${host}${node}.netact.nsn-rdnet.net"
return $?
}
function getNode {
charNum=$(echo -n $1|wc -m)
echo $1|grep -E "^0[0-9]+$" 2>&1 > /dev/null
if [[ $? -eq 0 ]]; then
isPrefixzero=true;
if [[ $charNum -gt 2 ]]; then
#echo "large than 9"
node="node"$(echo -n $1|sed 's/^0//')
else
#echo "between 0 - 9"
node="node$1"
fi
else
isPrefixzero=false;
if [[ $charNum -gt 1 ]]; then
#echo "large than 9"
node="node$1"
else
#echo "bwteen 0 - 9"
node="node0$1"
fi
fi
}
function copyKey {
(${DIRNAME}/copykey_expect.sh $1)
return $?
}
function sendKey {
grep $LOGIN_ADDR ~/.ssh/known_clab.txt 2>&1 > /dev/null
if [[ $? -ne 0 ]];then
if [ ! -e "$HOME/.ssh/id_rsa.pub" ]; then
echo "[2clab] ssh key file not found, try to generate"
ssh-keygen -t rsa;
if [[ $? -eq 0 ]]; then
echo "[2clab] generate ssh-key successful";
else
echo "[2clab] generate ssh-key failed";
exit 1;
fi
fi
#ssh-copy-id -o StrictHostKeyChecking=no -oCheckHostIP=no -i ~/.ssh/id_rsa.pub $LOGIN_ADDR
copyKey $LOGIN_ADDR
if [ $? -ne 0 ]; then
ssh-copy-id -o StrictHostKeyChecking=no -oCheckHostIP=no -i ~/.ssh/id_rsa.pub $LOGIN_ADDR
fi
echo $LOGIN_ADDR >> ~/.ssh/known_clab.txt
fi
}
function validP {
while [[ $1 != "" ]]; do
echo $1|grep "^[0-9]*$" 2>&1 > /dev/null
if [[ $? -ne 0 ]];then
echo "input perameter was invalid"
usage;
exit 1
fi
shift
done
}
function parseArg {
while [[ -n "$1" ]]; do
case $1 in
-p|--putty )shift
PUTTY_ON="YES";
echo "putty is set";
doLogin;
;;
-l|--lab) shift
validP $1 $2;
getAddress $1 $2;
shift
shift
doLogin;
;;
-v|--vnc) shift
validP $1 $2;
getAddress $1 $2;
shift
shift
doVnc $1;
shift
;;
-f|--file)shift
validP $1 $2;
getAddress $1 $2;
shift
shift
sendFile $1;
shift;
;;
-h|-help) shift
usage;
exit 0;
;;
*)
validP $1 $2;
getAddress $1 $2
shift
shift
doLogin;
;;
esac
done
}
# cleanup loginaddr linenumberNeedToCleanupIn ~/.ssh/known_hosts
function cleanup {
addr=$1;
host="${host}${node}.netact.nsn-rdnet.net";
sed "/$addr/"d ~/.ssh/known_clab.txt >> ~/.ssh/known_clab.new
mv ~/.ssh/known_clab.new ~/.ssh/known_clab.txt
ssh-keygen -R $host;
}
function doVnc {
vncid=$1;
VNC_PASS_FILE="$HOME/.vnc/passwd_$LOGIN_ADDR";
if [[ -f $VNC_PASS_FILE ]]; then
xtightvncviewer $host$node.netact.nsn-rdnet.net:$vncid -passwd ~/.vnc/passwd_$LOGIN_ADDR &
else
scp $LOGIN_ADDR:~/.vnc/passwd ~/.vnc/passwd_$LOGIN_ADDR;
xtightvncviewer $host$node.netact.nsn-rdnet.net:$vncid -passwd ~/.vnc/passwd_$LOGIN_ADDR &
fi
}
function doLogin {
sendKey $LOGIN_ADDR
if [[ $PUTTY_ON = "YES" ]]; then
putty ${host}${node}.netact.nsn-rdnet.net -l root &
else
ssh -o StrictHostKeyChecking=no -oCheckHostIP=no -X $LOGIN_ADDR
if [ $? -ne 0 ];then
cleanup $LOGIN_ADDR
fi
fi
}
function sendFile {
sendKey $LOGIN_ADDR;
if [[ -d $1 ]]; then
scp -r $1 $LOGIN_ADDR:/var/tmp;
elif [[ -e $1 ]]; then
scp $1 $LOGIN_ADDR:/var/tmp;
else
echo "file: $1 not exist"
exit 1;
fi
}
init
parseArg $@