Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Telnet via Perl fails

by shamala (Acolyte)
on May 22, 2004 at 06:18 UTC ( #355555=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to telnet using pel script
#!/usr/bin/perl 2 print "enter the name the machine you want to telnet\n"; 3 chomp(my $mac=<STDIN>); 4 $addr=join "",$mac,".rest.of.the.address"; #or $addr=$mac . ".rest.of .the.address"; 5 print "$addr\n"; 6 `telnet $addr`; 7 print "\n"; 8 print "login name\n"; 9 print "password\n";

but this isnt working..well why not??.

  • I am trying telnet $addr (then a newline on the STDIN)
  • pushing my login name on the STDOUT by doing the print
  • and the my password
  • donno why this isnt making sense to the interpreter or if its my script thats not making sense?? any help coming this way??

    thanks in advance

    shamala

    20040523 Edit by Corion: Changed title from 'Why wouldnt this work??'

    Replies are listed 'Best First'.
    Re: Telnet via Perl fails
    by arthas (Hermit) on May 22, 2004 at 08:24 UTC

      Hi!

      You might want to try Net::Telnet. This module allows you to easily open and manage telnet sessions.

      Hope this helps!

      Michele.

    Re: Telnet via Perl fails
    by jepri (Parson) on May 22, 2004 at 06:46 UTC
      The program will halt until telnet finishes on line six, and returns a value.

      If telnet succeeds, it won't return. If telnet fails, you've lost anyway.

      perlipc goes into this in detail, prepare yourself for a big, complicated read.

      But what will work better for what you want is to open a socket directly to the telnet port, and read using select() calls , and sysread ans syswrite, IIRC.

      Networking is complicated, so this will take a while to figure out. Keep at it though, it's the most fun bit of computing (for me).

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.

        got some help on this...sharing it with you.

        my friend says we should try duping the descriptors so

        that the forked process containing the telnet command

        can be controlled by us...pass the rest of the commands

        starting from line7 to the duped descriptor which takes input from STDIN

        but me doesnt know how to work with descriptors

        will do some R&D on it today

        u could try it out too..;)

        and ofcourse let me know abt it

        thanks a lot

        shamala

          No, I won't do your work for you.

          And if you keep listening to your friend rather than reading the manual (perlipc), you'll waste a lot of time. But that's your problem, not mine.

          ____________________
          Jeremy
          I didn't believe in evil until I dated it.

    Re: Telnet via Perl fails
    by JoeJaz (Monk) on May 23, 2004 at 02:11 UTC
      I agree with the others regarding using the Net::telnet module if you can afford to. I know that the implementation may be a bit more complicated, but you have less of a chance of having unexpected things happen in your script. For your case you would need to do something like this
      use Net::Telnet; # initialize the telnet module my $username = 'user'; my $password = 'some_password'; my $command = 'ls -la'; print "enter the full domain name or IP of the machine that you want t +o telnet to\n"; chomp(my $host=<STDIN>); my prompt_regex = '/ .* ( \-?\@? \w*?\s? [\$#\%>~] \]? | \\\[\\e\[0m\\\] \[0m ) \s? /x'; $shell = new Net::Telnet ('Timeout'=>'7', 'Errmode'=> sub { report_error("login failure"); }, 'Prompt' => $prompt_regex ); $shell->open(Host=>$host); # opens the object $shell->login($username,$password); # login # then we issue some command with $shell->cmd("$command"); # or get the output of some command my $cmd_output = $shell->cmd("$command"); # and close the telnet object sleep 1; $shell->close; sub report_error { .... some code here to handle errors.... }
      To give due credit, the above code is from a project that a friend and I work on for fun known as the Temerity Project. Though we both contributed to this script, he refined its implementation and added that awesome regular expression to check for a user's prompt type.
      We originally tried using the expect.pm for interacting with telnet. This works too but is a lot more messy.
      I hope that this is at least a bit of a help. The Net::Telnet module is a very nice module should you decide to use it. Take care, Joe

        "I agree with the others regarding using the Net::telnet module if you can afford to."

        Afford? I don't understand what it 'costs' that someone whould not be able to afford it. Perhaps i'm just not grasping it in the since you mean, i just don't want the OP scared off by thinking it costs money to use.


        ___________
        Eric Hodges
        hey thanks Joe...really appreciate the help.

    Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (2)
    As of 2022-10-04 23:29 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My preferred way to holiday/vacation is:











      Results (20 votes). Check out past polls.

      Notices?