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 Code:
wireless radio state = disabled Code:
#!/bin/bash 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. |
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. |
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 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" |
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 Code:
echo -e "<username>\r" 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. |