chat.8 17 KB

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