DOS批处理下操作telnet实现自动远程登录操作

转载:http://blog.sina.com.cn/s/blog_6446318a0100rhxy.html

 

 

我工作环境的配置:

开发工具:Ecplise

数据库:ORACLE10g

测试服务器:HP-UX + Weblogic9.2

由于是数十人同时开发,先由我们把自己的代码更新至CVS服务器,然后打成war包,用ftp传至测试机,解压,并且重新启动weblogic。部署到测试机下(要求测试机下需要对war包解压)。

-------------------------------------------------------------------------------------------------------

先看一下我总的工作调度脚本

FILE:autoRefresh8.bat

 

@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::设置10.1.18.8服务器的相关配置::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::服务器地址
set url=10.1.18.8
::远程登录的用户名,密码
set userid=weblogic
set password=weblogic
::登录的家目录
set homeDir=/weblogic
::执行shell脚本的目录及文件名
set shDir=/weblogic/syang
::解压及停止服务器的shell脚本文件名
set stopShFileName=stopAndExtract.sh
::启动服务器的shell脚本文件名
set startShFileName=startadmin.sh
::解压等待时间
set times=25000
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::停止服务器,并且解压最新war包::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo 停止服务器,并且解压部署文件...
call stopAndExtract.bat
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::上传最新jar包及相关配置文件:::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo 从本地上传jar包及配置文件至10.1.18.8...
call ftp_8.bat
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::启动服务器:::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo 开始启动服务器... 
call startAdmin.bat

 

 

telnet登录到服务器,并且解压war包

FILE:stopAndExtract.bat

 

@echo off
echo set sh=WScript.CreateObject("WScript.Shell") >telnet_tmp.vbs
::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::登录::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "%userid%{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "%password%{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::进入weblogic家目录:::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
echo sh.SendKeys "cd %homeDir%{ENTER}">>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::进入自动脚本目录::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
echo sh.SendKeys "cd %shDir%{ENTER}">>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::自动解压::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
rem echo 停止weblogic并且解压war包
echo sh.SendKeys "sh %stopShFileName% {ENTER}">>telnet_tmp.vbs
::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::解压,等待时间::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
echo WScript.Sleep %times%>>telnet_tmp.vbs
::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::退出shell::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
echo sh.SendKeys "exit{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "quit{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "{ENTER}" >>telnet_tmp.vbs
::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::开始执行:::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
start telnet %url%
::cscript telnet_tmp.vbs
::del telnet_tmp.vbs

 

 

由于测试服务器有两个,而这两个服务器需要一个特殊的jar包,而且内部内容不完全相同,所以只能分别上传了

FILE:ftp_8.bat

 

@echo off
ftp -ns:ftp_10.1.18.8.ftp

 

 

自动ftp脚本

FILE:ftp_10.1.18.8.ftp

 

open 10.1.18.8
User weblogic
weblogic
cd /weblogic/bea/user_projects/domains/base_domain/CEBPLMS/WEB-INF/lib
delete ceboci.jar
lcd D:\WorkSpace\jar
binary
put ceboci8.jar
rename ceboci8.jar ceboci.jar
ascii
cd ..
delete amarsoft.xml
put amarsoft.xml
bye
quit

 

 

启动服务器脚本

file:startAdmin.bat

 

@echo off
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::登录服务器::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo set sh=WScript.CreateObject("WScript.Shell") >telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "%userid%{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "%password%{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::进入家目录,启动服务::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo sh.SendKeys "cd %homeDir%{ENTER}">>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "sh %startShFileName% {ENTER}">>telnet_tmp.vbs
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::退出服务器::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "exit{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "quit{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "{ENTER}" >>telnet_tmp.vbs
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::开始执行::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
start telnet %url%
cscript telnet_tmp.vbs
::del telnet_tmp.vbs

 

 

 

最后附上停止weblogic服务器并且解压war包的的shell脚本。也许有人会说停止服务器,unix下不是有个 stopadmin.sh不过,这个脚本执行正常关闭weblogic,太慢了。我这里为了节约发部时间,所以停止weblogic采用直接干掉java 进程的方法

file:autoRefresh.sh

 

#!/bin/sh
# Get PID from process name
 #配置相关变量
 plmsHome='/weblogic/bea/user_projects/domains/base_domain/CEBPLMS'
 warFile=CEBPLMS.war
 #找到java进程的pid号,并且结束该进程
 psList=`ps -u weblogic |grep java`
 for list in $psList
 do
  pid=$list
  break
 done
 echo 停止PID:pid
 kill -9 $pid
 cd $plmsHome
 jar -xvf $warFile

 

 

 

最后总结:其实这几个bat脚本主要是模拟人手工操作,发送键盘命令。因为在dos下启动telnet,这里其他的批处理命令已不能用,是由于 telnet开了一个新的shell,所以只能模拟键盘命令了。特别提醒一下,执行过程中,不要进行其他操作。因为整个执行的过程,相当于我们自己敲键 盘。如果你在做其他的事,你会发现好像有人在敲你的键盘。恰恰是那段vbs文件执行的结果.如果您有更好的方法,望不吝见赐教。

 

在最末,附上vbs操作键盘的相关资料:

下面的资料从网上获取,不过忘记了出处

 

自动登录telnet批处理文件 
一。Windows平台:
复制一下代码到记事本中并另保存为autoTelnet.bat,之后双击运行就实现自动登录到telnet服务器了,服务器IP自己修改
@echo off
echo set sh=WScript.CreateObject("WScript.Shell") >telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "open 192.168.1.200" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "engineer{ENTER}" >>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "ls {ENTER}">>telnet_tmp.vbs
start telnet
cscript //nologo telnet_tmp.vbs
del telnet_tmp.vbs
附录:
对于SendKeys这个命令可以send什么,我们可以看下面的列表:
BACKSPACE {BACKSPACE}, {BS}, or {BKSP} 
BREAK {BREAK} 
CAPS LOCK {CAPSLOCK} 
DEL or DELETE {DELETE} or {DEL} 
DOWN ARROW {DOWN} 
END {END} 
ENTER {ENTER}or ~ 
ESC {ESC} 
HELP {HELP} 
HOME {HOME} 
INS or INSERT {INSERT} or {INS} 
LEFT ARROW {LEFT} 
NUM LOCK {NUMLOCK} 
PAGE DOWN {PGDN} 
PAGE UP {PGUP} 
PRINT SCREEN {PRTSC} 
RIGHT ARROW {RIGHT} 
SCROLL LOCK {SCROLLLOCK} 
TAB {TAB} 
UP ARROW {UP} 
F1 {F1} 
F2 {F2} 
F3 {F3} 
F4 {F4} 
F5 {F5} 
F6 {F6} 
F7 {F7} 
F8 {F8} 
F9 {F9} 
F10 {F10} 
F11 {F11} 
F12 {F12} 
F13 {F13} 
F14 {F14} 
F15 {F15} 
F16 {F16} 
SHIFT + 
CTRL ^ 
ALT % 
二。Linux平台

保证你的系统上存在expect这个可执行程序,保存以下代码到文件autoTelnet,并给与执行权限。详细操作察看命令expect
#!/usr/bin/expect --
set SERVER "192.168.1"
set USER "myusername"
set PASSWD "mypass"
if { $argc } {
spawn telnet $SERVER.$argv
} else {
spawn telnet $SERVER.200
}
expect "Password:"
send "$PASSWD\n"
expect "#"
send "ls\n"
interact

 

 

对了,还忘记了文件中涉及一个sleep命令,这个命令window下是没有的,不过网上的强人用bat也制成了这个文件。现一起附上。

 

 

@echo off 

echo q | debug>nul
echo Bj@jzh`0X-`/PPPPPPa(DE(DM(DO(Dh(Ls(Lu(LX(LeZRR]EEEUYRX2Dx=>sleep.com
echo 0DxFP,0Xx.t0P,=XtGsB4o@$?PIyU WwX0GwUY Wv;ovBX2Gv0ExGIuht6>>sleep.com
echo T}{z~~@GwkBG@OEKcUt`~}@MqqBsy?seHB~_Phxr?@zAB`LrPEyoDt@Cj?>>sleep.com
echo pky_jN@QEKpEt@ij?jySjN@REKpEt@jj?jyGjN@SEKkjtlGuNw?p@pjirz>>sleep.com
echo LFvAURQ?OYLTQ@@?~QCoOL~RDU@?aU?@{QOq?@}IKuNWpe~FpeQFwH?Vkk>>sleep.com
echo _GSqoCvH{OjeOSeIQRmA@KnEFB?p??mcjNne~B?M??QhetLBgBPHexh@e=>>sleep.com
echo EsOgwTLbLK?sFU`?LDOD@@K@xO?SUudA?_FKJ@N?KD@?UA??O}HCQOQ??R>>sleep.com
echo _OQOL?CLA?CEU?_FU?UAQ?UBD?LOC?ORO?UOL?UOD?OOI?UgL?LOR@YUO?>>sleep.com
echo dsmSQswDOR[BQAQ?LUA?_L_oUNUScLOOuLOODUO?UOE@OwH?UOQ?DJTSDM>>sleep.com
echo QTqrK@kcmSULkPcLOOuLOOFUO?hwDTqOsTdbnTQrrDsdFTlnBTm`lThKcT>>sleep.com
echo @dmTkRQSoddTT~?K?OCOQp?o??Gds?wOw?PGAtaCHQvNntQv_w?A?it\EH>>sleep.com
echo {zpQpKGk?Jbs?FqokOH{T?jPvP@IQBDFAN?OHROL?Kj??pd~aN?OHROd?G>>sleep.com
echo Q??PGT~B??OC~?ipO?T?~U?p~cUo0x>>sleep.com
sleep.com>sleep.exe
del sleep.com
 

 

二。Linux平台
保证你的系统上存在expect这个可执行程序,保存以下代码到文件autoTelnet,并给与执行权限。详细操作察看命令expect

#!/usr/bin/expect --
set SERVER "192.168.1"
set USER "myusername"
set PASSWD "mypass"
if { $argc } {
spawn telnet $SERVER.$argv
} else {
spawn telnet $SERVER.200
}
expect "Password:"
send "$PASSWD\n"
expect "#"
send "ls\n"
interact

你可能感兴趣的:(telnet)