Guest User

Untitled

a guest
Oct 8th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 2.00 KB | None | 0 0
  1. #!/usr/bin/env expect
  2.  
  3. set PROMPT "*\/Admin# "
  4.  
  5. proc connect {server login passw {type "telnet"}} {
  6.     if {$type == "telnet"} {
  7.         spawn telnet $server
  8.         expect "login: "
  9.         send "$login\r"
  10.         expect "Password: "
  11.         send "$passw\r"
  12.         expect "*\/Admin# "
  13.     } elseif {$type == "ssh"} {
  14.         spawn ssh $login\@$server
  15.         expect "Password: "
  16.         send "$passw\r"
  17.         expect "*\/Admin# "
  18.     } else {
  19.         puts "connection type is not supported"
  20.         return 1
  21.     }
  22.  
  23.     send "conf\r"
  24.     expect "*\/Admin\(config\)# "
  25.  
  26.     return 0
  27. }
  28.  
  29. proc append {line level} {
  30.     if {[string equal $line ""] == 1} {
  31.         return 0
  32.     }
  33.    
  34.     set line_len [string length $line]
  35.     set line [string trim $line]
  36.     set space_cnt [expr {$line_len - [string length $line]}]
  37.    
  38.     if {$space_cnt < $level} {
  39.         for {set i 0} {$i < [expr {$level - $space_cnt}]} {incr i 2} {
  40.             send "exit\r"
  41.             expect "*\/Admin\(config*\)# "
  42.             puts "exit"
  43.         }
  44.     }
  45.    
  46.     send "$line\r"
  47.     expect "*\/Admin\(config\)# "
  48.     return $space_cnt
  49.    
  50. }
  51.  
  52. proc main {argc argv} {
  53.     if {$argc < 4 || $argc > 5} {
  54.         puts "Incorrect parameter. USE: $argv0 [server] [login] [password] [path to config file] [connection type]"
  55.         return 1
  56.     }
  57.    
  58.     global server
  59.     set server  [lindex $argv 0]
  60.     set login   [lindex $argv 1]
  61.     set passw   [lindex $argv 2]
  62.     set path    [lindex $argv 3]
  63.    
  64.     if {$argc == 5} {
  65.         set type [lindex $argv 4]
  66.         connect $server $login $passw $type
  67.     } else {
  68.         connect $server $login $passw
  69.     }
  70.    
  71.     if {[file exists $path] != 1} {
  72.         puts "File \"$path\" not found"
  73.         return 1
  74.     }
  75.    
  76.     set fp [open $path r]
  77.     set prev_lvl 0
  78.    
  79.     while {1} {
  80.         if {[gets $fp line] == -1} break
  81.         set lvl [append $line $prev_lvl]
  82.         sleep 0.1
  83.         set prev_lvl $lvl
  84.     }
  85. }
  86.  
  87. main $argc $argv
Add Comment
Please, Sign In to add comment