home *** CD-ROM | disk | FTP | other *** search
/ ftp.mayn.de / ftp.mayn.de-pub.zip / ftp.mayn.de-pub / apple / apple_unix / Info / slip.help < prev   
Text File  |  2017-03-06  |  11KB  |  360 lines

  1. ==============================================================================
  2.  
  3. From: paul@hal.COM (Paul Sander)
  4. Subject: Initiating SL/IP connection
  5.  
  6. Here is a little Expect script that I use to initiate a SL/IP connection.
  7. It requires version 3.24 of the Expect program, which is somewhat dated
  8. (though it is available from the Gnu project).
  9.  
  10. It basically follows the procedure outlined in the admin doc set:  Use
  11. cu to dial a remote system, then escape out of cu and invoke slattconf.
  12. It also provides a means to determine if a connection is already active.
  13. It does not notice if a connection dies, because there are no reliable
  14. hooks for this.
  15.  
  16. It accepts on its command line a single parameter:  The path to a
  17. configuration file.  This file has the following format:
  18.  
  19. keyword value
  20.  
  21. The keyword is one of the following:
  22.  
  23. remote    - Name of server system, must be contained in Systems file
  24. login     - Login ID on server system
  25. password  - Password on server system
  26. localIP   - Client (local) IP address
  27. remoteIP  - Remove (server) IP address
  28.  
  29. Blank lines and lines where the first non-whitespace character is a # sign
  30. are ignored.  This file should be owned by the user who runs the script
  31. (preferably root), and its mode should be 600, but this is not enforced.
  32.  
  33. Exit value 0 indicates that slattconf was successfully invoked.  Exit
  34. value 3 indicates that the desired connection is already up.  Other
  35. exit values indicate serious failures.  Due to the implementation of
  36. slattconf, there is no way for this script to verify that a connection
  37. came up successfully without coding a race condition.  I decided that
  38. any wait loops or time-outs should be handled by other scripts that
  39. invoke this one.
  40.  
  41. The name of the server system must be correctly configured in the
  42. /usr/lib/uucp/Systems file, and cu must be properly configured for
  43. dialout operation.
  44.  
  45. It is best to disable getty on the port that is dialing out, because
  46. cu is killed after slattconf is invoked, to save a few resources.  There
  47. is no telling what getty might do with IP traffic, so it's best to play
  48. it safe.  This script should also be run by a user that is not likely
  49. to invoke cu for other purposes, because cu will contend with slattconf
  50. if they both want the same port.
  51.  
  52. I tried writing this in Perl at one time, but found that cu's habit of
  53. forking a process and closing file descriptors didn't work too well with
  54. the chat2.pl library.  Expect seems to tolerate it better.
  55.  
  56.  
  57.  
  58. #! /usr/local/bin/expect --
  59.  
  60. # Set paths to needed programs
  61. set cu /usr/bin/cu
  62. set slattconf /etc/slattconf
  63. set route /usr/etc/route
  64. set ifconfig /etc/ifconfig
  65.  
  66. # Maximum number of simultaneously supported SL/IP connections
  67. set maxslip 2
  68.  
  69. # Check path of configuration file
  70. if { [ llength argv ] < 2 } {
  71.     set conf [ lindex $argv 1 ]
  72. } else {
  73.     puts stdout "No configuration file given on command line"
  74.     exit 1
  75. }
  76.  
  77. # set parameters
  78. set patt "^\[     ]*(\[^     ]\[^     ]*)(\[     ]\[     ]*(.*))?\$"
  79. set fail 0
  80. set comment 0
  81. if { [ file readable $conf ] } {
  82.     set file [ open $conf "r" ]
  83.     gets $file line
  84.     while { ! [ eof $file ] } {
  85.         set key ""
  86.         set val ""
  87.         regexp $patt $line all key dummy val
  88.         case $key in {
  89.         ""        { }
  90.         #*        { set comment 1 }
  91.         remote        { set remote $val }
  92.         login        { set login $val }
  93.         password    { set password $val }
  94.         localIP        { set localIP $val }
  95.         remoteIP    { set remoteIP $val }
  96.         default        { puts stdout "Unknown keyword $key"
  97.                   set fail 1
  98.                 }
  99.         }
  100.         gets $file line
  101.     }
  102.     close $file
  103.     set patt "^\[0-9]\[0-9]*\.\[0-9]\[0-9]*\.\[0-9]\[0-9]*\.\[0-9]\[0-9]*$"
  104.     if { ! [ regexp $patt $localIP ] } {
  105.         puts stdout [ format "Local IP address (localIP %s) invalid" \
  106.                              $localIP ]
  107.         set fail 1
  108.     }
  109.     if { ! [ regexp $patt $remoteIP ] } {
  110.         puts stdout [ format "Remote IP address (remoteIP %s) invalid" \
  111.                              $remoteIP ]
  112.         set fail 1
  113.     }
  114. } else {
  115.     puts stdout [ format "Cannot read configuration file %s" $conf ]
  116.     set fail 1
  117. }
  118. if { $fail } {
  119.     puts stdout "Configuration file is invalid"
  120.     exit 2
  121. }
  122.  
  123. # Verify that the SL/IP connection is not already up.
  124. for { set conn 0 } { $conn < $maxslip } { incr conn } {
  125.     set interface [ format "sl%d" $conn ]
  126.     set status [ exec $ifconfig $interface ]
  127.     if { [ regexp $localIP\ -->\ $remoteIP $status ] } {
  128.         if { [ string first UP $status ] >= 0 } {
  129.             puts stdout "Connection is already up"
  130.             exit 3
  131.         }
  132.     }
  133. }
  134. puts stdout "Connection is not up"
  135.  
  136. # Kill the cu process.  The termination of cu appears to cause the line to
  137. # drop, which in turn sends a SIGHUP to slattconf.  But that's only theory;
  138. # slattconf vanishes, in any case.
  139. proc endcu { pid sid cur_id } {
  140.     if { $pid != "" } {
  141.         if { $sid != $cur_id } {
  142.             set spawn_id $sid
  143.         }
  144.         puts stdout [ concat Killing process $pid ]
  145.         exec kill -15 $pid
  146.         puts stdout [ concat Flushing process $pid ]
  147.         send "exit\r"
  148.         expect eof;
  149.         if { $sid != $cur_id } {
  150.             set spawn_id $cur_id
  151.         }
  152.         puts stdout [ concat Waiting on process $pid ]
  153.         wait -i $sid
  154.         puts stdout [ concat Process $pid is dead ]
  155.     }
  156. }
  157.  
  158. # Dial the remote system, wait for connection, or for UUCP to fail
  159. log_user 0
  160. puts stdout [ concat Dialing remote system $remote ]
  161. set pidcu [ spawn $cu -b7 -e -x5 $remote ]
  162. set sidcu $spawn_id
  163. trap { endcu $pidcu $sidcu $spawn_id; exit 4 } { 1 2 3 15 }
  164. puts stdout [ format "Pid of cu is %d" $pidcu ]
  165. set timeout -1
  166. set speed 0
  167. set dev ""
  168. expect {
  169.     "\n"                { continue -expect }
  170.     eof                { set result "Failed" }
  171.     -re "Connect failed: .*\n"    { set result "Failed" }
  172.     Connected            { set result "Connected" }
  173.     -re "opening serial line '(/dev/\[^'\]*)'" {
  174.                         set dev $expect_out(1,string)
  175.                         continue -expect
  176.                         }
  177.     -re "setting line for (\[0-9\]+) baud" {
  178.                         set speed $expect_out(1,string)
  179.                         continue -expect
  180.                          }
  181. }
  182. if { $speed == "" } {
  183.     puts stdout "cu did not report the speed"
  184.     set result "Failed"
  185. }
  186. if { $dev == "" } {
  187.     puts stdout "cu did not report the device"
  188.     set result "Failed"
  189. }
  190. if { $result != "Connected" } {
  191.     puts stdout "cu failed to connect"
  192.     endcu $pidcu $sidcu $spawn_id
  193.     exit 5
  194. }
  195. puts stdout "cu connected"
  196. puts stdout [ concat device is $dev ]
  197. puts stdout [ concat speed is $speed ]
  198.  
  199. # Loop up to three times to send login ID and password
  200. set timeout 5
  201. set attempts 1
  202. set result ""
  203. set cRem ""
  204. set cLoc ""
  205. while { $attempts <= 3 && $result != "Startup" } {
  206.     # Send up to 3 CR's at 5 second intervals until the login prompt
  207.     #appears
  208.     set cnt 0
  209.     expect {
  210.         eof        { set result "Failed" }
  211.         timeout        { incr cnt;
  212.                   if { $cnt <= 3 } {
  213.                     send "\r"
  214.                     continue -expect
  215.                   } else {
  216.                     set result "Timeout"
  217.                   }
  218.                 }
  219.         -re "ogin: *"    { set result "Login" }
  220.     }
  221.     if { $result != "Login" } {
  222.         endcu $pidcu $sidcu $spawn_id
  223.         puts stdout "cu failed to get login prompt"
  224.         exit 6
  225.     }
  226.  
  227.     # Send the login ID and wait for password prompt
  228.     set timeout 60
  229.     send $login
  230.     send "\r"
  231.     expect {
  232.         -re "assword: *"    { set result "Password" }
  233.         default            { set result "Failed" }
  234.     }
  235.     if { $result != "Password" } {
  236.         endcu $pidcu $sidcu $spawn_id
  237.         puts stdout "cu failed to get password prompt"
  238.         exit 7
  239.     }
  240.  
  241.     # Send the password and wait for SL/IP startup message
  242.     send $password
  243.     send "\r"
  244.     expect {
  245.         -re "from \\(?(\[0-9.\]+)\\)? "    { set cRem $expect_out(1,string)
  246.                           continue -expect
  247.                         }
  248.         -re "to \\(?(\[0-9.\]+)\\)? "    { set cLoc $expect_out(1,string)
  249.                           continue -expect
  250.                         }
  251.         "beginning...."            { set result "Startup" }
  252.         default                { set result "Failed" }
  253.     }
  254. }
  255. if { $result != "Startup" } {
  256.     endcu $pidcu $sidcu $spawn_id
  257.     puts stdout "cu failed to start SL/IP server"
  258.     exit 8
  259. }
  260.  
  261. # Verify that the server and client agree on their expected IP numbers.
  262. set fail 0
  263. if { $localIP != $cLoc } {
  264.     puts stdout [ format "Local %s and expected %s addresses differ" \
  265.                          $localIP $cLoc ]
  266.     set fail 1
  267. }
  268. if { $remoteIP != $cRem } {
  269.     puts stdout [ format "Remote %s and expected %s addresses differ" \
  270.                          $remoteIP $cRem ]
  271.     set fail 1
  272. }
  273. if { $fail != 0 } {
  274.     puts stdout [ concat SL/IP connection is not properly configured ]
  275.     endcu $pidcu $sidcu $spawn_id
  276.     exit 9
  277. }
  278. puts stdout "Logged in, SL/IP server started"
  279.  
  280. # Escape cu and start up slattconf.  cu isn't left running because we don't
  281. # want it reading data intended for SL/IP.
  282. send "~!\r"
  283. expect {
  284.     "created Shell process 0"    { set result "Escaped" }
  285.     default                { set result "Failed" }
  286. }
  287. if { $result != "Escaped" } {
  288.     puts stdout "cu failed to escape to shell"
  289.     endcu $pidcu $sidcu $spawn_id
  290.     exit 10
  291. }
  292. puts stdout "Starting up SL/IP client"
  293. set cmd [ list $slattconf +c $dev $speed $localIP $remoteIP -arp ]
  294. puts stdout $cmd
  295. eval [ system [ concat $cmd "2>&1" ">/dev/null" "&" ] ]
  296. puts stdout "SL/IP client is up"
  297. endcu $pidcu $sidcu $spawn_id
  298.  
  299. exit 0
  300.  
  301. -- 
  302. Paul M. Sander  (408) 379-7000  |  "If everything were easy, where would be
  303. HaL Computer Systems, Inc.      |   the challenge?"
  304. 1315 Dell Avenue                |
  305. Campbell, CA  95008  USA        |
  306.  
  307. ==============================================================================
  308.  
  309. From: eric@cse.ucsc.edu (Eric C. Rosen)
  310. Subject: Re: SL/IP question cont'd
  311.  
  312. To get SL/IP running, you need to do the following:
  313.  
  314. 1. Reconfigure your kernel to include the cslip package and reboot.
  315.  
  316. 2. Assuming your modem is on the modem port (tty0), edit /etc/inittab
  317.    and turn off the getty on tty0 unless you want to allow dialup 
  318.    access when SL/IP is not running. I recommend turning it off
  319.    initially.
  320.  
  321. 3. Use cu, tip, kermit or something equivalent to dialup your SL/IP
  322.    server and login to your SL/IP account. Without dropping DTR,
  323.    exit your communication program.
  324.  
  325. 4. Assuming your SL/IP server understands CSL/IP, the speed at
  326.    which you connected to your modem is 19200 baud, your modem is
  327.    on tty0 as in (2), your local host is called sapphire and your
  328.    SL/IP server is called slip-server, run the following command
  329.    as root:
  330.  
  331.    slattconf +c tty0 19200 sapphire slip-server -arp
  332.  
  333.    If your local host and the SL/IP server are not registered
  334.    in /etc/hosts, you'll have to use the IP addresses of the
  335.    machine(s) on the command line.
  336.  
  337. 5. You should now be connected. Use 'ping slip-server' to
  338.    confirm this.
  339.  
  340. 6. If you want to use a remote nameserver, edit /etc/resolv.conf
  341.    appropriately.
  342.  
  343. 7. If you want to talk to the world beyond slip-server, you'll
  344.    need to adjust your local routing tables. The simplest way to
  345.    do this that should work with most setups is to run the
  346.    following command:
  347.  
  348.    route add default slip-server 32
  349.  
  350.    Use the command 'netstat -r' to examine your routing table.
  351.  
  352. You will probably want to automate steps 3, 4, and 7. As you can
  353. tell, I've only given the general details of how to get SL/IP
  354. operating. This basic procedure is common to all UNIX SL/IP
  355. setups I've seen. If you need help with a specific task, post
  356. a followup. 
  357.  
  358. --Eric
  359.  
  360.