home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-bin / lib / dejagnu / remote.exp < prev    next >
Encoding:
Text File  |  1996-10-12  |  9.9 KB  |  466 lines

  1. #   Copyright (C) 1988, 1990, 1991, 1992 Free Software Foundation, Inc.
  2.  
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. # This program is distributed in the hope that it will be useful,
  8. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. # GNU General Public License for more details.
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program; if not, write to the Free Software
  13. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  14.  
  15. # Please email any bugs, comments, and/or additions to this file to:
  16. # bug-dejagnu@prep.ai.mit.edu
  17.  
  18. # This file was written by Rob Savoye. (rob@cygnus.com)
  19.  
  20. # these just need to be initialized
  21. set shell_id       0
  22.  
  23.  
  24. # Most of these procedures try to establish the connection 3 times before
  25. # returning. If $verbose is set to a value of 2 or greater, then error
  26. # messages will appear for each attempt. If there is an error that
  27. # can't be recovered from, it returns a -1. If the connection is
  28. # established, it returns the shell's process number returned by the
  29. # tcl command spawn.
  30. #   Hostname refers to the entry in /etc/hosts for this target. The
  31. # procedures's name is the same as it's unix counterpart.
  32.  
  33. #
  34. # telnet -- connect using telnet
  35. #
  36. proc telnet { args } {
  37.     global verbose
  38.     global connectmode
  39.     global shell_prompt
  40.     global spawn_id
  41.  
  42.     set hostname [lindex $args 0]
  43.     if [llength $args]>1 then {
  44.         set port [lindex $args 1]
  45.     } else {
  46.         set port 23
  47.     }
  48.     set retries 0
  49.     set result 0
  50.     spawn telnet $hostname $port
  51.     expect {
  52.     "$shell_prompt"    { 
  53.         verbose "Got prompt\n"
  54.         set result 0
  55.     }
  56.     -re "telnet: connect: Connection refused.*$" {
  57.         error "Connection refused."
  58.         set result -1
  59.         incr retries
  60.         if $retries<=2 then {
  61.         exp_continue
  62.         }
  63.     }
  64.     -re "Sorry, this system is engaged.*Connection closed by foreign host.*$" {
  65.         warning "Already connected."
  66.         set result -1
  67.         incr retries
  68.         if $retries<=2 then {
  69.         exp_continue
  70.         }
  71.     }
  72.     timeout            { 
  73.         warning "Timed out trying to connect."
  74.         set result -1
  75.         incr retries
  76.         if $retries<=2 then {
  77.         exp_continue
  78.         }
  79.     }
  80.     }
  81.  
  82.     if $result<0 then {
  83.     error "Couldn't connect after $retries retries."
  84.     return -1
  85.     } else {
  86.     set connectmode "telnet"
  87.     return $spawn_id
  88. }
  89. }
  90.  
  91. #
  92. # rlogin -- connect to using rlogin
  93. #
  94. proc rlogin { hostname } {
  95.     global verbose
  96.     global connectmode
  97.     global shell_prompt
  98.     global spawn_id
  99.  
  100.     set retries 0
  101.     set result -1
  102.     spawn rlogin $hostname
  103.     expect {
  104.     "$shell_prompt"    { 
  105.         verbose "Got prompt"
  106.         set result 0
  107.     }
  108.     -re "Sorry, shell is locked.*Connection closed.*$" {
  109.         warning "Already connected."
  110.         set result -1
  111.         incr retries
  112.         if $retries<=2 then {
  113.         exp_continue
  114.         }    
  115.     }
  116.     -re "Sorry, this system is engaged.*Connection closed.*$" {
  117.         warning "System engaged."
  118.         set result -1
  119.         incr retries
  120.         if $retries<=2 then {
  121.         exp_continue
  122.         }    
  123.     }
  124.     timeout            { 
  125.         error "Timed out trying to connect."
  126.         set result -1
  127.         incr retries
  128.         if $retries<=2 then {
  129.         exp_continue
  130.         }
  131.     }
  132.     }
  133.  
  134.     if $result<0 then {
  135.     error "Couldn't connect after $retries retries."
  136.     return -1
  137.     } else {
  138.     set connectmode "rlogin"
  139.     return $spawn_id
  140.     }
  141. }
  142.  
  143. #
  144. # rsh -- this procedure connects to using rsh
  145. #
  146. proc rsh { hostname } {
  147.     global verbose
  148.     global connectmode
  149.     global shell_prompt
  150.     global spawn_id
  151.  
  152.     set retries 0
  153.     set result -1
  154.     spawn rsh $hostname
  155.     expect {
  156.     "$shell_prompt"    { 
  157.         if $verbose>1 then {
  158.         send_user "Got prompt\n"
  159.         }
  160.         set result 0
  161.     }
  162.     -re "Sorry, shell is locked.*Connection closed.*$" {
  163.         warning "Already connected."
  164.         set result -1
  165.         incr retries
  166.         if $retries<=2 then {
  167.         exp_continue
  168.         }
  169.     }
  170.     timeout            {        
  171.         error "Timed out trying to connect."
  172.         set result -1
  173.         incr retries
  174.         if $retries<=2 then {
  175.         exp_continue
  176.         }
  177.     }
  178.     }
  179.  
  180.     if $result<0 then {
  181.     error "Couldn't connect after $retries retries."
  182.     return -1
  183.     } else {
  184.     set connectmode "rsh"
  185.     return $spawn_id
  186.     }
  187. }
  188.  
  189. #
  190. # tip -- connect to using tip
  191. #        port - must be a name from /etc/remote
  192. #        returns -1 if it failed, the spawn_id if it worked
  193. #
  194. proc tip { port } {
  195.     global verbose
  196.     global shell_prompt
  197.     global spawn_id
  198.  
  199.     set retries 0
  200.     set result -1
  201.  
  202.     spawn tip -v $port
  203.     expect {
  204.     -re ".*connected.*$" { 
  205.         send "\r\n"
  206.         expect {
  207.         -re ".*$shell_prompt.*$" {
  208.             if $verbose>1 then {
  209.             send_user "Got prompt\n"
  210.             }
  211.             set result 0
  212.             incr retries
  213.         }
  214.         timeout {
  215.             warning "Never got prompt."
  216.             set result -1
  217.             incr retries
  218.             if $retries<=2 then {
  219.             exp_continue
  220.             }
  221.         }
  222.         }
  223.     }
  224.     -re "all ports busy.*$" {
  225.         set result -1
  226.         error "All ports busy."
  227.         incr retries
  228.         if $retries<=2 then {
  229.         exp_continue
  230.         }        
  231.     }
  232.     -re "Connection Closed.*$" {
  233.         error "Never connected."
  234.         set result -1
  235.         incr retries
  236.         if $retries<=2 then {
  237.         exp_continue
  238.         }
  239.     }
  240.     -re ".*: Permission denied.*link down.*$" {
  241.         error "Link down."
  242.         set result -1
  243.         incr retries
  244.     }
  245.     timeout            {        
  246.         error "Timed out trying to connect."
  247.         set result -1
  248.         incr retries
  249.         if $retries<=2 then {
  250.         exp_continue
  251.         }
  252.     }
  253.     eof {
  254.         error "Got EOF from tip."
  255.         set result -1
  256.         incr retries
  257.     }
  258.     }
  259.  
  260.     send "\n~s"
  261.     expect {
  262.     "~\[set\]*" {
  263.         verbose "Setting verbose mode" 1
  264.         send "verbose\n\n\n"
  265.     }    
  266.     }
  267.     if $result<0 then {
  268.     error "Couldn't connect after $retries retries."
  269.     return -1
  270.     } else {
  271.     set connectmode "tip"
  272.     return $spawn_id
  273.     }
  274. }
  275.  
  276. #
  277. # tip_download -- downloads using the ~put command under tip
  278. #                 arg - is a full path name to the file to download
  279. #                 returns 1 if an error occured, otherwise it returns
  280. #                 the spawn_id.
  281. #
  282. proc tip_download { shell_id file } {
  283.     global verbose
  284.     global decimal
  285.     global shell_prompt
  286.     global expect_out
  287.  
  288.     set result 1
  289.     if ![file exists $file] then {
  290.     error "$file doesn't exist."
  291.     return 1
  292.     }
  293.  
  294.     send -i $shell_id "\n~p"
  295.     expect {
  296.     -i $shell_id "~\[put\]*" {
  297.         verbose "Downloading $file, please wait" 1
  298.         send -i $shell_id "$file\n"
  299.         set timeout 50
  300.         expect {
  301.         -i $shell_id -re ".*$file.*$" {
  302.             exp_continue
  303.         }
  304.         -i $shell_id -re ".*lines transferred in.*minute.*seconds.*$shell_prompt.*$" {
  305.             verbose "Download $file successfully" 1
  306.             set result 0
  307.         }
  308.         -i $shell_id -re ".*Invalid command.*$shell_prompt$" {
  309.             warning "Got an Invalid command to the monitor"
  310.         }
  311.         -i $shell_id -re ".*$decimal\r" {
  312.             if $verbose>1 then {
  313.             if [info exists expect_out(buffer)] then {
  314.                 send_user "$expect_out(buffer)\r"
  315.             }    
  316.             exp_continue
  317.             }
  318.         }
  319.         -i $shell_id timeout {
  320.             error "Timed out trying to download."
  321.             set result 1
  322.         }
  323.         }
  324.     }    
  325.     timeout {
  326.         error "Timed out waiting for response to put command."
  327.     }
  328.     }    
  329.     set timeout 10
  330.     return $result
  331. }
  332.  
  333. #
  334. # kermit -- connect to using kermit
  335. #        args - first is the device name, ie. /dev/ttyb
  336. #               second is the optional baud rate
  337. #        returns -1 if it failed, otherwise it returns
  338. #                 the spawn_id.
  339. #
  340. proc kermit { args } {
  341.     global verbose
  342.     global shell_prompt
  343.     global spawn_id
  344.  
  345.     if [llength $args]==1 then {
  346.     set baud 9600
  347.     } else {
  348.     set baud [lindex $args 1]
  349.     } 
  350.     set port [lindex $args 0]
  351.     set retries 0
  352.     set result -1
  353.     spawn kermit -l $port -b $baud
  354.     expect {
  355.     -re ".*ermit.*>.*$" { 
  356.         send "c\n"
  357.         expect {
  358.         -re ".*Connecting to $port.*Type the escape character followed by C to.*$" {
  359.             if $verbose>1 then {
  360.             send_user "Got prompt\n"
  361.             }
  362.             set result 0
  363.             incr retries
  364.         }
  365.         timeout {
  366.             warning "Never got prompt."
  367.             set result -1
  368.             incr retries
  369.             if $retries<=2 then {
  370.             exp_continue
  371.             }
  372.         }
  373.         }
  374.     }
  375.     -re "Connection Closed.*$" {
  376.         error "Never connected."
  377.         set result -1
  378.         incr retries
  379.         if $retries<=2 then {
  380.         exp_continue
  381.         }
  382.     }
  383.     timeout            {        
  384.         warning "Timed out trying to connect."
  385.         set result -1
  386.         incr retries
  387.         if $retries<=2 then {
  388.         exp_continue
  389.         }
  390.     }
  391.     }
  392.  
  393.     if $result<0 then {
  394.     error "Couldn't connect after $retries retries."
  395.     return -1
  396.     } else {
  397.     set connectmode "tip"
  398.     return $spawn_id
  399.     }
  400. }
  401.  
  402. #
  403. # exit_remote_shell -- exit the remote shell.
  404. #                   shell_id - This is the id number returned by the
  405. #                         any of the connection procedures procedure. 
  406. #
  407. proc exit_remote_shell { shell_id } {
  408.     global verbose
  409.  
  410.     if $shell_id<0 then {
  411.     return
  412.     }
  413.  
  414.     verbose "Exiting the remote shell $shell_id"
  415.     
  416.     catch "close -i $shell_id"
  417.     catch "wait -i $shell_id"
  418.     set shell_id 0
  419.     return 0
  420. }
  421.  
  422.  
  423. #
  424. # download -- download a file using stdin. This will download a file
  425. #             regardless of wether rlogin, telnet, tip, or kermit was
  426. #             used to establish the connection.
  427. #
  428. proc download { args } {
  429.     global spawn_id
  430.     global OBJCOPY
  431.     global verbose
  432.  
  433.     set file [lindex $args 0]
  434.  
  435.     if [llength $args]>1 then {
  436.         set shellid [lindex $args 1]
  437.     } else {
  438.         set shellid $spawn_id
  439.     }
  440.  
  441.     if [file exists $file] then {
  442.         exec $OBJCOPY -O srec $file $file.srec
  443.     } else {
  444.         error "$file does not exist."
  445.     }
  446.     set fd [open $file.srec r]
  447.     while { [gets $fd cur_line]>=0 } {
  448.         set errmess ""
  449.         catch "send -i $shellid \"$cur_line\"" errmess
  450.         if [string match "write\(spawn_id=\[0-9\]+\):" $errmess] then {
  451.             error "sent \"$command\" got expect error \"$errmess\""
  452.             catch "close"
  453.             return -1
  454.         }
  455.         if $verbose>=2 then {
  456.             send_user "."
  457.         }
  458.         verbose "Sent $cur_line" 3
  459.     }
  460.     close $fd
  461.     unset fd
  462.     return 0
  463. }
  464.