LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-24-2013, 10:59 AM   #1
toothandnail
Member
 
Registered: Apr 2007
Location: Oxfordshire, UK
Distribution: Arch, Sparky, Salix64
Posts: 85

Rep: Reputation: 7
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.

Last edited by toothandnail; 06-24-2013 at 05:48 PM.
 
Old 06-24-2013, 12:43 PM   #2
rgdacosta
Member
 
Registered: Jun 2007
Location: South Africa
Distribution: Linux Mint,Fedora, openSUSE, RHEL, SLES, Scientific Linux
Posts: 70

Rep: Reputation: 25
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.
 
1 members found this post helpful.
Old 06-24-2013, 01:19 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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"
 
1 members found this post helpful.
Old 06-24-2013, 04:17 PM   #4
toothandnail
Member
 
Registered: Apr 2007
Location: Oxfordshire, UK
Distribution: Arch, Sparky, Salix64
Posts: 85

Original Poster
Rep: Reputation: 7
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Telnet Session ajkannan83 Linux - Security 4 10-23-2009 02:20 AM
Telnet session consumes 8KB which is never released after session is terminated NilesBor Linux - Kernel 1 04-24-2009 12:14 PM
Expect Scripting:- script not coming out of telnet session. nik1984 Programming 2 09-11-2008 09:14 AM
tcpdump of a telnet session Paul McLean Linux - Networking 2 11-26-2004 02:33 PM
See a telnet session log Linux - Newbie 5 01-13-2003 09:11 AM


All times are GMT -5. The time now is 11:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration