Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Telnet router login with applescript

I have to realize a user-based firewall with a draytek vigor 2850 router. Therefore every user has to login at the router during startup with a individual telnet parameter sequence. With the Terminal application the following procedure works fine.


ADCT-iMac:~ Lucky$ telnet 192.168.3.1

Trying 192.168.3.1...

Connected to vigor.router.

Escape character is '^]'.


Account:admin

Password: *********


User login successful, expired time is "Unlimited".

Type ? for command help


> exit

Connection closed by foreign host.

ADCT-iMac:~ Lucky$


I tried the following AppleScript routine with does the job in the background without the Terminal application


set telnet to "telnet 192.168.3.1"

set account to "admin"

set passwd to "xxxxxx"

set resume to "exit"


do shell script telnet

delay 1

do shell script account

delay 1

do shell script passwd

delay 1

do shell script resume

delay 1


Unfortunately it interrupts with the message error "Connection closed by foreign host." number 1 😕

How do I get rid of this error ? Or has anyone a better idea to solve the task ?


Thanks in advance for any useful advice.

Alexander

iMac, Mac OS X (10.6.8), 8GB RAM 27 Display

Posted on May 23, 2012 12:45 PM

Reply
3 replies

May 23, 2012 1:15 PM in response to alucky

do shell script telnet

delay 1

do shell script account

delay 1

do shell script passwd

delay 1

do shell script resume

delay 1



This isn't going to work. Each 'do shell script' runs in its own distinct environment - think of it as opening separate terminal windows, just without the window. What happens here is that you start one shell command with your telnet command. You pause, and then start a whole new process, completely disconnected from the first in which you type your username, followed by another pause and a whole new process, completely disconnected from the first two... and so on.


To do what you want you have two options. Either change your script to use Terminal.app (and target all your commands to the same window), or combine your typing into a single shell command, typically using expect to wait for the prompts and 'type' the appropriate text.


Here's an example using Terminal.app:


set telnet to "telnet 192.168.3.1"

set account to "admin"

set passwd to "xxxxxx"

set resume to "exit"


tell application "Terminal"

set myWin to do scripttelnet


delay 1


do scriptaccountinmyWin


delay 1


do scriptpasswdinmyWin


delay 1


do scriptresumeinmyWin

end tell


An expect-based script would be significantly more complex, although for this setup you might be able to get away with a simple nc:


set cmd to "/usr/bin/nc 192.168.3.1 23 « EOF\\

\\

admin\\

*********\\

exit\\

EOF"

do shell scriptcmd

May 26, 2012 11:17 PM in response to Camelot

Thanks for your help.


Your example using Terminal.app works fine. But the display of the Terminal window is disturbing.

So I want to use the expect-based script with netcat, which does work on my system.

Sorry, I am not so familiar with entering script information in raw format.


I just copied and individualized your example to the AppleScript-Editor

set cmd to "/usr/bin/nc 192.168.3.1 23 « EOF\\

\\

admin\\

lexiadct56\\

exit\\

EOF"

do shell scriptcmd


Maybe my german keyboard is the reason ? How to type in the raw format ?


Thanks in advance for further help.

May 26, 2012 11:55 PM in response to alucky

I'm not sure what you're asking for here.


The script you have, as written, will attempt to connect to the telnet port of the device at 192.168.3.1. Once connected it will send the text as written - a blank return, the word 'admin' and return, what I assume is your password and a return then the word 'exit' and return.


You might need to adjust this to your specific needs - for example you didn't discuss logging out of the firewall when you're done, or you might not need the first blank return - so you might need to experiment a little.


The other disadvantage of using this simple nc script is one of timing - nc will basically try to type as quickly as it can, so if there's a delay before the login prompt appears, or if there's a delay between the username and password prompts, then this might be difficult to achieve via nc, which is where expect comes in. Scripting expect is a task not well suited to these discussion boards, though, since there are too many variables to consider.

Telnet router login with applescript

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.