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

Hi Monk,

Please suggest me some code which could open a putty to connect remote server through telnet by using user name and password and then I can fire some commands to get the work done.

Regards,

Sanjay

  • Comment on how to connect to a remote server through telnet and fire couple of commands?

Replies are listed 'Best First'.
Re: how to connect to a remote server through telnet and fire couple of commands?
by Corion (Patriarch) on Jun 17, 2016 at 07:20 UTC

    Also note that if by "putty" you actually mean an ssh connection, I recommend Net::SSH2 or Net::SSH::Any for use on Windows.

    Net::Telnet has been mentioned already, but for automation, you can also use the included plink.exe for connections to other hosts.

    Really do talk to your network administrators about the availability of ssh access. Using preshared keys is much better than storing passwords and sending plaintext.

      Hi,

      Thanks for your reply. Through manually I can log on to a remote server through putty by using telnet. Then I executed some commands to get some work done. But I have executed following code, but getting the error as follows "timed-out waiting for command prompt at telnet.pl "line 10

      use Net::Telnet (); $t = new Net::Telnet (Timeout => 10, Prompt => '/bash\$ $/'); $host="10.10.10.10"; $username="xyz"; $passwd="xyz1"; $t->open($host); $t->login($username, $passwd); @lines = $t->cmd("who"); print @lines;

      The remote server in my case is only supported telnet, because if I select SSH option in putty, I can't log on into the server.

      To perform those tasks automatically I need some Perl code, as I am working in a automation project.

      Regards,

      Sanjay

        As the documentation notes:

        The typical usage bug causes a time-out error because you've made incorrect assumptions about what the remote side actually sends. The easiest way to reconcile what the remote side sends with your expectations is to use "input_log()" or "dump_log()".

        Net::Telnet knows it has logged in when it sees the prompt you've told it to expect. The default *Prompt* is '/\$%#> $/'. That may work better than the bash$ in your example. Note that the value for prompt is treated as a regex, not a literal string.

        But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

        What is the command prompt when you logon manually ?

        poj
Re: how to connect to a remote server through telnet and fire couple of commands?
by marto (Cardinal) on Jun 17, 2016 at 06:48 UTC

    Automating putty is overkill if all you want to do is connect via telnet. The module Net::Telnet allows you do to this from perl. The docuemntaiton has some example code you can use to get you started. I'm sure you know this already, but telnet -> security

Re: how to connect to a remote server through telnet and fire couple of commands?
by Anonymous Monk on Jun 17, 2016 at 07:06 UTC