Last active
May 24, 2017 12:54
-
-
Save analogic/6ff79e6bf9a0e605fd1ec0a53cef681b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/expect | |
;# | |
;# quick "Expect" language manual at https://gist.github.com/analogic/95d2972b61b81ada177f620f847f6b51 | |
;# | |
lassign $argv userhost password interface port json | |
if {$port eq ""} { | |
puts "Help: ./dslam.expect <user@host> <password> <interface> <port> \[json\]" | |
puts " " | |
puts "Example: ./test.expect root@172.16.2.85 psst \"vdsl 0/4\" 36 json" | |
exit | |
} | |
log_user 0 ;# turn off output | |
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no $userhost | |
expect ":" | |
send "$password\r" | |
expect { | |
">" { | |
} | |
"Press 'Q' to break" { | |
send "q\r" | |
expect ">" | |
} | |
} | |
send "enable\r" | |
expect "#" | |
send "config\r" | |
expect "#" | |
send "interface $interface\r" | |
expect "#" | |
send "deactivate $port\r" | |
expect "#" | |
send "vdsl selt $port\r" | |
expect ":" | |
send "\r" ;# sending enter to confirm | |
expect "#" | |
;# it should take some time | |
set timeout 120 | |
expect "#" ;# Port 0/4/36 SELT data is sampled completely, begin to calculate... | |
if {$json eq "json"} { | |
expect -re {^.*Line length : ([\d\.]+)} { set length $expect_out(1,string) } | |
expect -re {^.*Line termination : ([\w]+)} { set termination $expect_out(1,string) } | |
expect -re {^.*Line gauge : ([\d\.]+)} { set gauge $expect_out(1,string) } | |
expect -re {^.*0\s+(\d+)\s+(\d+)} { set snr0 "{\"snr\": 0, \"up\": $expect_out(1,string), \"down\": $expect_out(2,string)}" } | |
expect -re {^.*3\s+(\d+)\s+(\d+)} { set snr3 "{\"snr\": 3, \"up\": $expect_out(1,string), \"down\": $expect_out(2,string)}" } | |
expect -re {^.*6\s+(\d+)\s+(\d+)} { set snr6 "{\"snr\": 6, \"up\": $expect_out(1,string), \"down\": $expect_out(2,string)}" } | |
expect -re {^.*9\s+(\d+)\s+(\d+)} { set snr9 "{\"snr\": 9, \"up\": $expect_out(1,string), \"down\": $expect_out(2,string)}" } | |
expect -re {^.*12\s+(\d+)\s+(\d+)} { set snr12 "{\"snr\": 12, \"up\": $expect_out(1,string), \"down\": $expect_out(2,string)}" } | |
puts "{\"length\": $length, \"termination\": \"$termination\", \"gauge\": $gauge, \"snr\": \[$snr0, $snr3, $snr6, $snr9, $snr12\]}" | |
} else { | |
log_user 1 | |
expect "#" | |
puts " " | |
log_user 0 | |
} | |
send "activate $port\r" | |
expect ":" | |
send "\r" ;# sending enter to confirm | |
expect "#" | |
send "quit\r" | |
expect "#" | |
send "quit\r" | |
expect "#" | |
send "quit\r" | |
expect ":" | |
send "y\r" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment