A-A+

批处理如何实现telnet登陆并进行一些简单命令

2020年09月29日 点滴记录 批处理如何实现telnet登陆并进行一些简单命令已关闭评论 阅读 2,975 次浏览 次

例子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文件的图标上即可。

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

标签:

评论已关闭!

Copyright © 海边拾贝 保留所有权利.   Theme  Ality 鲁ICP备17020531号-1
网站已运行: | 耗时 0.477 秒 | 查询 51 次 | 内存 29.88 MB

用户登录