批处理 判断能不能telnet

 我来答
xiangjuan314
2016-07-14 · TA获得超过3.3万个赞
知道大有可为答主
回答量:2.9万
采纳率:0%
帮助的人:2877万
展开全部
例子1:
具体如下命令:
先登陆到192.168.1.100.
telnet 192.168.0.100
Username:aaa
Password:111
登陆之后做几个简单的ping操作
ping 192.168.0.1
ping 192.168.0.2
请问用cmd的批处理如何实现?
回一楼的,肯定可以的,我要登陆的是台路由,我见有人做过可行的。

把如下代码保存为批处理(*.bat)

rem =========Yobo-lily版权=======
@del c:\temp.vbs
@echo on error resume next >>c:\temp.vbs
@echo dim WshShell>>c:\temp.vbs
@echo Set WshShell = WScript.CreateObject("WScript.Shell")>>c:\temp.vbs
@echo WshShell.run"cmd">>c:\temp.vbs
@echo WshShell.AppActivate"c:\windows\system32\cmd.exe">>c:\temp.vbs
@echo WScript.Sleep 200>>c:\temp.vbs
@echo WshShell.SendKeys"telnet 192.168.1.100">>c:\temp.vbs
@echo WshShell.SendKeys"{ENTER}">>c:\temp.vbs
@echo WScript.Sleep 100>>c:\temp.vbs
@echo WshShell.AppActivate"telnet.exe ">>c:\temp.vbs
@echo WScript.Sleep 2000>>c:\temp.vbs
@echo WshShell.SendKeys"aaa">>c:\temp.vbs
@echo WshShell.SendKeys"{ENTER}">>c:\temp.vbs
@echo WScript.Sleep 2000>>c:\temp.vbs
@echo WshShell.SendKeys"111">>c:\temp.vbs
@echo WshShell.SendKeys"{ENTER}">>c:\temp.vbs
@echo WScript.Sleep 2000>>c:\temp.vbs
@echo WshShell.SendKeys"ping 192.168.0.1">>c:\temp.vbs
@echo WshShell.SendKeys"{ENTER}">>c:\temp.vbs
@echo WScript.Sleep 1000>>c:\temp.vbs
@echo WshShell.SendKeys"ping 192.168.0.2">>c:\temp.vbs
@echo WshShell.SendKeys"{ENTER}">>c:\temp.vbs
@echo WScript.Sleep 1000>>c:\temp.vbs
@call c:\temp.vbs
rem =========Yobo-lily版权=======

例子2:
Windows平台:
复制一下代码到记事本中并另保存为autoTelnet.bat,之后双击运行就实现自动登录到telnet服务器了,服务器IP自己修改
@echo off
echo setsh=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 "登录账号{ENTER}">>telnet_tmp.vbs
echo WScript.Sleep 300 >>telnet_tmp.vbs
echo sh.SendKeys "登录密码{ENTER}">>telnet_tmp.vbs
start telnet
cscript. //nologo telnet_tmp.vbs
del telnet_tmp.vbs

例子3:
因为网络工程的实施过程中,需要对大量的设备进行配置,以及工程结束时对大量设备进行配置备份。因此想做一个批处理工具自动telnet。
目前,结合百度搜索的结果,已经完成了以下功能:、
1、通过bat文件自动生成vbs文件
2、运行vbs文件以后,自动telnet设备,并输入准备好的命令。可以配置,也可以备份配置。

代码如下:
1、bat文件的代码:
@del .\temp.vbs
@echo on error resume next >>.\temp.vbs
@echo dim WshShell>>.\temp.vbs
@echo Set WshShell = WScript.CreateObject("WScript.Shell")>>.\temp.vbs
@echo WshShell.run"cmd">>.\temp.vbs
@echo WshShell.AppActivate".\windows\system32\cmd.exe">>.\temp.vbs
@echo WScript.Sleep 200>>.\temp.vbs
@echo WshShell.SendKeys"telnet 192.168.1.2">>.\temp.vbs
@echo WshShell.SendKeys"{ENTER}">>.\temp.vbs
@echo WScript.Sleep 100>>.\temp.vbs
@echo WshShell.AppActivate"telnet.exe ">>.\temp.vbs
@echo WScript.Sleep 1000>>.\temp.vbs
for /f "tokens=1 delims=" %%a in ('type .\input.txt') do (@echo WshShell.SendKeys"%%a" >>.\temp.vbs)&(@echo WshShell.SendKeys"{ENTER}">>.\temp.vbs)&(@echo WScript.Sleep 1000>>.\temp.vbs)
@call .\temp.vbs>>.\temp1.txt

2、生成的vbs的代码:
on error resume next
dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.run"cmd"
WshShell.AppActivate".\windows\system32\cmd.exe"
WScript.Sleep 200
WshShell.SendKeys"telnet 192.168.1.2"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 100
WshShell.AppActivate"telnet.exe "
WScript.Sleep 1000
WshShell.SendKeys"cisco"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"en"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"cisco"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"show ip int br"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"conf t"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"exit"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"exit"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000

3、我测试用的txt文本
cisco
en
cisco
show ip int br
conf t
exit
exit

在使用时,只需要将txt文本拖动到bat文件的图标上即可。

遗留问题:
1、生成的vbs代码中,一个命令输出完以后无法根据网络设备返回的命令结果进行下一步配置,而是通过sleep的等待时间来进行下一步配置。这样对于某些需要执行很久的命令就不兼容。最好能有类似WaitForString或者Experts 之类的方法,等待返回结果后再输入下一个命令。
2、现有的vbs代码,无法记录整个批处理的执行过程。只能在结束后,通过tftp把配置文件考下来比对。不方便,也不放心。

例子4:
因为网络工程的实施过程中,需要对大量的设备进行配置,以及工程结束时对大量设备进行配置备份。因此想做一个批处理工具自动telnet。
目前,结合百度搜索的结果,已经完成了以下功能:、
1、通过bat文件自动生成vbs文件
2、运行vbs文件以后,自动telnet设备,并输入准备好的命令。可以配置,也可以备份配置。

代码如下:
1、bat文件的代码:
@del.\temp.vbs
@echoon error resume next >>.\temp.vbs
@echodim WshShell>>.\temp.vbs
@echoSet WshShell = WScript.CreateObject("WScript.Shell")>>.\temp.vbs
@echoWshShell.run"cmd">>.\temp.vbs
@echoWshShell.AppActivate".\windows\system32\cmd.exe">>.\temp.vbs
@echoWScript.Sleep 200>>.\temp.vbs
@echoWshShell.SendKeys"telnet 192.168.1.2">>.\temp.vbs
@echoWshShell.SendKeys"{ENTER}">>.\temp.vbs
@echoWScript.Sleep 100>>.\temp.vbs
@echoWshShell.AppActivate"telnet.exe ">>.\temp.vbs
@echoWScript.Sleep 1000>>.\temp.vbs
for/f "tokens=1 delims=" %%a in ('type .\input.txt') do (@echoWshShell.SendKeys"%%a" >>.\temp.vbs)&(@echoWshShell.SendKeys"{ENTER}">>.\temp.vbs)&(@echoWScript.Sleep 1000>>.\temp.vbs)
@call.\temp.vbs>>.\temp1.txt
2、生成的vbs的代码:
on error resume next
dim WshShell
Set WshShell =WScript.CreateObject("WScript.Shell")
WshShell.run"cmd"
WshShell.AppActivate".\windows\system32\cmd.exe"
WScript.Sleep 200
WshShell.SendKeys"telnet 192.168.1.2"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 100
WshShell.AppActivate"telnet.exe "
WScript.Sleep 1000
WshShell.SendKeys"cisco"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"en"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"cisco"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"show ip int br"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"conf t"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"exit"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
WshShell.SendKeys"exit"
WshShell.SendKeys"{ENTER}"
WScript.Sleep 1000
3、我测试用的txt文本
cisco
en
cisco
show ip int br
conf t
exit
exit
在使用时,只需要将txt文本拖动到bat文件的图标上即可。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式