home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / gethpconfig < prev    next >
Text File  |  2020-01-01  |  7KB  |  185 lines

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