xref: /freebsd/usr.bin/chat/chat.8 (revision c6063d0da8e1f8d8452b0c2ee1c7ad718c1a7052)
1.\" $FreeBSD$
2.Dd September 10, 2012
3.Dt CHAT 8
4.Os
5.Sh NAME
6.Nm chat
7.Nd Automated conversational script with a modem
8.Sh SYNOPSIS
9.Nm
10.Op Fl eSsVv
11.Op Fl f Ar chat-file
12.Op Fl r Ar report-file
13.Op Fl T Ar phone-number
14.Op Fl t Ar timeout
15.Op Fl U Ar phone-number2
16.Op Ar script
17.Sh DESCRIPTION
18The
19.Nm
20program defines a conversational exchange between the
21computer and the modem.
22Its primary purpose is to establish the
23connection between the Point-to-Point Protocol Daemon
24.Pq pppd
25and the remote's pppd process.
26.Sh OPTIONS
27.Bl -tag -width indent
28.It Fl e
29Start with the echo option turned on.
30Echoing may also be turned on
31or off at specific points in the chat script by using the ECHO
32keyword.
33When echoing is enabled, all output from the modem is echoed
34to
35.Em stderr .
36.It Fl f Ar chat-file
37Read the chat script from the chat file.
38The use of this option
39is mutually exclusive with the chat script parameters.
40The user must
41have read access to the file.
42Multiple lines are permitted in the file.
43Space or horizontal tab characters should be used to separate
44the strings.
45.It Fl r Ar report-file
46Set the file for output of the report strings.
47If you use the keyword
48.Dv REPORT ,
49the resulting strings are written to this file.
50If this
51option is not used and you still use
52.Dv REPORT
53keywords, the
54.Pa stderr
55file is used for the report strings.
56.It Fl S
57Do not use
58.Xr syslog 3 .
59By default, error messages are sent to
60.Xr syslog 3 .
61The use of
62.Fl S
63will prevent both log messages from
64.Fl v
65and error messages from being sent to
66.Xr syslog 3 .
67.It Fl s
68Use
69.Em stderr .
70All log messages from
71.Fl v
72and all error messages will be
73sent to
74.Em stderr .
75.It Fl T Ar phone-number
76Pass in an arbitrary string, usually a phone number, that will be
77substituted for the \\T substitution metacharacter in a send string.
78.It Fl t Ar timeout
79Set the timeout for the expected string to be received.
80If the string
81is not received within the time limit then the reply string is not
82sent.
83An alternate reply may be sent or the script will fail if there
84is no alternate reply string.
85A failed script will cause the
86.Nm
87program to terminate with a non-zero error code.
88.It Fl U Ar phone-number2
89Pass in a second string, usually a phone number, that will be
90substituted for the \\U substitution metacharacter in a send string.
91This is useful when dialing an ISDN terminal adapter that requires two
92numbers.
93.It Fl V
94Request that the
95.Nm
96script be executed in a
97.Em stderr
98verbose mode.
99The
100.Nm
101program will then log all text received from the
102modem and the output strings sent to the modem to the stderr device.
103This
104device is usually the local console at the station running the chat or
105pppd program.
106.It Fl v
107Request that the
108.Nm
109script be executed in a verbose mode.
110The
111.Nm
112program will then log the execution state of the chat
113script as well as all text received from the modem and the output
114strings sent to the modem.
115The default is to log through
116.Xr syslog 3 ;
117the logging method may be altered with the
118.Fl S
119and
120.Fl s
121flags.
122Logging is done to the
123.Em local2
124facility at level
125.Em info
126for verbose tracing and level
127.Em err
128for some errors.
129.El
130.Sh CHAT SCRIPT
131The
132.Nm
133script defines the communications.
134A script consists of one or more "expect-send" pairs of strings,
135separated by spaces, with an optional "subexpect-subsend" string pair,
136separated by a dash as in the following example:
137.Pp
138.D1 ogin:-BREAK-ogin: ppp ssword: hello2u2
139.Pp
140This line indicates that the
141.Nm
142program should expect the string "ogin:".
143If it fails to receive a login prompt within the time interval
144allotted, it is to send a break sequence to the remote and then expect the
145string "ogin:".
146If the first "ogin:" is received then the break sequence is
147not generated.
148.Pp
149Once it received the login prompt the
150.Nm
151program will send the
152string ppp and then expect the prompt "ssword:".
153When it receives the
154prompt for the password, it will send the password hello2u2.
155.Pp
156A carriage return is normally sent following the reply string.
157It is not
158expected in the "expect" string unless it is specifically requested by using
159the \\r character sequence.
160.Pp
161The expect sequence should contain only what is needed to identify the
162string.
163Since it is normally stored on a disk file, it should not contain
164variable information.
165It is generally not acceptable to look for time
166strings, network identification strings, or other variable pieces of data as
167an expect string.
168.Pp
169To help correct for characters which may be corrupted during the initial
170sequence, look for the string "ogin:" rather than "login:".
171It is possible
172that the leading "l" character may be received in error and you may never
173find the string even though it was sent by the system.
174For this reason,
175scripts look for "ogin:" rather than "login:" and "ssword:" rather than
176"password:".
177.Pp
178A very simple script might look like this:
179.Pp
180.D1 ogin: ppp ssword: hello2u2
181.Pp
182In other words, expect ....ogin:, send ppp, expect ...ssword:, send hello2u2.
183.Pp
184In actual practice, simple scripts are rare.
185At the vary least, you
186should include sub-expect sequences should the original string not be
187received.
188For example, consider the following script:
189.Pp
190.D1 ogin:--ogin: ppp ssword: hello2u2
191.Pp
192This would be a better script than the simple one used earlier.
193This would look
194for the same login: prompt, however, if one was not received, a single
195return sequence is sent and then it will look for login: again.
196Should line
197noise obscure the first login prompt then sending the empty line will
198usually generate a login prompt again.
199.Sh COMMENTS
200Comments can be embedded in the chat script.
201A comment is a line which
202starts with the # (hash) character in column 1.
203Such comment
204lines are just ignored by the chat program.
205If a '#' character is to
206be expected as the first character of the expect sequence, you should
207quote the expect string.
208If you want to wait for a prompt that starts with a # (hash)
209character, you would have to write something like this:
210.Bd -literal -offset indent
211# Now wait for the prompt and send logout string
212\&'# ' logout
213.Ed
214.Sh ABORT STRINGS
215Many modems will report the status of the call as a string.
216These strings may be
217.Dv CONNECTED
218or
219.Dv NO CARRIER
220or
221.Dv BUSY .
222It is often desirable to terminate the script should the modem fail to
223connect to the remote.
224The difficulty is that a script would not know
225exactly which modem string it may receive.
226On one attempt, it may receive
227.Dv BUSY
228while the next time it may receive
229.Dv NO CARRIER .
230.Pp
231These "abort" strings may be specified in the script using the ABORT
232sequence.
233It is written in the script as in the following example:
234.Pp
235.D1 ABORT BUSY ABORT 'NO CARRIER' '' ATZ OK ATDT5551212 CONNECT
236.Pp
237This sequence will expect nothing; and then send the string ATZ.
238The expected response to this is the string
239.Dv OK .
240When it receives
241.Dv OK ,
242the string ATDT5551212 to dial the telephone.
243The expected string is
244.Dv CONNECT .
245If the string
246.Dv CONNECT
247is received the remainder of the
248script is executed.
249However, should the modem find a busy telephone, it will
250send the string
251.Dv BUSY .
252This will cause the string to match the abort
253character sequence.
254The script will then fail because it found a match to
255the abort string.
256If it received the string
257.Dv NO CARRIER ,
258it will abort
259for the same reason.
260Either string may be received.
261Either string will
262terminate the
263.Nm
264script.
265.Sh CLR_ABORT STRINGS
266This sequence allows for clearing previously set
267.Dv ABORT
268strings.
269.Dv ABORT
270strings are kept in an array of a pre-determined size (at
271compilation time); CLR_ABORT will reclaim the space for cleared
272entries so that new strings can use that space.
273.Sh SAY STRINGS
274The
275.Dv SAY
276directive allows the script to send strings to the user
277at the terminal via standard error.
278If
279.Nm
280is being run by
281pppd, and pppd is running as a daemon (detached from its controlling
282terminal), standard error will normally be redirected to the file
283.Pa /etc/ppp/connect-errors .
284.Pp
285.Dv SAY
286strings must be enclosed in single or double quotes.
287If carriage return and line feed are needed in the string to be output,
288you must explicitly add them to your string.
289.Pp
290The
291.Dv SAY
292strings could be used to give progress messages in sections of
293the script where you want to have 'ECHO OFF' but still let the user
294know what is happening.  An 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