LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem scripting a telnet session (https://www.linuxquestions.org/questions/linux-newbie-8/problem-scripting-a-telnet-session-4175467171/)

toothandnail 06-24-2013 09:59 AM

Problem scripting a telnet session
 
I have recently acquired a Technicolor 582n modem/router. Not a wonderful device, but it will have to do for a while....

It has a LED/switch which is supposed to indicate when wifi is active, and also allow wifi to be toggled on and off. Trouble is, the switch does nothing.

After a bi]t of research, I find I can switch wifi on and off by opening a telnet session and entering these commands:

Code:

wireless radio state = enabled
or

Code:

wireless radio state = disabled
It is a bit of a pain to go through the manual process of logging into the router, entering the relevant command and logging out again. So I looked for a few scripting examples and tried them. This is what I've come up with:

Code:

#!/bin/bash
(
open 192.168.1.254
sleep 2
echo "<username>"
sleep 2
echo "<password>"
sleep 2
echo "wireless radio state = disabled"
sleep 2
echo "exit"
) | telnet

All looks ok, but there is a problem - the commands being echoed are not followed by a newline. As a result, they all pile up on the username prompt and the script never completes the log in.

I guess I could install expect and use that, but it seems like huge overkill for what I'm trying to accomplish.

So, is there any way of making sure that the telnet server in the router sees a newline and goes on through the script? Or am I stuck using expect to complete the script?

Paul.

rgdacosta 06-24-2013 11:43 AM

Use echo with the -e option to use back escaped characters like \n for new line.


Code:

echo -e "username"\n

or you could make use of autoexpect to create a script for you with all of your commands in it.

David the H. 06-24-2013 12:19 PM

It's better to use a here document for things like this. A heredoc is a way to configure text input, with everything between the opening and closing strings being sent literally.

http://mywiki.wooledge.org/BashGuide...nd_Herestrings

Code:

#!/bin/bash

telnet <<-'COMMANDS'
        open 192.168.1.254
        <username>
        <password>
        wireless radio state = disabled
        exit
COMMANDS

The "<<-" form of the redirection lets you indent the text with initial tab characters (and only tabs), that won't be included in the actual output when run. The single quotes around the starting string keep any possible variable or command substitution strings from expanding. Leave them off if you actually need this.

Shell-syntax quote marks are not processed inside heredocs either, so leave them off, unless you need them to be literally passed to telnet.


The final issue is your interspersion of sleep commands between the telnet commands. This is one thing that can't be done with a heredoc (at least not directly), but I doubt that it's really necessary.

And even if it is, while I'm not familiar with telnet, I imagine that there's some way to tell it to delay actions itself, or at least to wait until one line successfully finishes before starting the next.


@rgdacosta: I would generally recommend using printf instead of echo -e. It's safer and more portable.

Code:

printf '%s\n\n' "username"

toothandnail 06-24-2013 03:17 PM

Thanks for the suggestions. The heredoc approach didn't work. Looks as though the telnet client in the router requires a carriage return rather than a newline, so maybe that was the reason heredoc didn't work.

With a few changes, I've managed to get either echo -e or printf to work. I now have the following (as a test - since I'm on the laptop, toggling wifi off doesn't seem to be a very good idea...):

Code:

#!/bin/bash
(
echo "open 192.168.1.254"
sleep 1
printf '%s\r' "<username>"
sleep 1
printf '%s\r' "<password>"
sleep 1
printf '%s\r' "xdsl info expand = enabled"
) | telnet

In order to get echo -e to work, I had to change things a bit as well - as below:

Code:

echo -e "<username>\r"
So it looks as though I can get my script to toggle the wifi on and off when needed :)

One other oddity - it seems that there is no need of the "exit" command at the end of the script. For reasons that I don't understand, if I finish the script as in the example above, it drops back to the bash command prompt when the command completes. Not the behaviour I get when I simply log in to the router from the command prompt.

Anyhow, thanks for the help. Always good to learn something new...

Paul.


All times are GMT -5. The time now is 12:35 PM.