Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need a config grabber for a linux machine

Status
Not open for further replies.

migueljr3

Technical User
Jun 22, 2001
23
US
I need to compile a script to grab configs once a week on F5 devices (load balancers) and tack it on to cron.

Help?
 
Here's a skeleton using expect(
Code:
#!path/to/expect
##globals
set env(TERM) vt100 
set timeout 300 
#log_user 0
#exp_internal 1
#log_file "mylogfile"
set appname [lindex $argv 0]
set target [lindex $argv 1]

array set commands {
1 "sh proc\r\n"
2 "sh mem\r\n"
3 "sh ip route\r\n"
4 "sh ip ospf database\r\n"
}

array set devinfo {}

array set prompts {
login "\[Ll\]ogin.*"
loginstring "username"
passwd ".*assw.*"
ppass  "mypasswd"
command ".*>.*"
}

proc dump_info {aname} {
global devinfo
  parray devinfo
return 0
}

proc do_target_action {list} {
global commands prompts devinfo expect_out timeout

set cnt 1
    foreach p $list {
       if {![eof $p]} {
           while {$cnt < [array size commands]} {
               send -i $p &quot;$commands($cnt)\r\n&quot;
               expect -i $p -re &quot;$prompts(command)&quot; {
                   set devinfo($p,$cnt) $expect_out(buffer)
                }
              incr cnt
            }
       } else {
          send_user &quot;spawn id $p is closed\n&quot;
       }
     }
return 0
}
                

set cnt 0
if {[llength $argv] == 2} {
   send_user &quot;Starting [info script] at [exec date], tclstamp = [clock seconds]\n&quot;
   spawn -noecho $appname $target
   lappend id $spawn_id
   

   expect {
 
       -re &quot;$login&quot; {
            send_user &quot;sending login string $prompts(loginstring)\n&quot;
            send &quot;$prompts(loginstring)\r&quot;
            exp_continue
        }

       -re &quot;$passwd&quot; {
            send_user &quot;sending passwd...\n&quot;
            send &quot;$prompts(ppass)\r&quot;
            exp_continue
         }

       -re &quot;$command&quot; {
            send_user &quot;got login &quot;
            do_target_action $id
            dump_info
            send_user &quot;All done..quitting\n&quot;         
            catch {close ; wait}
        }

        eof {
            send_user &quot;Caught abend for $spawn_id at
            [clock seconds]..exiting\n&quot;
            exit
        }

       timeout {
             send_user &quot;Could not connect with $target,
             using $appname...last read was
             $expect_out(buffer) tcl stamp = [clock
             seconds]\n&quot;
             exit
        }
              
} else {
  send_user &quot;Improper format for [info script]:
  ex: [info script] applicationname targethost\n&quot;
  exit
}
 
brilliant, will check out this code and let you know
how it goes.

Mig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top