SHARE
TWEET

cnlogin

fuhbahr Aug 24th, 2015 28 Never
  1. #! /usr/bin/expect --
  2. ##
  3. ## $Id: mrvlogin.in 2254 2010-10-01 19:27:48Z heas $
  4. ##
  5. ## rancid 2.3.6
  6. ## Copyright (c) 1997-2009 by Terrapin Communications, Inc.
  7. ## All rights reserved.
  8. ##
  9. ## This code is derived from software contributed to and maintained by
  10. ## Terrapin Communications, Inc. by Henry Kilmer, John Heasley, Andrew Partan,
  11. ## Pete Whiting, Austin Schutz, and Andrew Fort.
  12. ##
  13. ## Redistribution and use in source and binary forms, with or without
  14. ## modification, are permitted provided that the following conditions
  15. ## are met:
  16. ## 1. Redistributions of source code must retain the above copyright
  17. ##    notice, this list of conditions and the following disclaimer.
  18. ## 2. Redistributions in binary form must reproduce the above copyright
  19. ##    notice, this list of conditions and the following disclaimer in the
  20. ##    documentation and/or other materials provided with the distribution.
  21. ## 3. All advertising materials mentioning features or use of this software
  22. ##    must display the following acknowledgement:
  23. ##        This product includes software developed by Terrapin Communications,
  24. ##        Inc. and its contributors for RANCID.
  25. ## 4. Neither the name of Terrapin Communications, Inc. nor the names of its
  26. ##    contributors may be used to endorse or promote products derived from
  27. ##    this software without specific prior written permission.
  28. ## 5. It is requested that non-binding fixes and modifications be contributed
  29. ##    back to Terrapin Communications, Inc.
  30. ##
  31. ## THIS SOFTWARE IS PROVIDED BY Terrapin Communications, INC. AND CONTRIBUTORS
  32. ## ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  33. ## TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34. ## PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COMPANY OR CONTRIBUTORS
  35. ## BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  36. ## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  37. ## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  38. ## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  39. ## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. ## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. ## POSSIBILITY OF SUCH DAMAGE.
  42. #
  43. #  The expect login scripts were based on Erik Sherk's gwtn, by permission.
  44. #
  45. # clogin - Cisco login
  46. #
  47. # Most options are intuitive for logging into a Cisco router.
  48. # The default is to enable (thus -noenable).  Some folks have
  49. # setup tacacs to have a user login at priv-lvl = 15 (enabled)
  50. # so the -autoenable flag was added for this case (don't go through
  51. # the process of enabling and the prompt will be the "#" prompt.
  52. # The default username password is the same as the vty password.
  53. #
  54.  
  55. # Usage line
  56. set usage "Usage: $argv0 \[-dSV\] \[-autoenable\] \[-noenable\] \[-c command\] \
  57. \[-Evar=x\] \[-e enable-password\] \[-f cloginrc-file\] \[-p user-password\] \
  58. \[-s script-file\] \[-t timeout\] \[-u username\] \
  59. \[-v vty-password\] \[-w enable-username\] \[-x command-file\] \
  60. \[-y ssh_cypher_type\] router \[router...\]\n"
  61.  
  62. # env(CLOGIN) may contain:
  63. #       x == do not set xterm banner or name
  64.  
  65. # Password file
  66. set password_file $env(HOME)/.cloginrc
  67. # Default is to login to the router
  68. set do_command 0
  69. set do_script 0
  70. # The default is to automatically enable
  71. set avenable 1
  72. # The default is that you login non-enabled (tacacs can have you login already
  73. # enabled)
  74. set avautoenable 0
  75. # The default is to look in the password file to find the passwords.  This
  76. # tracks if we receive them on the command line.
  77. set do_passwd 1
  78. set do_enapasswd 1
  79. # Save config, if prompted
  80. set do_saveconfig 0
  81. # Sometimes routers take awhile to answer (the default is 10 sec)
  82. set timeoutdflt 45
  83. #
  84. set send_human {.4 .4 .7 .3 5}
  85.  
  86. # Find the user in the ENV, or use the unix userid.
  87. if {[ info exists env(CISCO_USER) ]} {
  88.     set default_user $env(CISCO_USER)
  89. } elseif {[ info exists env(USER) ]} {
  90.     set default_user $env(USER)
  91. } elseif {[ info exists env(LOGNAME) ]} {
  92.     set default_user $env(LOGNAME)
  93. } else {
  94.     # This uses "id" which I think is portable.  At least it has existed
  95.     # (without options) on all machines/OSes I've been on recently -
  96.     # unlike whoami or id -nu.
  97.     if [ catch {exec id} reason ] {
  98.         send_error "\nError: could not exec id: $reason\n"
  99.         exit 1
  100.     }
  101.     regexp {\(([^)]*)} "$reason" junk default_user
  102. }
  103. if {[ info exists env(CLOGINRC) ]} {
  104.     set password_file $env(CLOGINRC)
  105. }
  106.  
  107. # Process the command line
  108. for {set i 0} {$i < $argc} {incr i} {
  109.     set arg [lindex $argv $i]
  110.  
  111.     switch  -glob -- $arg {
  112.         # Expect debug mode
  113.         -d* {
  114.             exp_internal 1
  115.         # Username
  116.         } -u* {
  117.             if {! [regexp .\[uU\](.+) $arg ignore user]} {
  118.                 incr i
  119.                 set username [ lindex $argv $i ]
  120.             }
  121.         # VTY Password
  122.         } -p* {
  123.             if {! [regexp .\[pP\](.+) $arg ignore userpasswd]} {
  124.                 incr i
  125.                 set userpasswd [ lindex $argv $i ]
  126.             }
  127.             set do_passwd 0
  128.         # ssh passphrase
  129.         } -r* {
  130.             # ignore -r
  131.         # VTY Password
  132.         } -v* {
  133.             if {! [regexp .\[vV\](.+) $arg ignore passwd]} {
  134.                 incr i
  135.                 set passwd [ lindex $argv $i ]
  136.             }
  137.             set do_passwd 0
  138.         # Version string
  139.         } -V* {
  140.             send_user "rancid 2.3.6\n"
  141.             exit 0
  142.         # Enable Username
  143.         } -w* {
  144.             if {! [regexp .\[wW\](.+) $arg ignore enauser]} {
  145.                 incr i
  146.                 set enausername [ lindex $argv $i ]
  147.             }
  148.         # Environment variable to pass to -s scripts
  149.         } -E* {
  150.             if {[regexp .\[E\](.+)=(.+) $arg ignore varname varvalue]} {
  151.                 set E$varname $varvalue
  152.             } else {
  153.                 send_user "\nError: invalid format for -E in $arg\n"
  154.                 exit 1
  155.             }
  156.         # Enable Password
  157.         } -e* {
  158.             if {! [regexp .\[e\](.+) $arg ignore enapasswd]} {
  159.                 incr i
  160.                 set enapasswd [ lindex $argv $i ]
  161.             }
  162.             set do_enapasswd 0
  163.         # Command to run.
  164.         } -c* {
  165.             if {! [regexp .\[cC\](.+) $arg ignore command]} {
  166.                 incr i
  167.                 set command [ lindex $argv $i ]
  168.             }
  169.             set do_command 1
  170.         # Expect script to run.
  171.         } -s* {
  172.             if {! [regexp .\[sS\](.+) $arg ignore sfile]} {
  173.                 incr i
  174.                 set sfile [ lindex $argv $i ]
  175.             }
  176.             if { ! [ file readable $sfile ] } {
  177.                 send_user "\nError: Can't read $sfile\n"
  178.                 exit 1
  179.             }
  180.             set do_script 1
  181.         # save config on exit
  182.         } -S* {
  183.             set do_saveconfig 1
  184.         # 'ssh -c' cypher type
  185.         } -y* {
  186.             if {! [regexp .\[eE\](.+) $arg ignore cypher]} {
  187.                 incr i
  188.                 set cypher [ lindex $argv $i ]
  189.             }
  190.         # alternate cloginrc file
  191.         } -f* {
  192.             if {! [regexp .\[fF\](.+) $arg ignore password_file]} {
  193.                 incr i
  194.                 set password_file [ lindex $argv $i ]
  195.             }
  196.         # Timeout
  197.         } -t* {
  198.             if {! [regexp .\[tT\](.+) $arg ignore timeout]} {
  199.                 incr i
  200.                 set timeoutdflt [ lindex $argv $i ]
  201.             }
  202.         # Command file
  203.         } -x* {
  204.             if {! [regexp .\[xX\](.+) $arg ignore cmd_file]} {
  205.                 incr i
  206.                 set cmd_file [ lindex $argv $i ]
  207.             }
  208.             if [ catch {set cmd_fd [open $cmd_file r]} reason ] {
  209.                 send_user "\nError: $reason\n"
  210.                 exit 1
  211.             }
  212.             set cmd_text [read $cmd_fd]
  213.             close $cmd_fd
  214.             set command [join [split $cmd_text \n] \;]
  215.             set do_command 1
  216.         # Do we enable?
  217.         } -noenable {
  218.             set avenable 0
  219.         # Does tacacs automatically enable us?
  220.         } -autoenable {
  221.             set avautoenable 1
  222.             set avenable 0
  223.         } -* {
  224.             send_user "\nError: Unknown argument! $arg\n"
  225.             send_user $usage
  226.             exit 1
  227.         } default {
  228.             break
  229.         }
  230.     }
  231. }
  232. # Process routers...no routers listed is an error.
  233. if { $i == $argc } {
  234.     send_user "\nError: $usage"
  235. }
  236.  
  237. # Only be quiet if we are running a script (it can log its output
  238. # on its own)
  239. if { $do_script } {
  240.     log_user 0
  241. } else {
  242.     log_user 1
  243. }
  244.  
  245. #
  246. # Done configuration/variable setting.  Now run with it...
  247. #
  248.  
  249. # Sets Xterm title if interactive...if its an xterm and the user cares
  250. proc label { host } {
  251.     global env
  252.     # if CLOGIN has an 'x' in it, don't set the xterm name/banner
  253.     if [info exists env(CLOGIN)] {
  254.         if {[string first "x" $env(CLOGIN)] != -1} { return }
  255.     }
  256.     # take host from ENV(TERM)
  257.     if [info exists env(TERM)] {
  258.         if [ regexp \^(xterm|vs) $env(TERM) ignore ] {
  259.             send_user "\033]1;[lindex [split $host "."] 0]\a"
  260.             send_user "\033]2;$host\a"
  261.         }
  262.     }
  263. }
  264.  
  265. # This is a helper function to make the password file easier to
  266. # maintain.  Using this the password file has the form:
  267. # add password sl*      pete cow
  268. # add password at*      steve
  269. # add password *        hanky-pie
  270. proc add {var args} { global int_$var ; lappend int_$var $args}
  271. proc include {args} {
  272.     global env
  273.     regsub -all "(^{|}$)" $args {} args
  274.     if { [ regexp "^/" $args ignore ] == 0 } {
  275.         set args $env(HOME)/$args
  276.     }
  277.     source_password_file $args
  278. }
  279.  
  280. proc find {var router} {
  281.     upvar int_$var list
  282.     if { [info exists list] } {
  283.         foreach line $list {
  284.             if { [string match [lindex $line 0] $router ] } {
  285.                 return [lrange $line 1 end]
  286.             }
  287.         }
  288.     }
  289.     return {}
  290. }
  291.  
  292. # Loads the password file.  Note that as this file is tcl, and that
  293. # it is sourced, the user better know what to put in there, as it
  294. # could install more than just password info...  I will assume however,
  295. # that a "bad guy" could just as easy put such code in the clogin
  296. # script, so I will leave .cloginrc as just an extention of that script
  297. proc source_password_file { password_file } {
  298.     global env
  299.     if { ! [file exists $password_file] } {
  300.         send_user "\nError: password file ($password_file) does not exist\n"
  301.         exit 1
  302.     }
  303.     file stat $password_file fileinfo
  304.     if { [expr ($fileinfo(mode) & 007)] != 0000 } {
  305.         send_user "\nError: $password_file must not be world readable/writable\n"
  306.         exit 1
  307.     }
  308.     if [ catch {source $password_file} reason ] {
  309.         send_user "\nError: $reason\n"
  310.         exit 1
  311.     }
  312. }
  313.  
  314. # Log into the router.
  315. # returns: 0 on success, 1 on failure
  316. proc login { router user userpswd passwd enapasswd cmethod cyphertype } {
  317.     global command spawn_id in_proc do_command do_script
  318.     global prompt u_prompt p_prompt e_prompt sshcmd
  319.     set in_proc 1
  320.     set uprompt_seen 0
  321.  
  322.     # try each of the connection methods in $cmethod until one is successful
  323.     set progs [llength $cmethod]
  324.     foreach prog [lrange $cmethod 0 end] {
  325.         incr progs -1
  326.         if [string match "telnet*" $prog] {
  327.             regexp {telnet(:([^[:space:]]+))*} $prog methcmd suffix port
  328.             if {"$port" == ""} {
  329.                 set retval [ catch {spawn telnet $router} reason ]
  330.             } else {
  331.                 set retval [ catch {spawn telnet $router $port} reason ]
  332.             }
  333.             if { $retval } {
  334.                 send_user "\nError: telnet failed: $reason\n"
  335.                 return 1
  336.             }
  337.         } elseif [string match "ssh*" $prog] {
  338.             regexp {ssh(:([^[:space:]]+))*} $prog methcmd suffix port
  339.             set cmd [join [lindex $sshcmd 0] " "]
  340.             if {"$port" != ""} {
  341.                 set cmd "$cmd -p $port"
  342.             }
  343.             set retval [ catch {eval spawn [split "$cmd -c $cyphertype -x -l $user $router" { }]} reason ]
  344.             if { $retval } {
  345.                 send_user "\nError: $sshcmd failed: $reason\n"
  346.                 return 1
  347.             }
  348.         } else {
  349.             send_user "\nError: unknown connection method: $prog\n"
  350.             return 1
  351.         }
  352.         sleep 0.3
  353.  
  354.         # This helps cleanup each expect clause.
  355.         expect_after {
  356.             timeout {
  357.                 send_user "\nError: TIMEOUT reached\n"
  358.                 catch {close}; catch {wait};
  359.                 if { $in_proc} {
  360.                     return 1
  361.                 } else {
  362.                     continue
  363.                 }
  364.             } eof {
  365.                 send_user "\nError: EOF received\n"
  366.                 catch {close}; catch {wait};
  367.                 if { $in_proc} {
  368.                     return 1
  369.                 } else {
  370.                     continue
  371.                 }
  372.             }
  373.         }
  374.  
  375.     # Here we get a little tricky.  There are several possibilities:
  376.     # the router can ask for a username and passwd and then
  377.     # talk to the TACACS server to authenticate you, or if the
  378.     # TACACS server is not working, then it will use the enable
  379.     # passwd.  Or, the router might not have TACACS turned on,
  380.     # then it will just send the passwd.
  381.     # if telnet fails with connection refused, try ssh
  382.     expect {
  383.         -re "(Connection refused|Secure connection \[^\n\r]+ refused)" {
  384.             catch {close}; catch {wait};
  385.             if !$progs {
  386.                 send_user "\nError: Connection Refused ($prog): $router\n"
  387.                 return 1
  388.             }
  389.         }
  390.         -re "(Connection closed by|Connection to \[^\n\r]+ closed)" {
  391.             catch {close}; catch {wait};
  392.             if !$progs {
  393.                 send_user "\nError: Connection closed ($prog): $router\n"
  394.                 return 1
  395.             }
  396.         }
  397.         eof { send_user "\nError: Couldn't login: $router\n"; wait; return 1 }
  398.         -nocase "unknown host\r" {
  399.             send_user "\nError: Unknown host $router\n";
  400.             catch {close}; catch {wait};
  401.             return 1
  402.         }
  403.         "Host is unreachable" {
  404.             send_user "\nError: Host Unreachable: $router\n";
  405.             catch {close}; catch {wait};
  406.             return 1
  407.         }
  408.         "No address associated with name" {
  409.             send_user "\nError: Unknown host $router\n";
  410.             catch {close}; catch {wait};
  411.             return 1
  412.         }
  413.         -re "(Host key not found |The authenticity of host .* be established).*\(yes\/no\)\?" {
  414.             send "yes\r"
  415.             send_user "\nHost $router added to the list of known hosts.\n"
  416.             exp_continue
  417.         }
  418.         -re "HOST IDENTIFICATION HAS CHANGED.* \(yes\/no\)\?" {
  419.             send "no\r"
  420.             send_user "\nError: The host key for $router has changed.  Update the SSH known_hosts file accordingly.\n"
  421.             catch {close}; catch {wait};
  422.             return 1
  423.         }
  424.         -re "Offending key for .* \(yes\/no\)\?" {
  425.             send "no\r"
  426.             send_user "\nError: host key mismatch for $router.  Update the SSH known_hosts file accordingly.\n"
  427.             catch {close}; catch {wait};
  428.             return 1
  429.         }
  430.         -re "(denied|Sorry)"    {
  431.                                   send_user "\nError: Check your passwd for $router\n"
  432.                                   catch {close}; catch {wait}; return 1
  433.                                 }
  434.         -re "Login (failed|incorrect)" {
  435.                                   send_user "\nError: Check your passwd for $router\n"
  436.                                   catch {close}; catch {wait}; return 1
  437.                                 }
  438.         -re "% (Bad passwords|Authentication failed)"   {
  439.                                   send_user "\nError: Check your passwd for $router\n"
  440.                                   catch {close}; catch {wait}; return 1
  441.                                 }
  442.         "Press any key to continue" {
  443.                                   # send_user "Pressing the ANY key\n"
  444.                                   send "\r"
  445.                                   exp_continue
  446.                                 }
  447.         -re "Enter Selection: " {
  448.                                   # Catalyst 1900s have some lame menu.  Enter
  449.                                   # K to reach a command-line.
  450.                                   send "K\r"
  451.                                   exp_continue
  452.                                 }
  453.         -re "Last login:"       {
  454.                                   exp_continue
  455.                                 }
  456.         -re "@\[^\r\n]+ $p_prompt"      {
  457.                                   # ssh pwd prompt
  458.                                   sleep 1
  459.                                   send -h -- "$userpswd\r"
  460.                                   exp_continue
  461.                                 }
  462.         -re "$u_prompt"         {
  463.                                   send -h -- "$user\r"
  464.                                   set uprompt_seen 1
  465.                                   exp_continue
  466.                                 }
  467.         -re "$p_prompt"         {
  468.                                   sleep 1
  469.                                   if {$uprompt_seen == 1} {
  470.                                         send -h -- "$userpswd\r"
  471.                                   } else {
  472.                                         send -h -- "$passwd\r"
  473.                                   }
  474.                                   exp_continue
  475.                                 }
  476.         -re "$prompt"           { break; }
  477.         "Login invalid"         {
  478.                                   send_user "\nError: Invalid login: $router\n";
  479.                                   catch {close}; catch {wait}; return 1
  480.                                 }
  481.         -re "\[^\r\n]\[\r\n]+"  { exp_continue }
  482.      }
  483.     }
  484.  
  485.     set in_proc 0
  486.     return 0
  487. }
  488.  
  489. # Enable
  490. proc do_enable { enauser enapasswd } {
  491.     global prompt in_proc
  492.     global u_prompt e_prompt
  493.     set in_proc 1
  494.  
  495.     send -h "enable\r"
  496.     expect {
  497.         -re "$u_prompt" { send -h -- "$enauser\r"; exp_continue}
  498.         -re "$e_prompt" { send -h -- "$enapasswd\r"; exp_continue}
  499.         "#"             { set prompt "#" }
  500.         -re "(denied|Sorry|Incorrect)"  {
  501.                           # % Access denied - from local auth and poss. others
  502.                           send_user "\nError: Check your Enable passwd\n";
  503.                           return 1
  504.                         }
  505.         "% Error in authentication" {
  506.                           send_user "\nError: Check your Enable passwd\n"
  507.                           return 1
  508.                         }
  509.         "% Bad passwords" {
  510.                           send_user "\nError: Check your Enable passwd\n"
  511.                           return 1
  512.                         }
  513.     }
  514.     # We set the prompt variable (above) so script files don't need
  515.     # to know what it is.
  516.     set in_proc 0
  517.     return 0
  518. }
  519.  
  520. # Run commands given on the command line.
  521. proc run_commands { prompt command } {
  522.     global in_proc
  523.     set in_proc 1
  524.  
  525.     # escape any parens in the prompt, such as "(enable)"
  526.     regsub -all {[)(]} $prompt {\\&} reprompt
  527.     # match cisco config mode prompts too, such as router(config-if)#,
  528.     # but catalyst does not change in this fashion.
  529.     regsub -all {^(.{1,11}).*([#>])$} $reprompt {\1([^#>\r\n]+)?\2(\\([^)\\r\\n]+\\))?} reprompt
  530.     expect {
  531.         -re $reprompt   {}
  532.         -re "\[\n\r]+"  { exp_continue }
  533.     }
  534.  
  535.     # this is the only way i see to get rid of more prompts in o/p..grrrrr
  536.     log_user 0
  537.  
  538.     set commands [split $command \;]
  539.     set num_commands [llength $commands]
  540.     # the pager can not be turned off on the PIX, so we have to look
  541.     # for the "More" prompt.  the extreme is equally obnoxious, with a
  542.     # global switch in the config.
  543.     # XXX I think the pager can be turned off and this is just an artifact of
  544.     #     clogin
  545.     for {set i 0} {$i < $num_commands} { incr i} {
  546.         send -h -- "[subst -nocommands [lindex $commands $i]]\r"
  547.         expect {
  548.             -re "\b+"                           { exp_continue }
  549.             -re "^\[^\n\r *]*$reprompt"         { send_user -- "$expect_out(buffer)"
  550.                                                 }
  551.             -re "^\[^\n\r]*$reprompt."          { send_user -- "$expect_out(buffer)"
  552.                                                   exp_continue }
  553.             -re "^\[more*\[\r\n]+"              { # specific match c1900 pager
  554.                                                   send " "
  555.                                                   exp_continue }
  556.             -re "\[\n\r]+"                      { send_user -- "$expect_out(buffer)"
  557.                                                   exp_continue }
  558.             -re "\[^\r\n]*Press <SPACE> to cont\[^\r\n]*"       {
  559.                                                   send " "
  560.                                                   # bloody ^[[2K after " "
  561.                                                   expect {
  562.                                                         -re "^\[^\r\n]*\r" {}
  563.                                                         }
  564.                                                   exp_continue
  565.                                                 }
  566.             -re "^ *---More---\[^\n\r]*"                {
  567.                                                   send " "
  568.                                                   exp_continue }
  569.             -re "^<-+ More -+>\[^\n\r]*"        {
  570.                                                   send_user -- "$expect_out(buffer)"
  571.                                                   send " "
  572.                                                   exp_continue }
  573.         }
  574.     }
  575.     log_user 1
  576.  
  577.     send -h "exit\r"
  578.     expect {
  579.         -re "^\[^\n\r *]*$reprompt"             {
  580.                                                   # the Cisco CE and Jnx ERX
  581.                                                   # return to non-enabled mode
  582.                                                   # on exit in enabled mode.
  583.                                                   send -h "exit\r"
  584.                                                   exp_continue;
  585.                                                 }
  586.         "Would you like to save them now"       { # Force10
  587.                                                   send "n\r"
  588.                                                   exp_continue
  589.                                                 }
  590.         "Configuration changes have occurred.*" { # Cisco CSS
  591.                                                   send "n\r"
  592.                                                   exp_continue
  593.                                                 }
  594.         "Do you wish to save your configuration changes" {
  595.                                                   send "n\r"
  596.                                                   exp_continue
  597.                                                 }
  598.         -re "\[\n\r]+"                          { exp_continue }
  599.         timeout                                 { catch {close}; catch {wait};
  600.                                                   return 0
  601.                                                 }
  602.         eof                                     { return 0 }
  603.     }
  604.     set in_proc 0
  605. }
  606.  
  607. #
  608. # For each router... (this is main loop)
  609. #
  610. source_password_file $password_file
  611. set in_proc 0
  612. set exitval 0
  613. foreach router [lrange $argv $i end] {
  614.     set router [string tolower $router]
  615.     send_user -- "$router\n"
  616.  
  617.     # device timeout
  618.     set timeout [find timeout $router]
  619.     if { [llength $timeout] == 0 } {
  620.         set timeout $timeoutdflt
  621.     }
  622.  
  623.     # Figure out the prompt.
  624.     # autoenable is off by default.  If we have it defined, it was done
  625.     # on the command line.  If it is not specifically set on the command
  626.     # line, check the password file.
  627.     if $avautoenable {
  628.         set autoenable 1
  629.         set enable 0
  630.         set prompt "#\[^\[:digit:]]"
  631.     } else {
  632.         set ae [find autoenable $router]
  633.         if { "$ae" == "1" } {
  634.             set autoenable 1
  635.             set enable 0
  636.             #set prompt "#\[^\[:digit:]]"
  637.             set prompt ">"
  638.         }
  639.         #else {
  640.         #    set autoenable 0
  641.         #    set enable $avenable
  642.         #    set prompt ">"
  643.         #}
  644.     }
  645.  
  646.     # look for noenable option in .cloginrc
  647.     if { [find noenable $router] == "1" } {
  648.         set enable 0
  649.     }
  650.  
  651.     # Figure out passwords
  652.     if { $do_passwd || $do_enapasswd } {
  653.       set pswd [find password $router]
  654.       if { [llength $pswd] == 0 } {
  655.         send_user -- "\nError: no password for $router in $password_file.\n"
  656.         continue
  657.       }
  658.       if { $enable && $do_enapasswd && $autoenable == 0 && [llength $pswd] < 2 } {
  659.         send_user -- "\nError: no enable password for $router in $password_file.\n"
  660.         continue
  661.       }
  662.       set passwd [join [lindex $pswd 0] ""]
  663.       set enapasswd [join [lindex $pswd 1] ""]
  664.     } else {
  665.         set passwd $userpasswd
  666.         set enapasswd $enapasswd
  667.     }
  668.  
  669.     # Figure out username
  670.     if {[info exists username]} {
  671.       # command line username
  672.       set ruser $username
  673.     } else {
  674.       set ruser [join [find user $router] ""]
  675.       if { "$ruser" == "" } { set ruser $default_user }
  676.     }
  677.  
  678.     # Figure out username's password (if different from the vty password)
  679.     if {[info exists userpasswd]} {
  680.       # command line username
  681.       set userpswd $userpasswd
  682.     } else {
  683.       set userpswd [join [find userpassword $router] ""]
  684.       if { "$userpswd" == "" } { set userpswd $passwd }
  685.     }
  686.  
  687.     # Figure out enable username
  688.     if {[info exists enausername]} {
  689.       # command line enausername
  690.       set enauser $enausername
  691.     } else {
  692.       set enauser [join [find enauser $router] ""]
  693.       if { "$enauser" == "" } { set enauser $ruser }
  694.     }
  695.  
  696.     # Figure out prompts
  697.     set u_prompt [find userprompt $router]
  698.     if { "$u_prompt" == "" } {
  699.         set u_prompt "(\[Uu]sername|Login|login|user name):"
  700.     } else {
  701.         set u_prompt [join [lindex $u_prompt 0] ""]
  702.     }
  703.     set p_prompt [find passprompt $router]
  704.     if { "$p_prompt" == "" } {
  705.         set p_prompt "(\[Pp]assword|passwd):"
  706.     } else {
  707.         set p_prompt [join [lindex $p_prompt 0] ""]
  708.     }
  709.     set e_prompt [find enableprompt $router]
  710.     if { "$e_prompt" == "" } {
  711.         set e_prompt "\[Pp]assword:"
  712.     } else {
  713.         set e_prompt [join [lindex $e_prompt 0] ""]
  714.     }
  715.  
  716.     # Figure out cypher type
  717.     if {[info exists cypher]} {
  718.         # command line cypher type
  719.         set cyphertype $cypher
  720.     } else {
  721.         set cyphertype [find cyphertype $router]
  722.         if { "$cyphertype" == "" } { set cyphertype "3des" }
  723.     }
  724.  
  725.     # Figure out connection method
  726.     set cmethod [find method $router]
  727.     if { "$cmethod" == "" } { set cmethod {{telnet} {ssh}} }
  728.  
  729.     # Figure out the SSH executable name
  730.     set sshcmd [find sshcmd $router]
  731.     if { "$sshcmd" == "" } { set sshcmd {ssh} }
  732.  
  733.     # Login to the router
  734.     if {[login $router $ruser $userpswd $passwd $enapasswd $cmethod $cyphertype]} {
  735.         incr exitval
  736.         # if login failed, move on to the next device
  737.         continue
  738.     }
  739.     if { $enable } {
  740.         if {[do_enable $enauser $enapasswd]} {
  741.             if { $do_command || $do_script } {
  742.                 incr exitval
  743.                 catch {close}; catch {wait};
  744.                 continue
  745.             }
  746.         }
  747.     }
  748.     # we are logged in, now figure out the full prompt
  749.     # jk 20111208
  750.     send -h "system shell set global-more off\r"
  751.     send -h "configuration save\r"
  752.     # end jk
  753.  
  754.     send "\r"
  755.     expect {
  756.         -re "\[\r\n]+"          { exp_continue; }
  757.         -re "^.+$prompt"        { set junk $expect_out(0,string);
  758.                                   regsub -all "\[\]\[]" $junk {\\&} prompt;
  759.                                 }
  760.     }
  761.  
  762.     if { $do_command } {
  763.         if {[run_commands $prompt $command]} {
  764.             incr exitval
  765.             continue
  766.         }
  767.     } elseif { $do_script } {
  768.         send -h "terminal length 0\r"
  769.         expect -re $prompt      {}
  770.         source $sfile
  771.         catch {close};
  772.     } else {
  773.         label $router
  774.         log_user 1
  775.         interact
  776.     }
  777.  
  778.     # End of for each router
  779.     catch {wait};
  780.     sleep 0.3
  781. }
  782. exit $exitval
RAW Paste Data
Top