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.

------------------------ # ./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.

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.