公司实验室有很多机器,公司所在的科技城老是停电,没有办法我们就老是要全部关机。
下面是我写的一个自动关机系统。
用法:shutdown.sh 59.151.13.0/24 333 root pass shutdown -h now
[jamesg@localhost shutdown]$ ll
总计 8
-rw-rw-r-- 1 jamesg jamesg 431 01-21 16:05 shutdown.exp
-rwxrwxr-x 1 jamesg jamesg 707 01-22 13:54 shutdown.sh
[jamesg@localhost shutdown]$ cat shutdown.exp
if {$argc<4} {
puts stderr "Usage: $argv0 server port login passwaord.\n "
exit 1
}
set SERVER [lindex $argv 0]
set PORT [lindex $argv 1]
set LOGIN [lindex $argv 2]
set PASS [lindex $argv 3]
set COMMAND [lindex $argv 4]
set flag 1
spawn ssh -p $PORT $SERVER -l $LOGIN $COMMAND
while {$flag > 0} {
expect {
"\(yes\/no\)\?" { send "yes\n" }
"assword:" { send "$PASS\n" }
eof { set flag 0 }
}
}
[jamesg@localhost shutdown]$ cat shutdown.sh
#!/bin/bash
if [ $# -lt 5 ];then
echo "Usage: $0 subnet PORT username password command"
echo " e.g.: $0 59.151.13.0/24 333 root pass shutdown -h now"
exit 1
fi
for j in 1 2 3 4 5
do
case $j in
1) SUBNET=$1
;;
2) PORT=$1
;;
3) USER=$1
;;
4) PASSWORD=$1
;;
5) COMMAND=$@
;;
esac
shift
done
nmap -n -sT $SUBNET -p $PORT >temp.nmap.log
if [ $? -eq 0 ];then
cat temp.nmap.log |grep Interesting|nl >temp.nmap.log1
cat temp.nmap.log |grep ${PORT}/tcp|nl >temp.nmap.log2
join temp.nmap.log1 temp.nmap.log2 |awk '{print $5$7}'|grep open|awk -F : '{print $1}' >temp.hostlist
for i in `cat temp.hostlist`
do
expect shutdown.exp $i ${PORT} ${USER} $PASSWORD "$COMMAND"
done
fi
rm -f temp.*
[jamesg@localhost shutdown]$
阅读(1069) | 评论(0) | 转发(0) |