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