Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 23 << EOF
:wireless ifconfig state=$1

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.


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
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… – quanta Oct 26 '11 at 10:06
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? – 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


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.