UNIX自動登錄telnet代碼設(shè)置全解
UNIX系統(tǒng)在很多企業(yè)中都有所應(yīng)用。比較復(fù)雜的一類應(yīng)用,這里我們就來講解一下UNIX自動登錄telnet的設(shè)置。那么為了幫助大家了解這部分內(nèi)容,我們搜集了一些資料,進行了整理,這里分享給大家。(http://www.linuxeden.com/forum/t138058.html)。這個例子很不錯,下邊對其進行分析。
- #===========autotelnet.sh==============
- #!/bin/bash
- tmptty=`tty` #取得當(dāng)前的tty值
- tmptty=`basename $tmptty` #去掉tty的絕對路徑
- tmpname=`whoami` #取得當(dāng)前執(zhí)行程序的用戶名
- ip="10.22.33.44" #目標(biāo)主機地址
- inp1="ABC^M" #主機的用戶名,注重^M必須在UNIX自動登錄telnet下重裝用以下方法輸入才能用!!
- #方法為按住ctrl鍵按v鍵,不放ctrl鍵,再按shift鍵和m鍵,完成后全部放開
- inp2="ABC^M" #主機的密碼,注重必須有^M
- inp3="ls^M" #其他進入后的命令,可無或用ls之類的命令代替,注重必須有^M
- inp4="pwd^M" #命令4,同上
- #--------------------------
- ininputfile=in #導(dǎo)入文件管道用的,不要改,這個值沒有任何關(guān)系
- outoutputfile=out.log #最終導(dǎo)出的文件
- rm -fr $inputfile
- rm -fr $outputfile
- mknod $inputfile p
- touch $outputfile
- #file description 7 for out and 8 for in 使用7作為輸入管道,8作為輸入
- exec 7<>$outputfile
- exec 8<>$inputfile
- telnet$ip <&8 >&7 &
- sleep 2; echo $inp1 >> $inputfile
- sleep 2; echo $inp2 >> $inputfile
- sleep 2; echo $inp3 >> $inputfile
- #UNIX自動登錄telnet設(shè)置中,假如沒有其他命令,這行和下一行可以去掉
- sleep 2; echo $inp4 >> $inputfile
- tail -f $outputfile & 強制在屏幕上顯示任何輸入輸出
- while true #正常情況下已經(jīng)進入目標(biāo)主機了,可以輸入任何命令,所有的一切輸入輸出都會被記錄
- do
- read str
- if [[ $str = "quit" || $str = "exit" ]]
- then echo $str >> $inputfile exit
- else echo $str >> $inputfile
- fi
- done
- #UNIX自動登錄telnet退出時自動殺掉相關(guān)進程
- ps -ef | greptelnet| grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh
- ps -ef | grep tail | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh
#p#這段代碼實現(xiàn)的功能是在UNIX自動登錄telnet系統(tǒng)上執(zhí)行這個腳本
- <strong style="font-weight: normal;
- cursor: hand; color: #0000ff;
- text-decoration: underline" onxxxxxxxx="isShowAds = false;isShowAds2 = false;"
- onxxxxx="javascript:window.open
- ('quot;http://rad.17luntan.com/ClickPortal/WebClick.aspx?id=20295'amp;k=%u811A%u672C'amp;siteid=95d6d193-1fb9-4fc0-8708-b7ceb3276924'amp;url=http%3A//iamliujianfeng.bokee.com/viewdiary.12107831.html'amp;gourl=http%3A//go.microsoft.com/%3Flinkid%3D6331215'amp;parm=E597DF415C11D759D30BCC37737F1307523F540DB74FDF8B'amp;alliedsiteid=0'quot;);
- " onxxxxxxxxx="isShowAds = true;isShowAds2 = true;
- ads.Move(this,'quot;'quot;,'quot;
- %u5FAE%u8F6F%u6700%u4F73%u811A%u672C%u8BED%u8A00%u793A%u4F8B%uFF0C%u9605%u8BFB%u8BF7%u70B9%u51FB%u3002'quot;,'quot;20295'quot;,'quot;
- 腳本'quot;,'quot;%u811A%u672C'quot;,'quot;http%3A//go.microsoft.com/%3Flinkid%3D6331215'quot;)">
自動登錄到腳本中變量<ip>聲明使用的主機上,并用腳本中<inp1>變量和<inp2>的值分別作為用戶名和密碼進行身份驗證。然后,用戶可以在控制臺上輸入任何命令,這些命令會被發(fā)送到遠端主機執(zhí)行。因此,我猜測這個腳本的作用和SecureCRT等telnet工具提供的自動登錄的功能是一樣的。這個例子的主要原理是這樣的:用后臺方式啟動一個telnet進程。將這個進程的輸入重定向到一個管道文件in,向這個管道文件追加要執(zhí)行的指令,也就是將指令傳送到telnet進程中執(zhí)行;同時,將這個進程的輸出重定向至一個日志文件out.log中,tail –f這個日志文件,就是實時刷新telnet的輸出。
這里有幾個細節(jié)問題需要說明一下:
1、向管道文件寫入要執(zhí)行的命令時,必須有一個結(jié)束標(biāo)志,告訴UNIX自動登錄telnet的shell進程這是一個完整的命令,可以執(zhí)行了。這個結(jié)束標(biāo)志就是” ^M”。這個東西的輸入還很復(fù)雜。按照作者的說明,要在UNIX系統(tǒng)上,按住Ctrl鍵后按v鍵,松開v鍵保持Ctrl鍵不放,然后按下Shift鍵后再按M鍵,然后同時放開Ctrl Shilf和M三個鍵。在實際使用中發(fā)現(xiàn),不需要Shift鍵,放開v鍵之后直接按m鍵即可;
2、在重定向telnet后臺進程的輸入時,因為要控制輸入內(nèi)容的速度(要等到出現(xiàn)login以后才能輸入用戶名),所以不能采用文件中直接保存用戶名、密碼及所有要執(zhí)行指令的方式,要求in文件是空的。
3、在重定向telnet后臺進程的輸入、輸出時,必須使用文件描述符。具體原因還不清楚,但用文件名稱進行重定向就不行;
4、在用戶輸入quit或exit后,需要退出后臺UNIX自動登錄telnet進程以及tail –f進程。腳本中采用kill進程的方式實現(xiàn)這一目標(biāo)。因為kiill進程時需要一些參數(shù),因此在腳本的開始處記錄了tty的類型等信息;對于腳本中具體指令的解釋,參加如下列表中的注釋
- #!/bin/bash
- tmptty=`tty`#取得當(dāng)前的tty值
- tmptty=`basename $tmptty` #去掉tty的絕對路徑
- tmpname=`whoami`#取得當(dāng)前執(zhí)行程序的用戶名
- #以上信息在最后kill進程時作為篩選條件使用ip="10.22.33.44"#目標(biāo)主機地址
- inp1="ABC^M" #主機的用戶名。注重^M必須在UNIX下重裝用以下方法輸入才能用!!
- #方法為按住ctrl鍵按v鍵,不放ctrl鍵,再按shift鍵和m鍵,完成后全部放開
- #經(jīng)過實際使用,不比按shilf鍵也可以
- inp2="ABC^M" #主機的密碼,注重必須有^M
- inp3="ls^M"#其他進入后的命令,可無或用ls之類的命令代替,注重必須有^M
- inp4="pwd^M" #命令4,同上
- #--------------------------
- ininputfile=in #將命令導(dǎo)入后臺telnet進程用的管道文件名稱
- outoutputfile=out.log#包含UNIX自動登錄telnet后臺進程輸入的文件名稱
- rm -fr $inputfile
- rm -fr $outputfile
- mknod $inputfile p#建立管道文件
- touch $outputfile #建立輸出文件
- exec 7<>$outputfile #將文件描述符7分配給outputfile
- exec 8<>$inputfile#將文件描述符8分配給inputfile
- telnet$ip <&8 >&7 & #后臺運行telent,同時重定向輸入、輸出
- sleep 2; echo $inp1 >> $inputfile#2秒后輸入用戶名
- sleep 2; echo $inp2 >> $inputfile#2秒后輸入密碼
- sleep 2; echo $inp3 >> $inputfile#2秒后輸入命令inp3
- sleep 2; echo $inp4 >> $inputfile#2秒后輸入命令inp3
- #這里面inp3和inp4只是一個說明,對自動登錄實際上沒有什么作用tail -f $outputfile & #強制在屏幕上顯示任何輸入輸出
- while true#正常情況下已經(jīng)進入目標(biāo)主機了,可以輸入任何命令,所有的一切輸入輸出都會被記錄
- do
- read str
- if [[ $str = "quit" || $str = "exit" ]]
- then echo $str >> $inputfile exit#這里的exit實際上是從循環(huán)中退出
- else echo $str >> $inputfile
- fi
- done
- #退出時自動殺掉相關(guān)進程
- ps -ef | greptelnet| grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh
- ps -ef | grep tail | grep -v grep | grep -v telnetd | grep $tmptty | grep $tmpname | awk '{print " kill -9", $2}' | sh
#p#明白了這個腳本的原理,我們可以做一個試驗,用另外一種更加明顯的方式來做輸入輸出的重定向。
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的輸出,可以看到輸入命令的執(zhí)行結(jié)果。在這個腳本的基礎(chǔ)上,可以實現(xiàn)UNIX自動登錄telnet到一個主機上并執(zhí)行一些列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