CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Help required in refining a script!!!

 



UnixAAP
Novice

Mar 25, 2014, 3:13 AM

Post #1 of 7 (10437 views)
Help required in refining a script!!! Can't Post

Hi Perl Gurus.,

I am a system admin & new to perl scripting..

I need help to create a program to run command on multiple servers (50) .

Login to servers with SSH & encryption key.

I have created a txt file with IP addresses(50) of those servers.= hostlist.txt

This is the command I want to run

command1= esxcli system syslog config set --loghost='udp://<IP address>:514

command2= esxcli system syslog reload



I referred to several other websites & tried to create a dummy script dunno if it works!!



Code
#!/usr/bin/perl 

use strict;
use Net::OpenSSH;

my $ssh;
my $host;
my $start;
my $password;

print "Password: ";
system('stty','-echo');
chop($password=<STDIN>);
system('stty','echo');
print "\n";
open (HOST,"input.txt");
while (<HOST>){
if(/\d+\.\d+\.\d+\.\d+/) {
chomp;
$host = $_;
} else {
print "input.txt must list hosts in IPv4 format.\n";
next;
}
print "Adding files to $host";
$ssh = Net::OpenSSH->new("root:$password\@$host");
$ssh->error and
die "Couldn't establish SSH connection: ". $ssh->error;
$ssh->system = ("esxcli system syslog config set --loghost='udp://<IP address>:514' && esxcli system syslog reload");



Any help on this would be of gr8 help!! :-)


(This post was edited by FishMonger on Mar 25, 2014, 6:52 AM)


FishMonger
Veteran / Moderator

Mar 25, 2014, 7:53 AM

Post #2 of 7 (10425 views)
Re: [UnixAAP] Help required in refining a script!!! [In reply to] Can't Post


Quote
tried to create a dummy script dunno if it works


The starting point would be to run your script and see if it does what you want. If it doesn't then refine your question and explain how it fails.

You should add the warnings or diagnostics pragma.

Code
use warnings; 
use diagnostics;


All vars should be declared in the smallest scope they require rather than at the beginning of the script.

Use chomp instead of chop.
http://perldoc.perl.org/functions/chomp.html

You should use the 3 arg form of open and a lexical var for the filehandle and check the return code to verify that it was successful. If the open call failed, you should print an error message and in most cases the script should "die".

Code
open my $fh, '<', 'input.txt' or die "failed to open 'input.txt' <$!>";


The assignment of $host should be done in the while loop initialization.

Code
while (my $host = <$fh>) {


The regex used to check the IP format could/should be improved, but your current one might be sufficient for your needs.


Code
#!/usr/bin/perl 

use strict;
use warnings;
use Net::OpenSSH;

print "Password: ";
system('stty','-echo');
chomp(my $password = <STDIN>);
system('stty','echo');
print "\n";

open my $fh, '<', 'input.txt' or die "failed to open 'input.txt' <$!>";

while (my $host = <$fh>) {
chomp $host;
if ($host !~ /^\d+\.\d+\.\d+\.\d+$/) {
print "$host is not in IPv4 format.\n";
next;
}
print "Adding files to $host";
$ssh = Net::OpenSSH->new("root:$password\@$host");
$ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
$ssh->system = ("esxcli system syslog config set --loghost='udp://<IP address>:514' && esxcli system syslog reload");
}



UnixAAP
Novice

Mar 26, 2014, 12:14 AM

Post #3 of 7 (10420 views)
Re: [FishMonger] Help required in refining a script!!! [In reply to] Can't Post

Thank you :-) FishMonger,,for the help

I used your code .. got below error

1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use diagnostics;
6 use Net::OpenSSH;
7
8 print "Password: ";
9 system('stty','-echo');
10 chomp(my $password = <STDIN>);
11 system('stty','echo');
12 print "\n";
13
14 open my $fh, '<', 'input.txt' or die "failed to open 'input.txt' <$!>";
15
16 while (my $host = <$fh>) {
17 chomp $host;
18 if ($host !~ /^\d+\.\d+\.\d+\.\d+$/) {
19 print "$host is not in IPv4 format.\n";
20 next;
21 }
22 print "Adding files to $host";
23 $ssh = Net::OpenSSH->new("root:$password\@$host");
24 $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
25 $ssh->system = ("esxcli system syslog config set --loghost='udp://198.19.99.243:514' && esxcli system syslog reload");



------------------------
# ./test1.pl
Global symbol "$ssh" requires explicit package name at ./test1.pl line 23.
Global symbol "$ssh" requires explicit package name at ./test1.pl line 24.
Global symbol "$ssh" requires explicit package name at ./test1.pl line 24.
Global symbol "$ssh" requires explicit package name at ./test1.pl line 25.
Missing right curly or square bracket at ./test1.pl line 25, at end of line
syntax error at ./test1.pl line 25, at EOF
Execution of ./test1.pl aborted due to compilation errors (#1)
(F) You've said "use strict" or "use strict vars", which indicates
that all variables must either be lexically scoped (using "my" or "state"),
declared beforehand using "our", or explicitly qualified to say
which package the global variable is in (using "::").

BEGIN not safe after errors--compilation aborted at /usr/share/perl5/Carp/Heavy.pm line 11.
Compilation failed in require at /usr/share/perl5/Carp.pm line 33.
SmileSmileSmileSmileSmile


UnixAAP
Novice

Mar 26, 2014, 3:38 AM

Post #4 of 7 (10410 views)
Re: [FishMonger] Help required in refining a script!!! [In reply to] Can't Post

now the rest of script works except this

I have shared the keys still the below error is there!!

Global symbol "$ssh" requires explicit package name at ./test4.pl line 22.
Global symbol "$ssh" requires explicit package name at ./test4.pl line 23.
Global symbol "$ssh" requires explicit package name at ./test4.pl line 23.
Global symbol "$ssh" requires explicit package name at ./test4.pl line 24.
Execution of ./test4.pl aborted due to compilation errors.


(This post was edited by UnixAAP on Mar 26, 2014, 4:40 AM)


UnixAAP
Novice

Mar 26, 2014, 5:14 AM

Post #5 of 7 (10386 views)
Re: [UnixAAP] Help required in refining a script!!! [In reply to] Can't Post

Hi FishMonger

Now the script runs except a new error


1 #!/usr/bin/perl
2
3 use strict();
4 use warnings;
5 use Net::OpenSSH;
6
7 print "Password: ";
8 system('stty','-echo');
9 chomp(my $password = <STDIN>);
10 system('stty','echo');
11 print "\n";
12
13 open my $fh, '<', 'input.txt' or die "failed to open 'input.txt' <$!>";
14
15 while (my $host = <$fh>) {
16 chomp $host;
17 if ($host !~ /^\d+\.\d+\.\d+\.\d+$/) {
18 print "$host is not in IPv4 format.\n";
19 next;
20 }
21 print "Adding files to $host";
22 $ssh = Net::OpenSSH->new("root:$password\@$host");
23 $ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
24 $ssh->system = ("esxcli system syslog config set --loghost='udp://198.19.99.243:514' && esxcli system syslog reload");
25 }


-----------------------------------------

ERROR

Couldn't establish SSH connection: ctl_dir /root/.libnet-openssh-perl/ is not secure at ./test4.pl line 23, <$fh> line 1.


FishMonger
Veteran / Moderator

Mar 26, 2014, 6:32 AM

Post #6 of 7 (10369 views)
Re: [UnixAAP] Help required in refining a script!!! [In reply to] Can't Post

Please use the code tags when posting code and don't include the line numbers. The code tags will retain the code formatting/indentation and set the code apart from the rest of the text making it easier to read.

Changing the use strict statement as you did is the wrong way to fix the Global symbol "$ssh" requires explicit package name errors. What you have done basically tells perl to ignore your errors, which is a bad idea. The proper fix is to use the 'my' keyword on line 22 to declare $ssh as a lexical var.

Code
my $ssh = Net::OpenSSH->new("root:$password\@$host");


I have not used that ssh module, but its documentation explains one cause of the error.

Quote
ctl_dir => $path

Directory where the SSH master control socket will be created.

This directory and its parents must be writable only by the current effective user or root, otherwise the connection will be aborted to avoid insecure operation.

By default ~/.libnet-openssh-perl is used.



UnixAAP
Novice

Mar 26, 2014, 11:50 PM

Post #7 of 7 (10135 views)
Re: [FishMonger] Help required in refining a script!!! [In reply to] Can't Post

Thank you FishMonger :-)


The script works now...


Is there possibility of using a better code & indentation ??

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives