[Reprint] use bat batch processing to implement automatic Telnet

Source: Internet
Author: User

 

In general, FTP is very common in batch transaction processing. What if batch Telnet makes management easier?

Generally, there are two methods:

1. vbs method (Windows Script Host)

2. Automatic Terminal method (some terminals that can be configured with automatic Telnet)

Here we will discuss the first method: The vbs method. Because Windows Script Host comes with the system, we can do it without installing local software. Therefore, it is generally the first choice to solve the problem.

First, we create a name calledTel. vbsThe script. telnet to the Cisco router as an example. You can modify the content.

'Create shell object
Set SH = wscript. Createobject ("wscript. Shell ")
Wscript. Sleep 1000
'Send the commands we input to telnet
Sh. sendkeys "Open 192.168.1.1"
Wscript. Sleep 1000
Sh. sendkeys "{enter }"
Wscript. Sleep 1000
Sh. sendkeys "password {enter }"
Wscript. Sleep 1000
Sh. sendkeys "en {enter }"
Wscript. Sleep 1000
Sh. sendkeys "passord {enter }"
Wscript. Sleep 1000
'Run the Cisco show Run Command
Sh. sendkeys "show run {enter }"

Then, createBatch_tel.bat:

Rem start Telnet
Start telnet.exe
Rem batch Telnet
Cscript // nologo Tel. vbs

Finally, we only need to run batch_tel.bat to easily complete the Telnet work.

For what the sendkeys command can send, we can look at the following list:

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 %

========================================================== ========================================================

I did it myself:

Tel. vbs content

Set SH = wscript. Createobject ("wscript. Shell ")
Wscript. Sleep 1000
Sh. sendkeys "Open 192.168.0.1 {enter }"
Wscript. Sleep 1000
Sh. sendkeys "username {enter }"
Wscript. Sleep 1000
Sh. sendkeys "password {enter }"
Wscript. Sleep 1000
Sh. sendkeys "Clear session {enter }"
Wscript. Sleep 5000
Sh. sendkeys "exit {enter }"
Wscript. Sleep 1000
Sh. sendkeys "{enter }"
Wscript. Sleep 1000
Sh. sendkeys "Quit {enter }"

Batch_tel.bat content:

Rem start Telnet
Start telnet.exe
Rem batch Telnet
Cscript // nologo Tel. vbs

Conclusion: if there is a special symbol in the password, Shift + is used for shift. It has not been tested.

The test is successful. No more.

I. Windows:
Copy the code to notepad and save it as autotelnet. bat. Then, double-click it to automatically log on to the Telnet server and modify the Server IP address.
@ 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

Appendix:

For what the sendkeys command can send, we can look at the following list:

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 %

II. Linux platform

Make sure that your system has an executable program, save the following code to the file autotelnet, and grant the execution permission. Detailed operation view command audit CT

#! /Usr/bin/CT --
Set server "192.168.1"
Set User "myusername"
Set passwd "mypass"
If {$ argc }{
Spawn Telnet $ server. $ argv
} Else {
Spawn Telnet $ server.200
}
Reset CT "Password :"
Send "$ passwd \ n"
CT "#"
Send "ls \ n"
Interact

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.