UNIX自动登录telnet代码设置全解(1)(3)
明白了这个脚本的原理,我们可以做一个试验,用另外一种更加明显的方式来做输入输出的重定向。
1、新建1个普通文件touch out.log
2、新建一个管道文件:mknod commands p
3、为日志文件分配文件描述符7 : exec 7<>out.log
4、为管道文件分配文件描述符8:exec 8<>commands
5、后台方式启动UNIX自动登录telnet:telnet136.30.75.102 <&8 >&7 &
6、实时刷新输出信息: tail –f out.log
7、另外启动一个终端,输入信息:
- echo “root^M” >>commands
- echo “password^M”>>commands
- echo “pwd^M” >> commands
- ….
8、观察tail –f的输出,可以看到输入命令的执行结果。在这个脚本的基础上,可以实现UNIX自动登录telnet到一个主机上并执行一些列command的功能。脚本如下:
- #!/bin/bashtmptty=`tty`tmptty=`basename $tmptty`tmpname=`whoami`ip="136.39.75.102"inp1="root^M"inp2="Easy2get^M"
- #you may add as many commands as you want
- #NOTE:every command should end with ^M, which is input under UNIX
- #方法为按住ctrl键按v键,不放ctrl键,再按shift键和m键,完成后全部放开
- #inp3="ls^M"inp4="echo 'hello,TeMIP users' >> hello.txt^M"
- #--inputfile=inoutputfile=out.logrm -fr $inputfilerm -fr $outputfilemknod $inputfile ptouch $outputfileexec 7<>$outputfileexec 8<>$inputfiletelnet$ip <&8 >&7 &sleep 2;
- echo $inp1 >> $inputfilesleep 2;
- echo $inp2 >> $inputfileecho "executing command $inp3"sleep 2;
- echo $inp3 >> $inputfileecho "executing $inp4"sleep 2;
- echo $inp4 >> $inputfileecho "exiting"sleep 2;
- echo "exit^M" >> $inputfilerm $inputfilerm $outputfile
评论暂时关闭