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

Hello gentlemen, Beginner to Perl, to scripts, to Linux... the whole package I guess. I got myself interested in scripts because I use a lot of repeated command lines on my server ( NETAPP server ) that I manually type everytime... I enjoyed Perl syntax and ... here I am. Using Perl on Ubuntu and tried to do something like a little program to let me use my commands simply and quickly by just using shortcuts with my keypad numbers I can send simple command lines to my server without any troubles... Problem comes when I need to respond to my server, for example : destroying a "volume" or an "aggregate", my server asks me if I'm sure of this decision with a simple "yes or no", I'm having troubles to "respond". Apologies in advance if I'm not clear enough or if I use wrong/bad terms... I'll try to do my best. So here is an extract of my script :
#!/usr/bin/env perl -w -s use Net::Telnet (); my $username = 'root'; my $password = 'passw0rd'; my $local = '10.10.4.46'; print "46 leads to => 10.10.4.46"; my $machine =<>; chomp $machine; while ($machine ne '46'){ print "\nBad entry :\n"; $machine = <>; chomp $machine; } if ($machine eq '46') { my $t = new Net::Telnet (Timeout => 10); $t->open("$local"); $t->login($username, $password); print "\n\n\n\n\t\t\tConnexion to 10.10.4.46... \n\n\n"; print "Enter to continue\n\n"; my $pass = <>; chomp $pass; while ($pass ne '99') print "004 = destroy volume\n"; my $pass = <>; chomp $pass; if($pass eq '004'){ print "\n\n\n Menu to destroy aggr or a volume\n"; my $value = <>; chomp $value; while($value ne '99'){ print "\n\n Destroy aggr = 1 \t Destroy volume += 2\n\n"; my $choice = <>; chomp $choice; if($choice eq '1'){ @lines =$t ->cmd ("sysconfig -r" +); print @lines; print "\n\nEnter the name of agg +r to destroy"; my $aggr = <>; chomp $aggr; @lines = $t ->print ("aggr destr +oy $aggr"); $t ->waitfor('/Are you sure you +want to destroy this aggregate ?/'); @lines = $t ->cmd ('yes'); print @lines; }

Here's the issue with an aggregate, it actually works, it destroys the aggregate ( checked in with my server ) but I got a timeout ( command timed-out at project.pl line 916 ) at the end of this and my "print @lines;" isn't functionning, don't have any response when I type my aggregate name

elsif($choice eq'2'){ @lines =$t ->cmd ("sysconfig -r" +); print @lines; print "\n\nEnter name of the vol +ume to destroy : "; my $vol = <>; chomp $vol; @lines =$t ->print ("vol destroy + $vol"); $t ->waitfor('/Are you sure you +want to destroy this volume ?/'); $t ->cmd ('yes'); print @lines; }

The exact same thing in here, it destroys my volume but also got a timeout and no response/display....

print "\n\n\n Entrer pour conti +nuer... (99 pour quitter)\n"; my $exit = <>; chomp $exit; if($exit == "99"){ last; } } }
I hope i've been clear enough for you to understand my situation. Thanks !

Replies are listed 'Best First'.
Re: Troubles with Telnet module
by hippo (Archbishop) on Mar 04, 2019 at 12:25 UTC

    It sounds as if you might benefit from using Expect.

    Also, please don't send your root password in the clear over telnet. Not even on a local network.

      Thanks for the tip, i'll remember that. Expect is a module to use INSTEAD of Net::Telnet, am I correct ?
Re: Troubles with Telnet module
by holli (Abbot) on Mar 04, 2019 at 12:14 UTC
    If it's your server, maybe consider using Perl to write a clean server side code with a decent html interface instead of hacking something together with telnet and shell commands?


    holli

    You can lead your users to water, but alas, you cannot drown them.
      "Perl to write a clean server side code with a decent html interface" You mean like a graphical interface ? That would be incredible... but I have no knowledge on html and just starting Perl... I'm not sure if this is the right idea for the moment... It seems like going through telnet and shell with Perl looks "simpler" to understand and learn... I don't know.
Re: Troubles with Telnet module
by bliako (Abbot) on Mar 04, 2019 at 14:25 UTC

    Net::Telnet's pod:

    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().

    However, you say that all your waitfor()'s are not timing out because the last action, destroy, happens OK. So your problem is after issuing $t->cmd('yes');

    I suggest logging to a file and see what happens after that "yes" using $t->dump_log("mylog.txt"); before connecting. Could host be asking you another question after destroying that volume?

    Now, this route is simple but painful (and relatively secure). Another route, already suggested, is setting a webserver on remote and having it execute actions whenever you do something like http://10.10.4.46/?action=delete_volume provided that you also authenticate (via cookies etc.) which means that your basic webserver now has to have a secure login page. This job can be made easier by using some framework, like Dancer2::Cookbook, instead of plain old CGI but your setup gets more and more complex. HOWEVER, this solution implies that on remote machine you have scripts which do the action without asking you to confirm. Of course you can add an html-based confirm dialog before you execute the action but it will not be the actual confirmation aggr destroy is asking. That can (probably) be bypassed in a very hackish way using the following echo "yes" | aggr destroy XYZ or set no-confirmations mode (I am sure there is something to set this up for aggr delete) or use Expect as suggested already here (in a way similar to waitfor)

    Also, there is the way of executing a command on a remote server using ssh. The benefit of it is that it can be password-less (via keeping keys etc.). E.g. alias XYZ='ssh abc@host command'. Use -t if you want interactive session. ssh's friend is screen which remembers the state of your remote shell even if you logout, meaning history command, past command output etc. But I digress. Bottomline is, you have plenty of options to automate but you need to plan security (passwords, webservers etc.) and safety (running delete without confirmation).