Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Telnet Issue

by jag (Initiate)
on Sep 03, 2009 at 16:48 UTC ( #793244=perlquestion: print w/replies, xml ) Need Help??

jag has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am new to perl programming and slowly starting my network programing. I want to connect to a device and fetch a who all logged in that device, for that i am not using Net::Telnet module to connect to the device however i am using normal telnet located in the /usr/bin/telnet to connect to the device.

Below is my program
#!/usr/bin/perl -w #use Net::Telnet; #use strict; print "Enter the hostname you want to connect? \n" ; $hostname = <>; $telnet = `telnet $hostname `; #$telnet = new Net::Telnet (Timeout=>10, Errmode=>'die'); #$telnet -> open('$hostname'); $telnet -> waitfor('/Username: $/i'); $telnet -> print('axiom'); $telnet -> waitfor('/Password: $/i'); $telnet -> print('axiom'); $telnet -> waitfor('/\$ $/i'); $telnet -> print('who'); $output = $telnet->waitfor('/\$ $/i'); print $output;
I am not sure what is wrong here in the code. Can anyone help me out on this regards.

Cheers,

Jag

Replies are listed 'Best First'.
Re: Telnet Issue
by ssandv (Hermit) on Sep 03, 2009 at 18:38 UTC

    Monks tend to be a lot like computers. The more precisely you follow the directions, the better the results you tend to get. The stuff below the posting box that says to use <code> tags isn't really just a suggestion--surely you looked at the preview and saw it was unreadable?

    In any event, since you didn't use Net::Telnet (perhaps you should explain why you're not using it), you can't then use the methods contained in Net::Telnet. What makes you think $telnet =  `telnet $hostname`; is anything like use Net::Telnet;$telnet = new Net::Telnet (...);? Backticks execute the string in a subshell and return the output. They don't give you a handle to the process. Note that you have a problem here because you're doing a blocking read on a telnet, which is in turn sitting there waiting for some input.

    The analogous set of steps to reproduce Net::Telnet's actions would be something like: Open a socket. Fork. Arrange the socket to allow 2-way communication between the two processes. Have the child process exec telnet. Have the parent print commands through the socket to the child and read the output back. Properly close the telnet connection and terminate the child.

    Now doesn't it sound easier to use Net::Telnet?

      Sorry sandy to bother you, from where can i download Net::Telnet module for perl version v5.8.4. Could you please let me know.
Re: Telnet Issue
by ramlight (Friar) on Sep 03, 2009 at 17:47 UTC
    Could you please repost your code surrounded with <code> tags?

    It looks like you are doing

    $telnet = `telnet $hostname`; $output = $telnet->waitfor('/\$ $/i');
    If I read your code correctly, $telnet is the data returned from the telnet command. But you are then using it as if it were a Net::Telnet object.
      I am reposting my code again as per your request.
      I have commented the Net::Telnet Object.
      #!/usr/bin/perl -w
      print "Enter the hostname you want to connect? \n" ;
      $hostname = <>;
      $telnet = `telnet $hostname `;
      $telnet -> waitfor('/Username: $/i');
      $telnet -> print('axiom');
      $telnet -> waitfor('/Password: $/i');
      $telnet -> print('axiom');
      $telnet -> waitfor('/\$ $/i');
      $telnet -> print('who');
      $output = $telnet->waitfor('/\$ $/i');
      print $output;
      Thanks for your response.
        As ssandy pointed out in greater detail, this is simply not going to do what you would like it to.
        $telnet = `telnet $hostname `; $telnet -> waitfor('/Username: $/i');
        If you want to script a telnet session, `telnet` isn't going to get you there; you're going to have to find another approach (such as Net::Telnet).

        Perhaps you might want to tell us more about why you decided NOT to use Net::Telnet. Perhaps someone here can help you get it to do what you need.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://793244]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2023-12-07 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (32 votes). Check out past polls.

    Notices?