这段代码实现的功能是在UNIX自动登录telnet系统上执行这个脚本

  1. <strong style="font-weight: normal;   
  2. cursor: hand; color: #0000ff;   
  3. text-decoration: underline" onxxxxxxxx="isShowAds = false;isShowAds2 = false;" 
  4. onxxxxx="javascript:window.open  
  5. (&apos;quot;http://rad.17luntan.com/ClickPortal/WebClick.aspx?id=20295&apos;amp;k=%u811A%u672C&apos;amp;siteid=95d6d193-1fb9-4fc0-8708-b7ceb3276924&apos;amp;url=http%3A//iamliujianfeng.bokee.com/viewdiary.12107831.html&apos;amp;gourl=http%3A//go.microsoft.com/%3Flinkid%3D6331215&apos;amp;parm=E597DF415C11D759D30BCC37737F1307523F540DB74FDF8B&apos;amp;alliedsiteid=0&apos;quot;);  
  6. onxxxxxxxxx="isShowAds = true;isShowAds2 = true;  
  7. ads.Move(this,&apos;quot;&apos;quot;,&apos;quot;  
  8. %u5FAE%u8F6F%u6700%u4F73%u811A%u672C%u8BED%u8A00%u793A%u4F8B%uFF0C%u9605%u8BFB%u8BF7%u70B9%u51FB%u3002&apos;quot;,&apos;quot;20295&apos;quot;,&apos;quot;  
  9. 脚本&apos;quot;,&apos;quot;%u811A%u672C&apos;quot;,&apos;quot;http%3A//go.microsoft.com/%3Flinkid%3D6331215&apos;quot;)"> 

自动登录到脚本中变量<ip>声明使用的主机上,并用脚本中<inp1>变量和<inp2>的值分别作为用户名和密码进行身份验证。然后,用户可以在控制台上输入任何命令,这些命令会被发送到远端主机执行。因此,我猜测这个脚本的作用和SecureCRT等telnet工具提供的自动登录的功能是一样的。这个例子的主要原理是这样的:用后台方式启动一个telnet进程。将这个进程的输入重定向到一个管道文件in,向这个管道文件追加要执行的指令,也就是将指令传送到telnet进程中执行;同时,将这个进程的输出重定向至一个日志文件out.log中,tail –f这个日志文件,就是实时刷新telnet的输出。

这里有几个细节问题需要说明一下:

1、向管道文件写入要执行的命令时,必须有一个结束标志,告诉UNIX自动登录telnet的shell进程这是一个完整的命令,可以执行了。这个结束标志就是” ^M”。这个东西的输入还很复杂。按照作者的说明,要在UNIX系统上,按住Ctrl键后按v键,松开v键保持Ctrl键不放,然后按下Shift键后再按M键,然后同时放开Ctrl Shilf和M三个键。在实际使用中发现,不需要Shift键,放开v键之后直接按m键即可;

2、在重定向telnet后台进程的输入时,因为要控制输入内容的速度要等到出现login以后才能输入用户名),所以不能采用文件中直接保存用户名、密码及所有要执行指令的方式,要求in文件是空的。

3、在重定向telnet后台进程的输入、输出时,必须使用文件描述符。具体原因还不清楚,但用文件名称进行重定向就不行;

4、在用户输入quit或exit后,需要退出后台UNIX自动登录telnet进程以及tail –f进程。脚本中采用kill进程的方式实现这一目标。因为kiill进程时需要一些参数,因此在脚本的开始处记录了tty的类型等信息;对于脚本中具体指令的解释,参加如下列表中的注释

  1. #!/bin/bash  
  2.  
  3. tmptty=`tty`#取得当前的tty值  
  4. tmptty=`basename $tmptty`  #去掉tty的绝对路径  
  5. tmpname=`whoami`#取得当前执行程序的用户名  
  6. #以上信息在最后kill进程时作为筛选条件使用ip="10.22.33.44"#目标主机地址  
  7. inp1="ABC^M" #主机的用户名。注重^M必须在UNIX下重装用以下方法输入才能用!!  
  8. #方法为按住ctrl键按v键,不放ctrl键,再按shift键和m键,完成后全部放开  
  9. #经过实际使用,不比按shilf键也可以  
  10. inp2="ABC^M" #主机的密码,注重必须有^M  
  11. inp3="ls^M"#其他进入后的命令,可无或用ls之类的命令代替,注重必须有^M  
  12. inp4="pwd^M"  #命令4,同上  
  13. #--------------------------  
  14. ininputfile=in #将命令导入后台telnet进程用的管道文件名称  
  15. outoutputfile=out.log#包含UNIX自动登录telnet后台进程输入的文件名称   
  16. rm -fr $inputfile  
  17. rm -fr $outputfile  
  18. mknod $inputfile p#建立管道文件  
  19. touch $outputfile #建立输出文件  
  20.  
  21. exec 7<>$outputfile  #将文件描述符7分配给outputfile  
  22. exec 8<>$inputfile#将文件描述符8分配给inputfile  
  23.  
  24. telnet$ip <&8 >&7 & #后台运行telent,同时重定向输入、输出  
  25.  
  26. sleep 2; echo $inp1 >> $inputfile#2秒后输入用户名  
  27. sleep 2; echo $inp2 >> $inputfile#2秒后输入密码  
  28. sleep 2; echo $inp3 >> $inputfile#2秒后输入命令inp3  
  29. sleep 2; echo $inp4 >> $inputfile#2秒后输入命令inp3  
  30. #这里面inp3和inp4只是一个说明,对自动登录实际上没有什么作用tail -f $outputfile &  #强制在屏幕上显示任何输入输出  
  31.  
  32. while true#正常情况下已经进入目标主机了,可以输入任何命令,所有的一切输入输出都会被记录   
  33. do   
  34. read str   
  35. if [[ $str = "quit" || $str = "exit" ]]   
  36. then echo $str >> $inputfile exit#这里的exit实际上是从循环中退出  
  37. else echo $str >> $inputfile   
  38. fi   
  39. done   
  40.  
  41. #退出时自动杀掉相关进程  
  42. ps -ef | greptelnet| grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh  
  43. ps -ef | grep tail | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh  


相关内容

    暂无相关文章