home *** CD-ROM | disk | FTP | other *** search
/ kermit.columbia.edu / kermit.columbia.edu.tar / kermit.columbia.edu / scripts / ckermit / gethpconfig.~1~ < prev    next >
Text File  |  2010-03-18  |  7KB  |  182 lines

  1. #!/p/kd/fdc/solaris9/wermit +
  2. #!/usr/bin/kermit +
  3. #
  4. # Script gethpconfig - Get and save configuration of HP switch.
  5. # Requires C-Kermit 9.0 or later to filter out escape sequences.
  6. #
  7. # Give this file execute permission and change the first line
  8. # to indicate the full path of the C-Kermit executable followed by " +".
  9. #
  10. # Arguments:
  11. #  1. Name of file containing list of devices
  12. #  2. Access password for switches
  13. #
  14. # Change the following command to indicate the directory where the
  15. # the device list file is and where the logs are to be created.
  16. #
  17. define directory ~/hatfield
  18.  
  19. ############################################################################
  20. #
  21. # Everything from here down should be site independent...
  22.  
  23. assign myname \fbasename(\v(cmdfile))   # Name of this file (without path)
  24.  
  25. define errquit {                        # Fatal error handler
  26.     if not def \%1 def \%1 Unspecified error
  27.     if open connection close connection
  28.     exit 1 "FATAL [\m(myname)] - \%1"
  29. }
  30. if llt "\v(version)" "900000" errquit "C-Kermit 9.0 or later required"
  31.  
  32. # To enable debug messages define an environment variable DEBUG; e.g.:
  33. # DEBUG=1 gethpconfig filename password
  34. #
  35. if def \$(DEBUG) set debug message on   # environment variable DEBUG defined.
  36.  
  37. message Checking network...
  38. check network
  39. if fail errquit "This version of Kermit does not support network connections"
  40.  
  41. message \m(myname) parameters: 0=\%0  1=\%1  2=\%2
  42. # 0=Script Name
  43. # 1=List of switches, or any CLI device category.
  44. # 2=Password.
  45.  
  46. define usage exit 1 "Usage: \m(myname) devicelistfile password"
  47. if not def \%1 usage
  48.  
  49. # Prompt for password if not given on command line
  50.  
  51. while not def \%2 {
  52.     askq /echo:* \%2 "Password: "
  53. }
  54. cd \m(directory)                        # Change to the right directory
  55. if fail errquit "\m(directory): \v(errstring)"
  56.  
  57. # Try to open the specified device list file.
  58. # If it can't be opened, quit right here.
  59. #
  60. assign infile \fcontents(\%1)           # Global copy of input file name
  61. assign passwd \fcontents(\%2)           # and password
  62.  
  63. fopen /read \%c \m(infile)        # Open the list of devices
  64. if fail errquit {Device list file \m(infile): \v(errstring)}
  65.  
  66. log cx ./GetSwitchConfig.cx.out         # Create connection log
  67.  
  68. set telopt start-tls refuse             # Do not use START_TLS option
  69. set telopt authentication refuse        # Do not use AUTH option
  70. set telopt encrypt refuse refuse        # Do not use ENCRYPT option
  71. if debug set telnet debug on        # Show telnet negotiations if debugging
  72.  
  73. set exit on-disconnect off              # Don't exit if connection broken
  74. set exit warning off                    # Don't give connection warning on exit
  75.  
  76. set session-log text                    # Filter escape sequences from log
  77. set input buffer-length 16384           # Increase input buffer size
  78.  
  79. # Display most recent record from the devicelist file (only when debugging)
  80. #
  81. define displayRecord {
  82.     echo ....................Device Name:  \m(deviceName)
  83.     echo ....................Device Brand: \m(deviceBrand)
  84.     echo ....................Device Make:  \m(deviceMake)
  85.     echo ....................Device IP:    \m(deviceIP);
  86. }
  87.  
  88. define n 0                              # Record (switch) counter
  89.  
  90. # Read a record from the devicelist file.
  91. # There are four lines in the file per device.
  92. #   1: Device name (appears in device's console prompt)
  93. #   2: Manufacturer (info only)
  94. #   3: Device Model
  95. #   4: IP address
  96. #   5: Blank line (end of record) or EOF: end of file
  97. #
  98. define readFileRecord {
  99.     if \f_eof(\%c) {
  100.         message \m(infile): EOF... \m(n) records processed.
  101.         fclose \%c
  102.         exit 0
  103.     }
  104.     fread /line \%c deviceName
  105.     if fail errquit {\m(infile): Bad Device Name}
  106.     fread /line \%c deviceBrand
  107.     if fail errquit {\m(infile): Bad Device Brand}
  108.     fread /line \%c deviceMake
  109.     if fail errquit {\m(infile): Bad Device Make}
  110.     fread /line \%c deviceIP
  111.     if fail errquit {\m(infile): Bad DeviceIP}
  112.     fread /line \%c blankLine
  113.     if fail errquit {\m(infile): Bad blankLine}
  114.     increment n
  115.     if debug displayRecord
  116. }
  117. # commSession does the actual communication and logging.
  118.  
  119. define commSession {
  120.     lineout   # Force a new line, even if switch flags password error.
  121.     input 5 Password:
  122.     if success {
  123.         message Sending... \m(passwd)
  124.         lineout \m(passwd)
  125.         input 4 "\m(deviceName)# "
  126.         if fail end 1 "Timeout [1]"
  127.         message Asking for no page...
  128.         lineout no page
  129.         message Asking for configs...
  130.         input 4 "\m(deviceName)# "
  131.         if fail end 1 "Timeout [2]"
  132.         message Setting session... \m(deviceName)
  133.         lineout show config
  134.         input 10 "Startup configuration:" # Wait for heading
  135.  
  136.         # Start logging now - this way we only see the 'show config' output
  137.  
  138.         assign logname ./output/\m(deviceName).pre_cfg
  139.         log session \m(logname) new
  140.         if fail errquit "Session log: \m(deviceName).pre_cfg: \v(errstring)"
  141.         input 20 "\m(deviceName)# "
  142.         if fail end 1 "Timeout [3]"
  143.         close session                   # Close session log now
  144.         lineout exit                    # exit from CLI
  145.         input 4 "\m(deviceName)> "
  146.         if fail end 1 "Timeout [4]"
  147.         lineout exit
  148.         input 4 "Do you want to log out [y/n]? "
  149.         if fail { hangup, end 0 }       # Don't worry if this fails
  150.         lineout y
  151.     }
  152.     # connect  <-- why???
  153. }
  154. while 1 {
  155.     readFileRecord                      # Read info record for switch
  156.     message "Connecting to host \m(deviceName) at \m(deviceIP)..."
  157.     set host \m(deviceIP) 23 /telnet    # Connect to switch console
  158.     if fail {                           # On failure try the next one
  159.         echo "WARNING - \v(lastcommand): Connection failed"
  160.         continue
  161.     }
  162.     commSession                         # Success - get the config
  163.     if fail {                           # On failure warn and try next
  164.         local newname
  165.         .newname := \freplace(\m(logname),pre_cf,CHECK_ME)
  166.         echo "WARNING: Session \m(deviceName) did not end normally."
  167.         xecho "Renaming \m(deviceName) to \m(newname)..."
  168.         if open session close session   # Close session log if still open.
  169.         rename \m(logname) \m(newname)    # Rename the log
  170.         if fail { echo " [FAILED]" } else { echo " [OK]" }
  171.     }
  172. }
  173. exit 0                    # 0 = success
  174.  
  175. # The following are for the EMACS text editor used to compose this script.
  176. # They are executed by EMACS but for the script itself they are comments.
  177.  
  178. # Local Variables:
  179. # comment-column:40
  180. # comment-start:"# "
  181. # End:
  182.