1. 程式人生 > >telnet自動登入並執行任務

telnet自動登入並執行任務

autotelnet.sh可以通過建立管道的方式,實現自動登入,並自動從tftp伺服器下載測試驅動模組進行替換,最後自動退出回到登入之前的狀態。

#autotelnet.sh
#!/bin/bash
if (( $# != 2 ))
then
  echo " usage: $0 remote_ip tftp_ip "
  exit 1
fi

ip=$1
inp1="root"
inp2="password"
inp3=""
inp4="tftp -g -l helloworld.ko -r hellworld.ko $2"
inp5="mv /lib/modules/2.6.18/kernel/char/hellworld.ko /lib/modules/2.6.18/kernel/char/hellworld.ko.bak"
inp6="cp -vf hellworld.ko /lib/modules/2.6.18/kernel/char/hellworld.ko"
inp7="rm -vf /root/hellworld.ko"
inp8="exit"
inp9=""

inputfile=in
outputfile=out
rm -fr $inputfile
rm -fr $outputfile
mknod $inputfile p
touch $outputfile

#file description 7 for out and 8 for in
exec 7<>$outputfile
exec 8<>$inputfile

telnet $ip <&8 >&7 &

sleep 1; echo $inp1 >> $inputfile
sleep 1; echo $inp2 >> $inputfile
echo $inp3 >> $inputfile
echo $inp4 >> $inputfile
echo $inp5 >> $inputfile
echo $inp6 >> $inputfile
echo $inp7 >> $inputfile

tail -f $outputfile &

sleep 1; echo $inp8 >> $inputfile

echo "Bingo!!! Auto update test driver success!"