Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I figured out how to enable/disable my wireless connection with telnet, but I want to automate it.

I read telnet is not suitable for this, and one should use netcat instead.

This is what I have:

nc 192.168.1.254 23 << EOF
username
password
:wireless ifconfig state=$1
EOF

But when I run that I get something like:

????????Username : Administratorfietspomp:wireless i

Some searching turned up the -t and -i options, but neither help very much.

update

Expect it is:

#!/usr/bin/expect -f

set force_conservative 0  ;# set to 1 to force conservative mode even if
              ;# script wasn't run conservatively originally
if {$force_conservative} {
    set send_slow {1 .1}
    proc send {ignore arg} {
        sleep .1
        exp_send -s -- $arg
    }
}

if { [lindex $argv 0] == 1} {
    set status "enabled"
} else {
    set status "disabled"
}

set timeout -1
spawn telnet 192.168.1.254
match_max 100000
expect -exact "Username : "
send -- "username\r"
expect -exact "Password : "
send -- "password\r"
expect -exact "_{Administrator}=>"
send -- ":wireless ifconfig state=$status\r"
expect *
send -- "exit\r"
expect eof
share|improve this question
    
Can't help with the netcat script but this would be pretty trivial to do with expect. –  Commsbloke Oct 26 '11 at 10:01
    
serverfault.com/questions/92736/… –  quanta Oct 26 '11 at 10:06

1 Answer 1

up vote 0 down vote accepted

Use autoexpect. If you don't have that command already, install package expect.

Then just enter autoexpect and hit enter. Whatever you enter next will be saved as script.exp. When you are finished, hit Ctrl+d.

Finally you can run the automated version with expect ./script.exp. Of course you can rename the script.exp and move it wherever you want.

share|improve this answer
    
Any idea how to get autoexpect on Mac? DuckGo turned up nothing. expect is installed though, so I could try to write it by hand. –  Pepijn Oct 26 '11 at 10:20
    
Does this ancient link help you? discussions.apple.com/thread/418636?start=0&tstart=0 –  Janne Pikkarainen Oct 26 '11 at 10:23
    
No, some idiot redirected the page to his Linkedin profile, and I can't find the Tcl folder on my Mac. –  Pepijn Oct 26 '11 at 10:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.