Home > Operation and Maintenance > Linux Operation and Maintenance > Detailed explanation of scenario linux examples

Detailed explanation of scenario linux examples

PHP中文网
Release: 2017-06-21 11:19:17
Original
1922 people have browsed it

Scenario linux--How to exit telnet gracefully in a script

Scenario

The telnet command is the user interface of the TELNET protocol. It supports two modes: Command mode and Session Mode. Although telnet supports many commands, in most cases, we just use it to check whether the target host has opened a certain port (default is 23).

There are two execution results:

  • The port is not open

$ telnet 101.199.97.65 62715
Copy after login
Copy after login
Trying 101.199.97.65...
telnet: connect to address 101.199.97.65: Connection refused
Copy after login
Copy after login
At this point, the command has exited.

  • The port is open

$ telnet 101.199.97.65 62715
Copy after login
Copy after login
Trying 101.199.97.65...
Connected to 101.199.97.65.
Escape character is '^]'.
Copy after login
The command has not exited at this time.

According to the prompt
Escape character is '^]'.It can be seen that the exit character is '^]' (CTRL+]). Entering other characters at this time will not cause it to exit, nor will CTRL+C. After entering CTRL+], it will be automatically executed and enter the command mode:

^]
Copy after login
telnet>
Copy after login
Run

quit at this time to truly exit.

telnet> quit
Copy after login
Connection closed.
Copy after login
Among them, the Escape character can be customized, using the parameter

-e:

$ telnet -e p 101.199.97.65 62715
Telnet escape character is 'p'.
Trying 101.199.97.65...
Connected to 101.199.97.65.
Escape character is 'p'.
p
telnet> quit
Connection closed.
Copy after login
Even so, exiting telnet is still troublesome. So, going one step further, how should I (gracefully) exit telnet if it appears in a script?

Plan

In fact, it can be like this:

  • Exit immediately after outputting the results

    $ echo "" | telnet 101.199.97.65 62715
    Copy after login
    Copy after login
Trying 101.199.97.65...
Connected to 101.199.97.65.
Escape character is '^]'.
Connection closed by foreign host.
Copy after login
Copy after login
$ echo "" | telnet 101.199.97.65 62715
Copy after login
Copy after login
Trying 101.199.97.65...
telnet: connect to address 101.199.97.65: Connection refused
Copy after login
Copy after login
  • Delayed exit after outputting results

    sleep 2 causes telnet to output results and stay for 2 seconds before exiting command mode.

    $ sleep 2 | telnet 101.199.97.65 62715
    Copy after login
Trying 101.199.97.65...
Connected to 101.199.97.65.
Escape character is '^]'.
Connection closed by foreign host.
Copy after login
Copy after login
Using this method, the standard output and standard error can be redirected to a file, and the port open status can be determined by analyzing the contents of the file.

The above is the detailed content of Detailed explanation of scenario linux examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template