LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-15-2008, 10:40 AM   #1
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Rep: Reputation: 30
Net::Telnet with no prompt


Hi,

I am writing a Perl script which connects to a socket and sends some standard AT command. I created the script first using Socket::INET but soon realised that the socket wasnt a raw data socket but actually ran Telnet.

I therefore started using Net::Telnet but I am having some problems here. I think the problem is because the telnet session does not give me a prompt so Net::Telnet times out. I tried changing the Prompt option to /^$/, //, "", and anything I can think of but I still cant get it to work. Can someone provide me with a sample of how to get Net::Telnet to work without needing a prompt, just do it on a per line basis?

This is what a normal connection would look like.

Send: AT\n
Receive: OK\n
Send: AT+CGDCONT="IP","internet"\n
Receive: OK\n

Nothing complicated, just send one line, receive one, send another, receive another etc.

I think I need to set cmd_remove_mode to 1 as well as the device echoes back everything I send it.

In worst case I will have to go back to Socket::INET but I would perfer not to as I then have to ignore garbage I get back and ignore every second response which is my own echo etc.



Thanks in advance!

/ Fredrik
 
Old 09-15-2008, 10:58 AM   #2
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
Code:
#!/usr/bin/perl

use Net::Telnet ();

$t = new Net::Telnet (
        Timeout => 10,
        Port => '170',
        Cmd_remove_mode =>      '1'
);

$t->open("192.168.0.1");

$t->print("at");

$line = $t->getline(Errmode     => 'return', Timeout    =>      '2');
$line =~ 's/\n|\r//g';
if ($line =~ /OK/) { print "Got: $line\n"; }

$t->close;
I tried this code which works if I run Perl in the debugger but for some reason fails if I turn debugging of... Any idea why?

Last edited by Fredde87; 09-15-2008 at 11:23 AM.
 
Old 09-15-2008, 08:07 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
1. tell us exactly what the fail symptoms are. Show example.
2. use warnings and strict
 
Old 09-16-2008, 05:22 AM   #4
Fredde87
Member
 
Registered: Aug 2005
Posts: 158

Original Poster
Rep: Reputation: 30
1. The symptoms are as follows, if I use the above code, then $line will be empty if I dont use the debugger (-d). If I do and step through the same code $line contains OK and the if statement matches and I get "Line: OK" printed to the screen.

Any other of the ~10 examples of Net::Telnet I have found on google gives me a "command timed-out at line X". The one I posted above I set errmode to return so that it would not die when it fails but then I get the symptoms I described above.

2. I tried adding warnings as you suggested and I then get these errors,

Use of uninitialized value in pattern match (m//) at ./fb4 line 16.
Use of uninitialized value in pattern match (m//) at ./fb4 line 17.

Adding strict and declaring my variables still gives same result.


Thanks!
 
Old 09-16-2008, 08:33 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
According to this: http://search.cpan.org/~jrogers/Net-.../Net/Telnet.pm you should prob be using cmd() instead of print(). Also, looks like you're 'suffering from buffering', see this

Quote:
What To Know Before Using

* All output is flushed while all input is buffered. Each object contains its own input buffer.
...
http://perl.plover.com/FAQs/Buffering.html

Last edited by chrism01; 09-16-2008 at 08:34 PM.
 
  


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
change Telnet login prompt Joshsawyer77 Linux - Newbie 4 07-01-2006 05:50 AM
telnet delays before login prompt laxu Linux - General 2 07-02-2004 02:39 AM
Telnet Login Prompt Config MattOlz Linux - Networking 4 02-08-2004 09:14 AM
no login prompt in telnet bilal.shaikh Linux - Newbie 1 11-13-2003 01:02 PM
telnet connects but login prompt does not appear jhess Linux - Software 2 10-10-2003 07:05 AM


All times are GMT -5. The time now is 08:50 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration