明白了这个脚本的原理,我们可以做一个试验,用另外一种更加明显的方式来做输入输出的重定向。

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、另外启动一个终端,输入信息:

  1. echo “root^M” >>commands  
  2. echo “password^M”>>commands  
  3. echo “pwd^M” >> commands  
  4.  …. 

8、观察tail –f的输出,可以看到输入命令的执行结果。在这个脚本的基础上,可以实现UNIX自动登录telnet到一个主机上并执行一些列command的功能。脚本如下:

  1. #!/bin/bashtmptty=`tty`tmptty=`basename $tmptty`tmpname=`whoami`ip="136.39.75.102"inp1="root^M"inp2="Easy2get^M" 
  2. #you may add as many commands as you want  
  3. #NOTE:every command should end with ^M, which is input under UNIX  
  4. #方法为按住ctrl键按v键,不放ctrl键,再按shift键和m键,完成后全部放开  
  5. #inp3="ls^M"inp4="echo 'hello,TeMIP users' >> hello.txt^M"  
  6. #--inputfile=inoutputfile=out.logrm -fr $inputfilerm -fr $outputfilemknod $inputfile ptouch $outputfileexec 7<>$outputfileexec 8<>$inputfiletelnet$ip <&8 >&7 &sleep 2;  
  7. echo $inp1 >> $inputfilesleep 2;   
  8. echo $inp2 >> $inputfileecho "executing command $inp3"sleep 2;  
  9. echo $inp3 >> $inputfileecho "executing $inp4"sleep 2;   
  10. echo $inp4 >> $inputfileecho "exiting"sleep 2;   
  11. echo "exit^M" >> $inputfilerm $inputfilerm $outputfile  


相关内容

    暂无相关文章