1. 程式人生 > >用批處理如何實現telnet登陸並進行一些簡單命令

用批處理如何實現telnet登陸並進行一些簡單命令

例子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裝置,並輸入準備好的命令。可以配置,也可以備份配置。程式碼如下:
1bat檔案的程式碼:
@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把配置檔案考下來比對。不方便,也不放心。