Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env expect
- set PROMPT "*\/Admin# "
- proc connect {server login passw {type "telnet"}} {
- if {$type == "telnet"} {
- spawn telnet $server
- expect "login: "
- send "$login\r"
- expect "Password: "
- send "$passw\r"
- expect "*\/Admin# "
- } elseif {$type == "ssh"} {
- spawn ssh $login\@$server
- expect "Password: "
- send "$passw\r"
- expect "*\/Admin# "
- } else {
- puts "connection type is not supported"
- return 1
- }
- send "conf\r"
- expect "*\/Admin\(config\)# "
- return 0
- }
- proc append {line level} {
- if {[string equal $line ""] == 1} {
- return 0
- }
- set line_len [string length $line]
- set line [string trim $line]
- set space_cnt [expr {$line_len - [string length $line]}]
- if {$space_cnt < $level} {
- for {set i 0} {$i < [expr {$level - $space_cnt}]} {incr i 2} {
- send "exit\r"
- expect "*\/Admin\(config*\)# "
- puts "exit"
- }
- }
- send "$line\r"
- expect "*\/Admin\(config\)# "
- return $space_cnt
- }
- proc main {argc argv} {
- if {$argc < 4 || $argc > 5} {
- puts "Incorrect parameter. USE: $argv0 [server] [login] [password] [path to config file] [connection type]"
- return 1
- }
- global server
- set server [lindex $argv 0]
- set login [lindex $argv 1]
- set passw [lindex $argv 2]
- set path [lindex $argv 3]
- if {$argc == 5} {
- set type [lindex $argv 4]
- connect $server $login $passw $type
- } else {
- connect $server $login $passw
- }
- if {[file exists $path] != 1} {
- puts "File \"$path\" not found"
- return 1
- }
- set fp [open $path r]
- set prev_lvl 0
- while {1} {
- if {[gets $fp line] == -1} break
- set lvl [append $line $prev_lvl]
- sleep 0.1
- set prev_lvl $lvl
- }
- }
- main $argc $argv
Add Comment
Please, Sign In to add comment