Linux Shell script for TCP/UPD Protocol Communication (redirection Application)

Source: Internet
Author: User
Tags sendmsg

A few days ago, I sent a redirection and pipeline-related usage method. Here are some interesting examples. Implement TCP/UDP-based software communication through redirection.

 

The Linux device contains a special file:

/Dev/[TCP | UPD]/host/port as long as the file is read or written, it is equivalent to the system will try to connect to: host this machine, corresponding port. If the host and port exist, a socket connection is established. The corresponding file appears under the/proc/self/FD directory.

I. Test:/dev/tcp/host/Post File

[Chengmo @ centos5 shell] $ cat </dev/tcp/127.0.0.1/22ssh-2.0-openssh_5.1 # My machine's shell port is: 22 # actually:/dev/tcp does not have this directory at all, this is a special device [chengmo @ centos5 shell] $ cat </dev/tcp/127.0.0.1/223-Bash: CONNECT: Connection denied-Bash:/dev/tcp/127.0.0.1/223: rejected connection #223 the interface does not exist, opening failed [chengmo @ centos5 shell] $ exec 8 <>/dev/tcp/127.0.0.1/22 [chengmo @ centos5 shell] $ LS-L/proc/self/FD/total 0lrwx ------ 1 chengmo 64 10-21 23:05 0->/dev/pts/0lrwx ------ 1 chengmo 64 10-21 23:05 1->/dev/pts/0lrwx ------ 1 chengmo 64 10-21 2->/dev/pts/0lr-x ------ 1 chengmo 64 10-21 3->/proc/22185/fdlrwx ------ 1 chengmo 64 10-21 am 8-> socket: [15067661] # The file descriptor 8 has opened a socket communication channel, which can read and write socket channels, because the following code is used: "<>" open [chengmo @ centos5 shell] $ exec 8> &-# Close the channel [chengmo @ centos5 shell] $ LS-L/proc/self/FD/total 0lrwx ------ 1 chengmo 64 10-21 0->/dev/pts/0lrwx ------ 1 chengmo 64 10-21 1->/dev/pts/0lrwx ------ 1 chengmo 64 10-21 2->/dev/pts/0lr-x ------ 1 chengmo 64 10-21 3->/proc/22234/FD

 

Read time from time server:

[Chengmo @ centos5 HTML] $ cat </dev/tcp/time-b.nist.gov/13

55491 10-10-22 11:33:49 17 0 0 596.3 UTC (NIST )*

The preceding statement uses the Redirect input statement.

 

2. Read remote web server header information through redirection

#! /Bin/sh # testhttphead. sh # The Web server header information is read through the host name and port # copyright chengmo, QQ: 8292669if ($ # <2); then Echo "Usage: $0 host port "; exit 1; FI # exit if the parameter is missingProgram, Returns the status 1 exec 6 <>/dev/tcp/$1/$2 2>/dev/NULL; # opens the socket connection that can be read and written by the host port, connect to file descriptor 6 if ($ ?! = 0); then Echo "Open $1 $2 error! "; Exit 1; FI # If opening fails, $? If the return value is not 0, terminate the echo-e "head/HTTP/1.1 \ n"> & 6; # Set the head information, send it to the socket connection cat <& 6; # Read the returned information from the socket and display it as the standard output exec 6 <&-; Exec 6> &-; # Close the socket input, output exit 0;

 

After the script is created, it is stored as testhttphead. Sh.

Running result:

[Chengmo @ centos5 ~ /Shell] $ sh testhttphead. sh www.baidu.com 80 HTTP/1.1 200 okdate: Thu, 21 Oct 2010 15:17:23 gmtserver: BWS/1.0content-length: 6218content-type: text/html; charset = gb2312Cache-Control: privateexpires: Thu, 21 Oct 2010 15:17:23 gmtset-COOKIE: baiduid = 1c40b2f8c676180fd887108a6e286dc1: fg = 1; expires = Thu, 21-oct-40 15:17:23 GMT; Path =/; domain =. baidu. comp3p: Cp = "Oti DSP cor IVA our ind com" connection: Ke EP-alive [chengmo @ centos5 ~ /Shell] $ sh testhttphead. Sh 127.0.0.1 8080 open 127.0.0.1 8080 error!

SuddenlyThere is a strange idea:

In the Windows era, we can achieve TCP/UPD communication through Telnet. How can we achieve this through traditional methods?

[Chengmo @ centos5 ~ /Shell] $ echo-e "head/HTTP/1.1 \ n" | Telnet www.baidu.com 80 trying 220.181.6.175... connected to www.baidu.com. escape Character is '^]'. connection closed by foreign host. # Sending directly, failed [chengmo @ centos5 ~ /Shell] $ (Telnet www.baidu.com 80) <eofhead/HTTP/1.1 eoftrying 220.181.6.175... connected to www.baidu.com. escape Character is '^]'. connection closed by foreign host. # redirect input or fail?

Find the correct method:

[Chengmo @ centos5 shell] $ (echo-e "head/HTTP/1.1 \ n"; sleep 2) | Telnet www.baidu.com 80 trying 220.181.6.175... connected to www.baidu.com. escape Character is '^]'. HTTP/1.1 200 okdate: Thu, 21 Oct 2010 15:51:58 gmtserver: BWS/1.0content-length: 6218content-type: text/html; charset = gb2312Cache-Control: privateexpires: Thu, 21 Oct 2010 15:51:58 gmtset-COOKIE: baiduid = 0b6a01acecd5108e00007e088a8cb 345a: fg = 1; expires = Thu, 21-oct-40 15:51:58 GMT; Path =/; domain =. baidu. comp3p: Cp = "Oti DSP cor IVA our ind com" connection: keep-alive # succeeded! You can add sleep. You can also change sleep to 1 second.

 

Is it because after sleep, ECHO will launch 2 seconds to send to the channel: Telnet? Inferences can be overturned from these two aspects:

One aspect: the data included in () is a pair of commands, which will be executed as a sub-command and end after the program is executed together. Each echo statement of the command is directly sent to the screen (that is, the standard output). As long as there is a standard output, a telnet will be uploaded through the channel. If there is an output in the next command, it will be sent to telnet until all commands in () are executed, and the connection to the channel is disconnected.

 

Another aspect: if it is to delay sending, when will data be sent to telnet and when the Telnet command is started. We recommend that you send it later. It does not matter.

 

For this type of command, we can see that sleep is actually a two-second connection between the channel and telnet. The channel is connected, and the input from the Telnet terminal is still there. Therefore, data can be obtained from the Baidu server.

Therefore, the delay is related to the server processing speed.

 

If you use echo to send data to telnet and keep the channel connected, it is good to use sleep.

By redirecting telnet to input parameters, I cannot think of how to implement delayed input. If you have any friends, you can give me some advice.

Differences:

Telnet and echo implement HTTP access, which is different from opening a read/write socket. Opening a socket channel can exchange data. Input the command, activity result, and command to obtain the result. Telnet cannot be handled using echo.

 

 

 

Iii. Monitor memcache status through shell script redirection

Instance:

#! /Bin/sh # Pass in the IP address and port and send the command to get the returned data # copyright chengmo QQ: 8292669 # The function is usually put on the top of function sendmsg () {MSG = $1; echo "$1"> & 8; getout;} # Sends a command to the socket channel and calls the function getout () to obtain the returned parameter () {# Read command-u reads data from open file descriptor 8,-D reads data and ignores: \ r linefeed while read-U 8-d $ '\ R' name; do if ["$ {name}" = "end"-o "$ {name}" = "error"]; then break; FI; echo $ name; done} # Because memcached finishes communication each time, it will return end or error. If [$ # -Lt 2]; thenecho "Usage: $0 host port [command]"; Exit 1; FI; [[$ #-GT 2] & command = $3; # Set the default value. If the command is defined as statscommand = "$ {command = stats}"; host = "$1"; Port = "$2 "; exec 8 <>/dev/tcp/$ {Host}/$ {port}; # Open the channel 8if ["$? "! = "0"]; thenecho "Open $ host $ port fail! "; Exit 1; FI sendmsg" $ command "; # Send the specified command sendmsg" quit "; # Send the exit to the command exec 8 <&-; Exec 8> &-; # disable socket channel exit 0;

 

This is through redirection to implement socket communication, send and obtain the returned example. Actually, the aboveCodeIt seems that only one segment can be sent at a time. Time. We can call sendmsg repeatedly to capture output data. Implement continuous, read, and write operations.

Instance:

 

Other implementation methods:

In fact, Telnet can also be used.

[Chengmo @ centos5 shell] $ (echo "stats"; sleep 2) | Telnet 127.0.0.1 11211

Using NC commands:

[Chengmo @ centos5 shell] $ (echo "stats") | NC 127.5.0.0.1 11211

Open the channel directly without latency

In the second program, we can see that shell can fully process the interaction design. In this case, login to FTP, POP3, and stmp can be implemented similarly. These are implemented through shell socket similar programs. It should not be difficult, but it just captures issues such as sending and parsing.

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.