Script for automatic telnet to remote host in linux

Source: Internet
Author: User
In linux, the script automatically telnet to the remote host-Linux Enterprise Application-Linux server application information. The following is a detailed description. Use Case: the automatic test system. After the automatic compilation is completed, it must be able to run automatically on the terminal. The terminal only supports Serial Port and telnet control. telnet is an ideal choice, you only need to telnet to complete the interaction process automatically.

Solution: Run telnet in the background, redirect the standard input to the MPs queue, and send interactive commands to the MPs queue. (Take a look at the relevant content of the MPs queue and add an article if necessary)

Supplement: In this example, the main content of the script is the telnet background running and pipeline. Other scripts, such as lua, can be used if necessary, of course, exec is required.

Script:
#=========== Autotelnet. sh ==========================
#! /Bin/bash

Tmptty = 'tty '# obtain the current tty Value
Tmptty = 'basename $ tmptty '# Remove the absolute path of tty
Tmpname = 'whoam' # obtain the username of the currently executed Program
Ip = "10.22.33.44" # target host address
Inp1 = "ABC ^ M" # host user name. Note that ^ M must be entered in UNIX as follows !! (Test in VI)
# Press ctrl and press v. Do not press ctrl, and then press shift and m.
Inp2 = "ABC ^ M" # host password. Note that ^ M is required.
Inp3 = "ls ^ M" # other commands after entry can be replaced by none or ls commands. Note that ^ M must be included.
Inp4 = "pwd ^ M" # command 4, same as above
#--------------------------

Inputfile = in # It is used to import the file pipeline. Do not change it. This value does not matter.
Outputfile = out. log # final exported file
Rm-fr $ inputfile
Rm-fr $ outputfile
Mknod $ inputfile p
Touch $ outputfile

# File description 7 for out and 8 for in uses 7 as the input pipeline and 8 as the input
Exec 7 <> $ outputfile
Exec 8 <> $ inputfile

# This sentence is the key. Pay attention to the redirection of the standard input and standard output, and the operation in the background to facilitate subsequent operations.
Telnet $ ip <& 8> & 7 &

Sleep 2; echo $ inp1> $ inputfile # Wait for 2 seconds, and then input inp1, that is, username + press enter, to the input Pipeline
Sleep 2; echo $ inp2 >>$ inputfile
Sleep 2; echo $ inp3> $ inputfile # if there are no other commands, this line and the next line can be removed.
Sleep 2; echo $ inp4 >>$ inputfile

Tail-f $ outputfile & # force any input/output to be displayed on the screen

# Normally, you have already entered the target host. You can enter any command and all input and output will be recorded.
# The following section is modified based on actual needs. In this case, you can exit after waiting for some events and do not need to read them.
While true
Do
Read str # read a row
If [[$ str = "quit" | $ str = "exit"]
# Remotely execute this line
Then echo $ str >>$ inputfile exit
Else echo $ str >>$ inputfile
Fi
Done

# Killing related processes automatically upon exit
Ps-ef | grep telnet | grep-v grep | grep-v telnetd | grep $ tmptty | grep $ tmpname | awk '{print "kill-9 ", $2} '| sh
Ps-ef | grep tail | grep-v grep | grep-v telnetd | grep $ tmptty | grep $ tmpname | awk '{print "kill-9 ", $2} '| sh
#================================ Script end =================== ===
Related Article

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.