Introduction to Linux Basics--Network Tools--chat

1 Overview

Automatic dialogue script and modem, chat program defines the dialogue exchange between computer and modem. Its main purpose is to establish a connection between the point-to-point protocol daemon (pppd) and the remote pppd process.

2. Grammar

chat [ options ] script

3. Options

Options Description
-f < chat file> Read the chat script from the chat file. The use of this option and chat script parameters are mutually exclusive. The user must have read access to the file. Multiple lines are allowed in the file. You should use spaces or horizontal tabs to separate strings.
-t < timeout> Set the timeout for the expected string to be received. If the string is not received within the time limit, the reply string will not be sent. An alternate response may be sent, or if there is no alternate response string, the script will fail. A failed script will cause the chat program to terminate with a non-zero error code. You can also use a timeout string to specify the timeout.
-r < report file> Set the output file of the report string. If the keyword REPORT is used, the generated string will be written to the file. If this option is not used and you still use report keywords, the stderr file is used for report strings.
-e Start by turning on the echo option. By using the ECHO keyword, you can also turn on or off echo in a specific location of the chat script. When echo is enabled, all modem output will be echoed to stderr.
-E Use standard $xxx syntax in chat scripts to enable environment variable substitution.
-v Request to execute the chat script in verbose mode. The chat program will record the execution status of the chat script, as well as all the text received from the modem and the output string sent to the modem. The default is to log in through SYSLOG; you can use the -S and -S flags to change the logging method.
-V Request a chat chat script in stderr detailed mode. The chat program then records all the text received from the modem and the output string sent to the modem to the stderr device. This device is usually a local console running a chat or pppd program.
-s Use stderr. All log messages from'-v' and all error messages will be sent to stderr.
-S Do not use system logs. By default, error messages are sent to the system log. Using -S will prevent log messages and error messages from'-v' from being sent to the system log.
-T < phone number> Pass in an arbitrary string, usually a phone number, and it will be replaced with \T replacement metacharacters in the sending string.
-U < phone number 2> Pass in the second string, usually a phone number, and it will be replaced with \U replacement metacharacters in the sent string. This is useful when dialing requires a two-digit ISDN terminal adapter.

4.chat script

The chat script defines the communication. The script contains one or more "expect-send" string pairs, separated by spaces, and an optional "subexpect-subsend" string pair, separated by dashes.

ogin:-BREAK-ogin: ppp ssword: hello2u2

This line indicates that the chat program should expect the string "ogin:". If it does not receive the login prompt within the allocated time interval, it will be assigned to send an interrupt sequence to the remote, and then expect the string "ogin:". If the first "ogin:" is received, no interrupt sequence will be generated. Once it receives the login prompt, the chat program will send the string ppp and then expect the prompt "ssword:". When it receives the password prompt, it will send the password hello2u2. The carriage return is usually sent after the reply string. Unless specifically requested with \r, it will not appear in the sequence of characters in the "expect" string. The expect sequence should only contain what is needed to identify the string. Because it is usually stored in a disk file, it should not contain variable information. It is generally unacceptable to use time strings, network identification strings, or other variable data segments as expected strings.

5. Comments

Comments can be embedded in chat scripts. The comment is the line starting with the # (hash) character in the first column. Such comment lines will be ignored by the chat program. If you expect the'#' character as the first character of the expect sequence, you should quote the expect string. If you want to wait for a prompt that starts with the # (hash) character, you must write:
#Now wait for the prompt and send the logout string
"#" to logout

6. Send data from file

If the string to be sent starts with the @ symbol, the rest of the string is taken as the name of the file to be read to obtain the string to be sent. If the last character of the data read is a newline character, delete it. The file can be a named pipe (or fifo) instead of a regular file. This provides a way for chat to communicate with another program, for example, a program that prompts the user and receives the entered password.

7.ABORT string

Many modems report the status of the call as a string. These strings can be connected, they can be carrier-free, or they can be busy. If the modem cannot connect to the remote server, the script usually needs to be terminated. The difficulty is that the script does not know which modem string it might have received. In one attempt, it may receive busy, and the next time it may not receive the operator. These "abort" strings can be specified in the script using the abort sequence. It is written in the script, as shown in the following example:

ABORT BUSY ABORT 'NO CARRIER' '' ATZ OK ATDT5551212 CONNECT

There is no expectation for this sequence; then the string ATZ is sent. The expected response is the string OK. When it receives OK, the string ATDT5551212 dials the phone. The expected string is CONNECT. If the string CONNECT is received, the rest of the script is executed. However, if the modem detects a busy call, it will send the string busy. This will cause the string to match the abort character sequence. The script will fail because it found a string that matches the aborted string. If it receives the string NO CARRIER, it will abort for the same reason. Any string can be received. Any string will terminate the chat script.

8. CLR_ABORT string

This sequence allows the previously set abort string to be cleared. The abort string is stored in an array of a predetermined size (at compile time); CLR_ABORT will reclaim the space of the cleared entry so that the new string can use the space.

9.SAY string

The SAY instruction allows the script to send a string to the terminal user via standard error. If the chat is run by pppd, and pppd runs as a daemon (separate from its controlling terminal), standard errors will usually be redirected to the /etc/ppp/connect-errors file. SAY strings must be enclosed in single or double quotes. If carriage returns and line feeds are required in the output string, they must be explicitly added to the string. The SAY string can be used to provide progress messages in the part of the script where you want "ECHO OFF" but still let the user know what happened. An example is:

ABORT BUSY
     ECHO OFF
     SAY "Dialling your ISP...\n"
     '' ATDT5551212
     TIMEOUT 120
     SAY "Waiting up to 2 minutes for connection ... "
     CONNECT ''
     SAY "Connected, now logging in ...\n"
     ogin: account
     ssword: pass
     $ \c
     SAY "Logged in OK ...\n" etc ...

This sequence will only show the SAY string to the user, and all the details of the script will remain hidden. For example, if the above script works, the user will see:

Dialling your ISP...
Waiting up to 2 minutes for connection ... Connected, now logging in ...
Logged in OK ...

10.REPORT string

The REPORT string is similar to the abort string. The difference is that all characters of the string and the next control character (such as carriage return) are written to the REPORT file. The REPORT string can be used to isolate the transmission rate of the modem connection string and return the value to the chat user. The analysis of the REPORT string logic is carried out together with other string processing, such as finding the expected string. It may not be very useful to use the same string for the report and the abort sequence, but it is possible. The report string does not change the completion code of the program. These "report" strings can be specified in the script using the report sequence. It is written in the script, as shown in the following example:

REPORT CONNECT ABORT BUSY '' ATDT5551212 CONNECT '' ogin: account

There is no expectation for this sequence; then the string ATDT5551212 is sent to make the call. The expected string is CONNECT. If the string CONNECT is received, the rest of the script is executed. In addition, the program will write the string "CONNECT" and any characters after it, such as the connection rate, to the desired file.

11.CLR_REPORT string

This sequence allows clearing the previously set REPORT string. The REPORT string is stored in an array with a predetermined size (at compile time); CLR_REPORT will reclaim space for cleared entries so that new strings can use the space.

12.ECHO

The echo option controls whether to echo the modem's output to stderr. This option can be set with the -e option, but it can also be controlled by the ECHO keyword. "Expect-send" enables ECHO for ECHO ON, and ECHO OFF disables ECHO. Using this keyword, you can choose which parts of the conversation should be visible. For example, the following script:

ABORT   'BUSY'
ABORT   'NO CARRIER'
''      ATZ
OK\r\n  ATD1234567
\r\n    \c
ECHO    ON
CONNECT \c
ogin:   account

All output generated by the modem configuration and dialing is invisible, but starting from the CONNECT (or BUSY) message, everything will be echoed.

13.HANGUP

The hang option controls whether a modem hang should be treated as an error or not. This option is very useful in the script of the dialing system, it will suspend and call back your system. The suspend option can be on or off. When the suspension is set and the modem is suspended (for example, after logging into the callback system in the first stage), chat will continue to run the script (for example, waiting for incoming calls and the second stage login prompt). Once the incoming call is connected, you should use the suspend command to reinstall the normal suspend signal behavior. Here is a (simple) example script:

ABORT   'BUSY'
              ''      ATZ
              OK\r\n  ATD1234567
              \r\n    \c
              CONNECT \c
              'Callback login:' call_back_ID
              HANGUP OFF
              ABORT "Bad Login"
              'Callback Password:' Call_back_password
              TIMEOUT 120
              CONNECT \c
              HANGUP ON
              ABORT "NO CARRIER"
              ogin:--BREAK--ogin: real_account
              etc ...

14.TIMEOUT

The initial timeout value is 45 seconds. This can be changed using the -t parameter. You can also specify "TIMEOUT 0". To change the timeout value of the next expect string, you can use the following example:

ATZ OK ATDT5551212 CONNECT TIMEOUT 10 ogin:--ogin: TIMEOUT 5 assword: hello2u2

This will change the timeout period to 10 seconds while waiting for the login: prompt. Then, when it looked for the password hint, the timeout period was changed to 5 seconds. Once the timeout is changed, it will remain valid until it is changed again.

15. Send EOT

The special EOT reply string indicates that the chat program should send an EOT character to the remote. This is usually the end-of-file character sequence. No return characters are sent after EOT. The EOT sequence can be embedded in the sending string using the sequence ^D.

16. Generate an interrupt

The interrupted special reply string will cause the interruption condition to be sent. An interrupt is a special signal on the transmitter. The normal processing on the receiver is to change the transmission rate. It can be used to cycle the available transfer rate on a remote server until a valid login prompt can be received. You can use the \K sequence to embed the interrupt sequence into the sending string.

17. Escape sequence

Expectation and response strings may contain escape sequences. All sequences in the reply string are legal.

18. Environment variables

If the -E option is specified on the command line, you can use environment variables in the chat script. The meta character $ is used to introduce the name of the environment variable to be replaced. If the replacement fails because the requested environment variable is not set, nothing will be replaced for the variable.

19. Termination code

The chat program will terminate with the following completion code.

Code value Description
0 The normal termination of the program. This shows that no normal errors occurred when the script was executed.
1 One or more parameters are invalid, or the expected string is too large for the internal buffer. This indicates that the program was not executed correctly.
2 An error occurred during program execution. This may be because the read or write operation failed for some reason or the chat received a signal (such as SIGINT).
3 When there is an expect string but no "-subsend" string, a timeout event will occur. This may mean that you did not write the script correctly for the condition, or that some unexpected event occurred and the expected string could not be found.
4 The first string marked as an abort condition appears.
5 The second string marked as an abort condition appears.
6 The third string marked as a termination condition appears.
7 The fourth character string marked as an abort condition appears.
Other termination codes are also strings marked as termination conditions.

Reminder:
If there is any unclear description in the above article, please comment in the comment area. If you have time, we will reply as soon as possible, thank you!

Guess you like

Origin blog.csdn.net/qq_20677327/article/details/107950966