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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/bin/bash
#auth:codewalker
#mail:simon.xie@codewalker.me

#do remote script ssh $host "ret=0" 2>&1

IS\_admin\_MODE=false
C\_MODE\_FUNCTION_LISTS='\[rwho\]\[rusers\]\[rhost\]\[connect\]\[rsu\]\[rpasswd\]\[rfinger\]\[rwrite\]'
A\_MODE\_FUNCTION_LISTS='\[rhost\]\[ausers\]\[afinger\]\[rlistusers\]'
PING_COUNT=1
PING_TIMEOUT=1
HOST\_LIST\_FILE='/tmp/host_list'
USER\_LIST\_FILE='/tmp/user_list'
START_TIME=$(date +%s)

function help(){
cat <\> " is\_set\_ip_range
if \[ $is\_set\_ip_range = 'y' \]; then
echo "Please type a range like: xxx.xxx.xxx"
read -p ">> " local\_ip\_format
echo "Now, scanning..."
else
#ONLY for centos.
local\_ip\_format=$(get\_local\_IP)
echo "choosed N, use default:$local\_ip\_format"".x"
fi
echo 'this may take a few minutes, please wait...'
echo 'Hosts:'
for (( i = 2; i < 256; i++)); do
#find host.
ping -c$PING\_COUNT -w$PING\_TIMEOUT $local\_ip\_format'.'$i|grep "bytes from"|awk -F: '{print $1}'|awk -F'from' '{print $2}' |tee -a $HOST\_LIST\_FILE
done
count_host
unset local\_ip\_format
}

function count_host() {
#to count how many lines in this file.
count=$(wc -l $HOST\_LIST\_FILE|awk -F" " '{print $1}')
echo "there are $count hosts."
unset count
}

function rhost() {
#OPTION 1
#notes that the IP address is NOT same as mien.
#echo $local\_ip\_format{1..254}|xargs -n1 -P0 ping -c1|grep "bytes from"
#################################
#OPTION 2
if \[ -f /tmp/host_list \]; then
echo '\[host_list\] EXIST.'
echo "Do you need rescan host?\[y/n\] "
read -p ">> " is_rescan
if \[ $is_rescan = 'y' \]; then
rm -fv $HOST\_LIST\_FILE
time scan_HOST #how much time for scan host.
else
cat $HOST\_LIST\_FILE
count_host
fi
else
echo "Do you need scan host?\[y/n\] "
read -p ">> " is_scan
if \[ $is_scan = "y" \]; then
rm -fv $HOST\_LIST\_FILE
time scan_HOST
else
if \[ -f /tmp/host_list \]; then
cat $HOST\_LIST\_FILE
count_host
else
echo "$HOST\_LIST\_FILE NO FOUND!"
fi
fi

fi

}

function connect() {
#connect to host by \[ssh\].
echo 'Type a ip address to login: '
read -p '>>' t_ip
#check host
if \[ $(grep -c $t\_ip /tmp/host\_list) -eq 0 \];then
echo "$t_ip not boot..."
do_exit
fi
#check user
echo 'Type a username: '
read -p '>>' t_username
if \[ $(grep -c $t\_username /tmp/user\_list) -eq 0 \]; then
echo "Unauthorized. $t_username CANNOT login..."
do_exit
fi
ssh $t\_username@$t\_ip
unset t_username
unset t_ip
}

function rsu() {
su
}

function rpasswd() {
#set password.
echo 'Type a username to change password for: '
read -p '>>' t_username
passwd $t_username
unset t_username
}

function rfinger() {
#get information
echo 'aaa'
echo 'Type a username to displays information about: '
read -p '>>' t_username
finger $t_username
unset t_username
}

function rwrite() {
#send message for other user(s) in same host.
echo 'Type some words for other users.'
read -p '>>' t_message
wall $t_message
unset t_message
}

function rlistusers() {
if \[ -f /tmp/user_list \]; then
awk -F: '{print $2}' $USER\_LIST\_FILE
else
echo "$USER\_LIST\_FILE NO FOUND, do you wanna add user?\[y\]"
read -p ">> " is\_add\_user
if \[ $is\_add\_user = 'y' \];then
ausers
else
echo 'choosed no, exit...'
do_exit
fi
fi
}

function ausers() {
if \[ -f /tmp/user_list \]; then
echo "FOUND $USER\_LIST\_FILE."
awk -F: '{print $2}' $USER\_LIST\_FILE
echo "Do you rewrite it?\[y\]"
read -p ">> " is\_rewrite\_user_list
if \[ $is\_rewrite\_user_list = 'y' \]; then
rm -fv $is\_rewrite\_user_list
fi
fi
echo "input host IP which you need to add (field-separator: >>,<< )"
read -p ">> " t_hosts
IFS=',' read -a t\_arr\_hosts <<< "${t_hosts}"
echo "you will add \[${#t\_arr\_hosts\[@\]}\] hosts"
for host in "${t\_arr\_hosts\[@\]}" "${!array\[@\]}"
do
#echo "input this remote host\[$host\] login username."
#read -p ">> " r_username
#echo "input remote host\[$host\] login password."
#read -p ">> " r_password
#echo "can you longin\[$host\] without password?\[y\] "
#read -p ">> " without_psd
#if \[ $without_psd = 'y' \]; then
echo "add user on $host."
echo "input usernames (add more than one,field-separator: >>,<< )"
read -p ">> " t_users
IFS=',' read -a t\_arr\_username <<< "${t_users}"
echo "you will add \[${#t\_arr\_username\[@\]}\] users on $host"
for username in "${t\_arr\_username\[@\]}"
do
echo "set password for \[$username\] on $host"
read -p ">> " t_pwd
#echo "###$t\_pwd for $username on $r\_username@$host:$r_password \[DONE\]###"
echo "###$t_pwd for $username @$host \[DONE\]###"
echo
echo "$host:$username:$t\_pwd" >> $USER\_LIST_FILE
done
#else
# echo "can't longin remote host."
#fi
done
echo "add all user \[DONE\]"
unset t_hosts
unset t\_arr\_hosts
unset host
unset t_users
unset username
}


#connect mode
function CONNECT_MODE() {
while true;do
echo 'select a function\[\*exit\* to exit,\*help\* for list functions\]:'
read -p ">>" F
case $F in
rwho)
rwho
;;
rusers)
rusers
;;
rhost)
rhost
;;
connect)
connect
;;
rsu)
su
;;
rpasswd)
rpasswd
;;
rfinger)
rfinger
;;
rwrite)
rwrite
;;
help)
C_HELP
;;
exit)
do_exit
exit 1
;;
*)
echo '!!!unknow argument!!!'
echo $C\_MODE\_FUNCTION_LISTS
esac
done
}

#admin mode
function ADMIN_MODE() {
while true;do
echo 'select a function\[\*exit\* to exit,\*help\* for list functions\]:'
read -p ">>" F
case $F in
ausers)
ausers
;;
rhost)
rhost
;;
afinger)
rfinger
;;
rwho)
rwho
;;
rusers)
rusers
;;
connect)
connect
;;
rsu)
su
;;
rpasswd)
rpasswd
;;
rwrite)
rwrite
;;
rlistusers)
rlistusers
;;
help)
A_HELP
;;
exit)
do_exit
exit 1
;;
*)
echo '!!!unknow argument!!!'
echo $A\_MODE\_FUNCTION_LISTS
esac
done
}
####main#####
if \[ $# -eq 0 \]; then
echo 'no argument '
echo 'Please select a mode to using.'
echo 'c for \[connect\]. a for\[admin\]'
read -p '>>' MODE
case $MODE in
c)
echo "Using connect mode"
IS\_admin\_MODE=false
echo $C\_MODE\_FUNCTION_LISTS
CONNECT_MODE
;;
a)
echo "Using admin mode"
IS\_admin\_MODE=true
echo $A\_MODE\_FUNCTION_LISTS
ADMIN_MODE
;;
?)
echo "unkonw mode"
help
exit 1
;;
esac
do_exit
exit 1
fi

while getopts "ac" arg
do
case $arg in
c)
echo "Using connect mode"
IS\_admin\_MODE=false
echo $C\_MODE\_FUNCTION_LISTS
CONNECT_MODE
;;
a)
echo "Using admin mode"
IS\_admin\_MODE=true
echo $A\_MODE\_FUNCTION_LISTS
ADMIN_MODE
;;
?)
echo "unkonw argument"
help
exit 1
;;
esac
done