chat Command in Linux



The chat is a Linux program that is used for automated communication with a modem. This command will act as an intermediary between the computer running the Point to Point daemon (PPD) and the remote daemon. The main purpose of the chat command is to establish a connection between a local machine and the remote modem. It uses predefined chat scripts to guide the conversation. Instead of manual input, the chat program automates the exchange of messages with the modem.

Table of Contents

Syntax for chat Command in Linux

The chat command follows a specific format to establish a connection with your modem, this is given below −

chat [ options ] script

Here,

  • chat is the base command that tells the system you want to use the chat functionality.
  • [options] are different flags that can be used with the chat command to change its behavior.
  • script is the chat script itself, a series of commands that tell the chat program what to send to the modem and what responses to expect.

Different Options Available for chat Command

While the core syntax is essential, the chat command offers various options for fine-tuning the communication process. These options are provided in the table below −

Option Description
-f <chat file> This option lets you specify a file containing the chat script. This script defines the conversation flow between you (through the chat program) and your modem.
-t <timeout> This option sets a time limit for the chat program to wait for expected responses from the modem. If the modem does not respond within the set time, the chat program might consider it an error.
-r <report file> Specifies a file where report strings will be written. If you include the keyword "report," the resulting strings will be saved to the specified file; if not used, the stderr file is used for report strings.
-e When you begin a chat script with the "e" option enabled, you have the ability to turn echoing on or off at specific points via the "echo" keyword. When echoing is enabled, all modem output is reflected in the standard error stream (stderr).
E Enables environment variable substitution inside the chat scripts via the standard $xxx syntax.
-v

Requests execution in verbose mode. The chat program keeps a log of the scripts execution state, the text received from the modem, and the strings output delivered to the modem.

By default, logging is done through the syslog, but you can alter the logging method with the -s and -S flags.

-V Requests execution in stderr verbose mode. When the chat command logs information about received text from the modem and output strings delivered to the modem, it typically sends this data to the stderr device (usually the local console).
-s Uses stderr; log messages using -v and error messages are delivered to stderr.
-S Disables the use of syslog. By default, error messages are delivered to syslog. When you use the -S option, it ensures that log messages from -v and error messages are not sent to the standard error stream (stderr). Instead, they are directed elsewhere.
-t <phone number> Passes an arbitrary string (a phone number most often) to substitute for the \t metacharacter in a delivered string.
-u <phone number 2> Passes a second string, a phone number usually to substitute for the \u metacharacter in a send string. When you are connecting an ISDN terminal adapter (TA) that needs two phone numbers, this feature becomes handy.

Lets explore in detail about chat scripts.

Chat Script Basics

A chat script defines communication between two systems and consists of "expect-send" pairs of strings.

Each pair is separated by spaces, having an optional "subexpect-subsend" pair that is separated by a dash.

Example Script

Suppose we have this line in the script −

ogin:-BREAK-ogin: ppp ssword: hello2u2

Breakdown

  • Expect "ogin:". If not received, send a break sequence and expect "ogin:" again.
  • Once "ogin:" is received, send "ppp".
  • Expect the prompt "ssword:". When received, send the password "hello2u2".

Carriage Return

Normally, a carriage return is delivered after the reply string, which is usually not expected in the "expect" string unless you requested it specifically via \r.

Best Practices

The "expect" sequence should only identify the string (avoid variable info). You can use "ogin:" instead of "login:" to handle potential character errors. Apart from that, you can add more robust scripts that include sub-expect sequences for cases when the original string isnt received.

Comments

In chat scripts, you can add comments by beginning a line using the "#" (hash) character. These comment lines are most often neglected by the chat program. If you expect a prompt that begins with a "#" character, you should quote the expect string. For example −

# Please wait for the prompt and then deliver logout string
'# ' logout

Sending Data from a File

When a string begins with an "@" sign, the chat program interprets the remaining part as a filename. It reads the file to obtain the string to send and if the last character in the data read is a newline, it will be removed. This feature allows chat to communicate with other programs. For example, prompting the user and receiving a typed password.

When working with modems, they often report call status strings such as "CONNECTED," "NO CARRIER," or "BUSY." To handle these situations effectively, we can use the ABORT sequence in our chat script.

Here is an example −

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

The script expects nothing initially. It sends the string "ATZ" to the modem. The expected response is "OK." Upon receiving "OK," it dials the telephone number "4441313" using "ATDT." The script waits for the string "CONNECT." If the modem encounters a busy line (sending "BUSY"), the script matches the abort sequence and fails. Similarly, if it receives "NO CARRIER," the script aborts; either of these strings will terminate the chat script.

CLR_ABORT STRINGS

The CLR_ABORT sequence serves to clear previously set ABORT strings; these ABORT strings are stored in an array of a predetermined size (this is determined during compilation). When you use CLR_ABORT, it frees up space for new strings to utilize.

Say Strings

The "SAY" directive is used to allow a script to send strings to the user through the standard error stream in a command-line terminal. When chat is managed by pppd (Point-to-Point Protocol Daemon), and pppd runs as a detached daemon, standard error output typically goes to the file "/var/log/ppp/connect-errors."

You must enclose SAY strings in single or double quotes. If you require carriage return and line feed characters in the output string, be sure to include them explicitly.

You can utilize the "SAY" strings to provide progress messages within script sections where youve disabled "ECHO" but still want to keep the user informed about ongoing processes. An example is −

ABORT BUSY
ECHO OFF
SAY "Dialing your ISP...\n"
'' ATDT4441313
TIMEOUT 60
SAY "Waiting up to 1 minute for connection ... "
CONNECT ''
SAY "Connected, now logging in ...0
ogin: account
ssword: pass
$ SAY "Logged in OK ...0 etc ...
This sequence will display only the SAY strings to the user, while keeping all the details of the script hidden. For instance, if the script works as intended, the user will only see the output of the SAY statements:
Dialing your ISP...
Waiting up to 1 minute for connection ... Connected, now logging in ...
Logged in OK ...

In this script, the SAY strings provide progress messages while keeping other details hidden. Once the script is executed, the user will see −

Dialing your ISP...
Waiting up to 1 minute for connection ...
Connected, now logging in ...
Logged in OK ...

Report String

In chat scripts, a "report" string serves a purpose much like the ABORT string. However, theres a crucial difference: report strings, along with all characters up to the next control character, like a carriage return, are written to a report file.

Heres how report strings can be useful −

  • Isolating Transmission Rate − You can use report strings to extract the transmission rate from the connect string of the modem. After that, you can provide that value to the chat user. This analysis typically happens alongside other string processing, such as checking for an expected string.
  • Logic with Expect Strings − When using report strings, youll often combine them with expect strings. The logic involves looking for specific patterns in the communication flow.
  • Same String for Report and Abort − While its not commonly done, you can technically use the same string for both reporting and aborting sequences.

Heres an example of specifying report strings in a script −

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

In this sequence −

The script starts by sending the string "ATDT4441313" to dial the telephone. If it receives the expected response "CONNECT," the rest of the script proceeds. Furthermore, the program records the "CONNECT" string (along with any subsequent characters, like the connection rate) in the expect-file.

CLR_REPORT STRINGS

The CLR_REPORT sequence serves to clear previously set REPORT strings in chat scripts. These REPORT strings are maintained in a predefined size array, which is determined during the compilation stage. When you use CLR_REPORT, it frees up space for cleared entries, making room for new strings to occupy that space.

ECHO

In chat scripts, the "ECHO" options control whether the modems output is echoed to standard error (stderr). You can set this option using the -e flag or by using the ECHO keyword. Heres how it works −

ECHO ON

When the "expect-send" pair ECHO ON is used, echoing is enabled. This means that the output from the modem will be visible. However, only specific parts of the conversation will be shown. For example, in the following script −

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

At the beginning, any output related to modem configuration and dialing remains hidden. However, once the "CONNECT" (or "BUSY") message is received, all subsequent output becomes visible and is echoed.

ECHO OFF − Conversely, using ECHO OFF disables echoing. In this mode, the user wont see any output related to modem configuration or dialing, except for specific messages like "CONNECT" or "BUSY."

HANGUP

The "HANGUP" option shows whether a modem hangup is treated as an error or not. This feature is especially valuable in scripts designed for dialing systems that disconnect and subsequently initiate a callback to your system. Lets break it down −

HANGUP ON/OFF − When the "HANGUP" option is set to "OFF," the chat script continues executing even after the modem hangs up. This behavior is particularly relevant during the initial stages of logging in to a callback system.

If the modem hangs up, the script persists, waiting for the incoming call and potentially handling a second-stage login prompt.

Once the incoming call connects, apply the "HANGUP ON" directive to revert to normal hang-up behavior.

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

The default initial timeout value is 45 seconds, however, you can adjust this using the -t parameter.

EOT

The "EOT" (End of Transmission) string informs the chat program to send an EOT character to the remote system. Typically, this represents the end of a file or data transmission. Unlike other control characters, an EOT doesnt include a return character following it. You can embed the EOT sequence into a send string using the "^D" sequence.

Generating Break

The "BREAK" reply string triggers a break condition to be sent; when received by the remote system, this signal typically changes the transmission rate. Its often used to cycle through available transmission rates until a valid login prompt is received. YTo embed the break sequence into a send string, you can use the \K sequence.

Escape Sequences

Escape sequences are used in expect and reply strings. Here are some common escape sequences −

Espace Sequence Description
'' (two single quotes or double quotes) Represents a null string and if you send such string, it will still include the return character.
\b Specifies a backspace character.
\c Suppresses the newline at the end of a reply string, allowing you to send a string without a trailing return character. For example, if you use the sequence "hello\c," it will simply send the characters "h," "e," "l," "l," "o."
\d Specifies a specific escape sequence.
\K This sequence is used to insert a BREAK signal (not valid in the expect string).
\n Send a linefeed character or new line.
\N Send a null character; you can also represent this as \0 (not valid in the expect string).
\p Introduce a brief pause of 1/10th of a second, however, its important to note that this sequence is not valid within the expect string.
\q Prevents the string from being written to the SYSLOG file. Instead, the log entry will display "???" in place of the suppressed string. Ensure that this sequence is not valid within the expect string.
\r Sends or expects a carriage return.
\s Represents a space character in the string; useful when you dont want to quote strings containing spaces. For example, "HI TIM" and "HI\sTIM" are equivalent.
\t Sends or expects a tab character.
\T Used for sending the phone number string specified with the -T option. However, you must ensure that this sequence is not valid within the expect string.
\U Allows you to send the second phone number string specified with the -U option. However, it is not valid within the expect string.
\\ Delivers or expects a backslash character.
\ddd Collapses octal digits into a single ASCII character and delivers that character. However, you must note that some characters may not be valid within the expect string.
^C Replace it with the control character denoted by C. For example, if the character DC1 (17) is involved, it appears as ^Q. Keep in mind that certain characters are not valid within the expect string.

Environment Variables

In chat scripts, you can use the environment variables by adding the -E option. For introducing the name of an environment variable for substitution, you simply use the metacharacter $. If the substitution fails due to an unset environment variable, the variable remains unchanged without any replacement.

Termination Code

Lets discuss the termination codes for the chat program −

Termination Code Description
0 Normal termination of the program. This specifies that the script executed without errors and reached its normal state.
1 This error message shows that either one or more parameters are invalid, or the expect string provided was large enough for the internal buffers. In either case, it suggests that the program was not executed correctly.
2 If an error occurs during program execution, it could be due to various reasons. These might include read or write operation failures or receiving a signal like SIGINT (which typically occurs when the user interrupts the program). In any case, it shows that the program encountered an error during its execution.
3

The error message indicates that a timeout event occurred due to an expect string without a corresponding "-subsend" string.

In other words, the program expected a specific response but didnt receive it within the specified time frame. This might indicate incorrect script programming or an unexpected event preventing the expected string from being found.

4, 5, 6, and 7 and so on These codes represent specific ABORT conditions. By analyzing the termination code, you can determine which event caused the script to terminate. For example, distinguishing between receiving "BUSY" versus "NO DIAL TONE" allows informed decisions on whether to retry.

Conclusion

chat is a powerful program that helps you in automating the communication with the modem. This tutorial has explored the syntax of chat commands along with different options that can be used with the command.

A detailed description about the chat scripts is also provided to help users in getting a full overview of the chat program. Understanding the description will help users interact with devices, establish connections, and exchange data.

Advertisements