1.\" $OpenBSD: chat.8,v 1.21 2023/08/07 06:21:53 guenther Exp $ 2.\" Id: chat.8,v 1.7 1998/02/04 01:35:49 paulus Exp $ 3.\" manual page [] for chat 1.8 4.Dd $Mdocdate: August 7 2023 $ 5.Dt CHAT 8 6.Os 7.Sh NAME 8.Nm chat 9.Nd automated conversational script with a modem 10.Sh SYNOPSIS 11.Nm chat 12.Op Fl eSsVv 13.Bk -words 14.Op Fl f Ar chat_file 15.Op Fl r Ar report_file 16.Op Fl T Ar phone_number 17.Op Fl t Ar timeout 18.Op Fl U Ar phone_number_2 19.Ar script 20.Ek 21.Sh DESCRIPTION 22The 23.Nm 24program defines a conversational exchange between the computer and the modem. 25Its primary purpose is to establish a connection between the 26Point-to-Point Protocol Daemon 27.Pf ( Xr pppd 8 ) 28and the remote's pppd process. 29.Sh OPTIONS 30.Bl -tag -width Ds 31.It Fl e 32Start with the echo option turned on. 33Echoing may also be turned on or off at specific points in the chat script 34by using the 35.Ic ECHO 36keyword. 37When echoing is enabled, all output from the modem is echoed to 38.Ar stderr . 39.It Fl f Ar chat_file 40Read the chat script from the 41.Ar chat_file . 42The use of this option is mutually exclusive with the chat 43.Ar script 44parameter. 45The user must have read access to the file. 46Multiple lines are permitted in the file. 47Space or horizontal tab characters should be used to separate the strings. 48.It Fl r Ar report_file 49Set the file for output of the report strings. 50If you use the keyword 51.Ic REPORT , 52the resulting strings are written to this file. 53If this option is not used and you still use 54.Ic REPORT 55keywords, the 56.Ar stderr 57file is used for the report strings. 58.It Fl S 59Do not use 60.Xr syslog 3 . 61By default, error messages are logged through 62.Xr syslog 3 63with facility 64.Dq local2 65and level 66.Dq warning . 67The use of 68.Fl S 69will prevent both log messages from 70.Fl v 71and error messages from being logged via 72.Xr syslog 3 . 73.It Fl s 74Use 75.Ar stderr . 76All log messages from 77.Fl v 78and all error messages will be sent to 79.Ar stderr . 80.It Fl T Ar phone_number 81Pass in an arbitrary string, usually a phone number, that will be 82substituted for the \eT substitution metacharacter in a send string. 83.It Fl t Ar timeout 84Set the timeout for the expected string to be received. 85If the string is not received within the time limit then the reply string 86is not sent. 87An alternate reply may be sent or the script will fail if there 88is no alternate reply string. 89A failed script will cause the 90.Nm 91program to terminate with a non-zero error code. 92.It Fl U Ar phone_number_2 93Pass in a second string, usually a phone number, that will be 94substituted for the \eU substitution metacharacter in a send string. 95This is useful when dialing an ISDN terminal adapter that requires two numbers. 96.It Fl V 97Request that the chat script be executed in a 98.Ar stderr 99verbose mode. 100The 101.Nm 102program will then log all text received from the modem and the output strings 103sent to the modem to the 104.Ar stderr 105device. 106This device is usually the local console at the station running the 107.Nm 108or 109.Xr pppd 8 110program. 111.It Fl v 112Request that the chat script be executed in a verbose mode. 113The 114.Nm 115program will then log the execution state of the chat script as well as all 116text received from the modem and the output strings sent to the modem. 117The default is to log through 118.Xr syslog 3 119with level 120.Dq info , 121though this may be altered with the 122.Fl S 123and 124.Fl s 125flags. 126.It Ar script 127If the script is not specified in a file with the 128.Fl f 129option, then the script is included as parameters to the 130.Nm 131program. 132.El 133.Sh CHAT SCRIPT 134The chat script defines the communications. 135.Pp 136A script consists of one or more 137.Dq expect-send 138pairs of strings, 139separated by spaces, with an optional 140.Dq subexpect-subsend 141string pair, separated by a dash as in the following example: 142.Pp 143.Dl ogin:-BREAK-ogin: ppp ssword: hello2u2 144.Pp 145This line indicates that the 146.Nm 147program should expect the string 148.Dq ogin: . 149If it fails to receive a login prompt within the time interval allotted, 150it is to send a break sequence to the remote and then expect the 151string 152.Dq ogin: . 153If the first 154.Dq ogin: 155is received then the break sequence is not generated. 156.Pp 157Once it receives the login prompt, the 158.Nm 159program will send the string ppp and then expect the prompt 160.Dq ssword: . 161When it receives the prompt for the password, it will send the password 162hello2u2. 163.Pp 164A carriage return is normally sent following the reply string. 165It is not expected in the 166.Dq expect 167string unless it is specifically requested by using the \er character sequence. 168.Pp 169The expect sequence should contain only what is needed to identify the string. 170Since it is normally stored on a disk file, it should not contain 171variable information. 172It is generally not acceptable to look for time strings, network 173identification strings, or other variable pieces of data as an expect string. 174.Pp 175To help correct for characters which may be corrupted during the initial 176sequence, look for the string 177.Dq ogin: 178rather than 179.Dq login: . 180It is possible that the leading 181.Dq l 182character may be received in error and you may never find the string 183even though it was sent by the system. 184For this reason, scripts look for 185.Dq ogin: 186rather than 187.Dq login: 188and 189.Dq ssword: 190rather than 191.Dq password: . 192.Pp 193A very simple script might look like this: 194.Pp 195.Dl ogin: ppp ssword: hello2u2 196.Pp 197In other words, expect ....ogin:, send ppp, expect ...ssword:, send hello2u2. 198.Pp 199In actual practice, simple scripts are rare. 200At the very least, you should include sub-expect sequences should the 201original string not be received. 202For example, consider the following script: 203.Pp 204.Dl ogin:--ogin: ppp ssword: hello2u2 205.Pp 206This would be a better script than the simple one used earlier. 207This would look for the same login: prompt. 208If one is not received, a single return sequence is sent and then it will 209look for login: again. 210Should line noise obscure the first login prompt then sending the empty line 211will usually generate a login prompt again. 212.Sh COMMENTS 213Comments can be embedded in the chat script. 214A comment is a line which starts with the 215.Sq # 216(hash) character in column 1. 217Such comment lines are just ignored by the 218.Nm 219program. 220If a 221.Sq # 222character is to be expected as the first character of the expect sequence, 223you should quote the expect string. 224If you want to wait for a prompt that starts with a 225.Sq # 226(hash) character, you would have to write something like this: 227.Bd -literal -offset indent 228# Now wait for the prompt and send logout string 229\'# ' logout 230.Ed 231.Sh ABORT STRINGS 232Many modems will report the status of the call as a string. 233These strings may be 234.Em CONNECT 235or 236.Em NO CARRIER 237or 238.Em BUSY . 239It is often desirable to terminate the script should the modem fail to 240connect to the remote. 241The difficulty is that a script would not know exactly which modem string 242it may receive. 243On one attempt it may receive 244.Em BUSY , 245while the next time it may receive 246.Em NO CARRIER . 247.Pp 248These 249.Dq abort 250strings may be specified in the script using the 251.Ic ABORT 252sequence. 253It is written in the script as in the following example: 254.Pp 255.Dl "ABORT BUSY ABORT 'NO CARRIER' '' ATZ OK ATDT5551212 CONNECT" 256.Pp 257This sequence will expect nothing; and then send the string ATZ. 258The expected response to this is the string 259.Em OK . 260When it receives 261.Em OK , 262it sends the string ATDT5551212 to dial the telephone. 263The expected string is 264.Em CONNECT . 265If the string 266.Em CONNECT 267is received, the remainder of the script is executed. 268However, should the modem find a busy telephone, it will send the string 269.Em BUSY . 270This will cause the string to match the abort character sequence. 271The script will then fail because it found a match to the abort string. 272If it received the string 273.Em NO CARRIER , 274it will abort for the same reason. 275Either string may be received. 276Either string will terminate the chat script. 277.Sh CLR_ABORT STRINGS 278This sequence allows for clearing previously set 279.Ic ABORT 280strings. 281.Ic ABORT 282strings are kept in an array of a pre-determined size (at compilation time); 283.Ic CLR_ABORT 284will reclaim the space for cleared entries so that new strings can use 285that space. 286.Sh SAY STRINGS 287The 288.Ic SAY 289directive allows the script to send strings to the user 290at the terminal via standard error. 291If 292.Nm 293is being run by 294.Xr pppd 8 , 295and pppd is running as a daemon (detached from its controlling terminal), 296standard error will normally be redirected to the file 297.Pa /etc/ppp/connect-errors . 298.Pp 299.Ic SAY 300strings must be enclosed in single or double quotes. 301If carriage return and line feed are needed in the string to be output, 302you must explicitly add them to your string. 303.Pp 304The 305.Ic SAY 306strings could be used to give progress messages in sections of 307the script where you want to have 'ECHO OFF' but still let the user 308know what is happening. 309An example is: 310.Bd -literal -offset indent 311ABORT BUSY 312ECHO OFF 313SAY "Dialling your ISP...\en" 314\'' ATDT5551212 315TIMEOUT 120 316SAY "Waiting up to 2 minutes for connection ... " 317CONNECT '' 318SAY "\enConnected, now logging in ...\en" 319ogin: account 320ssword: pass 321$ \c 322SAY "Logged in OK ...\en" 323etc ... 324.Ed 325.Pp 326This sequence will only present the 327.Ic SAY 328strings to the user and all the details of the script will remain hidden. 329For example, if the above script works, the user will see: 330.Bd -literal -offset indent 331Dialling your ISP... 332Waiting up to 2 minutes for connection ... 333Connected, now logging in ... 334Logged in OK ... 335.Ed 336.Sh REPORT STRINGS 337A report string is similar to the 338.Ic ABORT 339string. 340The difference is that the strings, and all characters to the next control 341character such as a carriage return, are written to the report file. 342.Pp 343The report strings may be used to isolate the transmission rate of the 344modem's connect string and return the value to the 345.Nm 346user. 347The analysis of the report string logic occurs in conjunction with the 348other string processing such as looking for the expect string. 349The use of the same string for a report and abort sequence is probably not 350very useful; however, it is possible. 351.Pp 352The report strings do not change the completion code of the program. 353.Pp 354These 355.Dq report 356strings may be specified in the script using the 357.Ic REPORT 358sequence. 359It is written in the script as in the following example: 360.Pp 361.Dl "REPORT CONNECT ABORT BUSY '' ATDT5551212 CONNECT '' ogin: account" 362.Pp 363This sequence will expect nothing; and then send the string 364ATDT5551212 to dial the telephone. 365The expected string is 366.Em CONNECT . 367If the string 368.Em CONNECT 369is received, the remainder of the script is executed. 370In addition the program will write to the expect-file the string 371.Dq CONNECT 372plus any characters which follow it such as the connection rate. 373.Sh CLR_REPORT STRINGS 374This sequence allows for clearing previously set 375.Ic REPORT 376strings. 377.Ic REPORT 378strings are kept in an array of a pre-determined size (at compilation time); 379.Ic CLR_REPORT 380will reclaim the space for cleared entries so that new strings can use 381that space. 382.Sh ECHO 383The echo options controls whether the output from the modem is echoed to 384.Ar stderr . 385This option may be set with the 386.Fl e 387option, but it can also be controlled by the 388.Ic ECHO 389keyword. 390The 391.Dq expect-send 392pair 393.Ic ECHO ON 394enables echoing, and 395.Ic ECHO OFF 396disables it. 397With this keyword you can select which parts of the conversation should be 398visible. 399For instance, with the following script: 400.Bd -literal -offset indent 401ABORT 'BUSY' 402ABORT 'NO CARRIER' 403\&'' ATZ 404OK\er\en ATD1234567 405\er\en \ec 406ECHO ON 407CONNECT \ec 408ogin: account 409.Ed 410.Pp 411all output resulting from modem configuration and dialing is not visible, 412but starting with the 413.Em CONNECT 414(or 415.Em BUSY ) 416message, everything will be echoed. 417.Sh HANGUP 418The 419.Ic HANGUP 420options control whether a modem hangup should be considered as an error or not. 421This option is useful in scripts for dialing systems which will hang up and 422call your system back. 423The 424.Ic HANGUP 425options can be 426.Ic ON 427or 428.Ic OFF . 429.Pp 430When 431.Ic HANGUP 432is set 433.Ic OFF 434and the modem hangs up (e.g., after the first stage of logging in to a 435callback system), 436.Nm 437will continue running the script 438(e.g., waiting for the incoming call and second-stage login prompt). 439As soon as the incoming call is connected, you should use the 440.Ic HANGUP ON 441directive to reinstall normal hangup signal behavior. 442Here is an example script: 443.Bd -literal -offset indent 444ABORT 'BUSY' 445\&'' ATZ 446OK\er\en ATD1234567 447\er\en \ec 448CONNECT \ec 449\'Callback login:' call_back_ID 450HANGUP OFF 451ABORT "Bad Login" 452\'Callback Password:' Call_back_password 453TIMEOUT 120 454CONNECT \ec 455HANGUP ON 456ABORT "NO CARRIER" 457ogin:--BREAK--ogin: real_account 458etc ... 459.Ed 460.Sh TIMEOUT 461The initial timeout value is 45 seconds. 462This may be changed using the 463.Fl t 464parameter. 465.Pp 466The following example illustrates how to change the timeout value for 467the next expect string: 468.Pp 469.Dl "ATZ OK ATDT5551212 CONNECT TIMEOUT 10 ogin:--ogin: TIMEOUT 5 assword: hello2u2" 470.Pp 471This will change the timeout to 10 seconds when it expects the login: prompt. 472The timeout is then changed to 5 seconds when it looks for the password prompt. 473.Pp 474The timeout, once changed, remains in effect until it is changed again. 475.Sh SENDING EOT 476The special reply string of 477.Ic EOT 478indicates that the 479.Nm 480program should send an EOT character to the remote. 481This is normally the End-of-file character sequence. 482A return character is not sent following the EOT. 483The EOT sequence may be embedded into the send string using the sequence 484.Em ^D . 485.Sh GENERATING BREAK 486The special reply string of 487.Ic BREAK 488will cause a break condition to be sent. 489The break is a special signal on the transmitter. 490The normal processing on the receiver is to change the transmission rate. 491It may be used to cycle through the available transmission rates on 492the remote until you are able to receive a valid login prompt. 493The break sequence may be embedded into the send string using the 494.Em \eK 495sequence. 496.Sh ESCAPE SEQUENCES 497The expect and reply strings may contain escape sequences. 498All of the sequences are legal in the reply string. 499Many are legal in the expect string. 500Those which are not valid in the expect sequence are so indicated. 501.Bl -tag -width Ds 502.It '' 503Expects or sends a null string. 504If you send a null string then it will still send the return character. 505This sequence may be a pair of either apostrophe or quote characters. 506.It \eb 507Represents a backspace character. 508.It \ec 509Suppresses the newline at the end of the reply string. 510This is the only method to send a string without a trailing return character. 511It must be at the end of the send string. 512For example, the sequence 513.Qq hello\ec 514will simply send the characters h, e, l, l, o. 515(Not valid in expect.) 516.It \ed 517Delay for one second. 518The program uses 519.Xr sleep 3 520to sleep for one second. 521(Not valid in expect.) 522.It \eK 523Insert a 524.Ic BREAK . 525(Not valid in expect.) 526.It \en 527Send a newline or linefeed character. 528.It \eN 529Send a NUL character. 530The same sequence may be represented by \e0. 531(Not valid in expect.) 532.It \ep 533Pause for a fraction of a second. 534The delay is 1/10th of a second. 535(Not valid in expect.) 536.It \eq 537Suppress writing the string to the 538.Xr syslog 3 539file. 540The string 541.Dq hidden 542is written to the log in its place. 543(Not valid in expect.) 544.It \er 545Send or expect a carriage return. 546.It \es 547Represents a space character in the string. 548This may be used when it is not desirable to quote the strings which 549contain spaces. 550The sequence 'HI TIM' and HI\esTIM are the same. 551.It \et 552Send or expect a tab character. 553.It \e\e 554Send or expect a backslash character. 555.It \eddd 556Collapse the octal digits (ddd) into a single ASCII character and send that 557character. 558(Some characters are not valid in expect.) 559.It ^C 560Substitute the sequence with the control character represented by C. 561For example, the character DC1 (17) is shown as ^Q. 562(Some characters are not valid in expect.) 563.El 564.Sh TERMINATION CODES 565The 566.Nm 567program will terminate with the following completion codes: 568.Bl -tag -width Ds 569.It 0 570The normal termination of the program. 571This indicates that the script was executed without error to the normal 572conclusion. 573.It 1 574One or more of the parameters are invalid or an expect string was too 575large for the internal buffers. 576This indicates that the program was not properly executed. 577.It 2 578An error occurred during the execution of the program. 579This may be due to a read or write operation failing for some reason or 580.Nm 581receiving a signal such as 582.Dv SIGINT . 583.It 3 584A timeout event occurred when there was an 585.Dq expect 586string without having a 587.Dq \-subsend 588string. 589This may mean that you did not program the script correctly for the condition 590or that some unexpected event has occurred and the expected string could not 591be found. 592.It 4 593The first string marked as an 594.Ic ABORT 595condition occurred. 596.It 5 597The second string marked as an 598.Ic ABORT 599condition occurred. 600.It 6 601The third string marked as an 602.Ic ABORT 603condition occurred. 604.It 7 605The fourth string marked as an 606.Ic ABORT 607condition occurred. 608.It ... 609The other termination codes are also strings marked as an 610.Ic ABORT 611condition. 612.El 613.Pp 614Using the termination code, it is possible to determine which event 615terminated the script. 616It is possible to decide if the string 617.Dq BUSY 618was received from the modem as opposed to 619.Dq NO DIAL TONE . 620While the first event may be retried, the second will probably have little 621chance of succeeding during a retry. 622.\" .Sh SEE ALSO 623.\" Additional information about chat scripts may be found with UUCP 624.\" documentation. 625.\" The chat script was taken from the ideas proposed 626.\" by the scripts used by the uucico program. 627.\" .Pp 628.\" .Xr uucp 1 629.Sh COPYRIGHT 630The 631.Nm 632program is in the public domain. 633This is not the GNU public license. 634If it breaks then you get to keep both pieces. 635