In a previous article I talked about how we could use the openssh module in perl for establishing ssh connections to remote hosts. In this article I'll talk about ssh based communication without the use of any modules.
Using modules is the right way to go but take an isolated scenario wherein you are not allowed to install perl modules from CPAN on your servers. This is a workaround for that.
root@buntu:~# cat test6.pl
#!/usr/bin/perl -w
open ( HAN1, "ssh -l sa buntu uptime;uname -a; whoami|") || die "not connecting $!";
open (HAN2, ">> outfile.txt") || die "could not write to file $!";
while (<HAN1>) {
print HAN2;
}
close HAN1;
The above example uses the open function to interact with the ssh process & prints the output to file outfile.txt.
Here's another example to iterate through a list of hosts.
root@buntu:~# cat test9.pl
#!/usr/bin/perl -w
@hlist = `cat hostlist` ;
foreach my $list (@hlist) {
# system ("ssh -l sa $list uptime") ;
open ( HAN1, "ssh -l sa $list uptime;uname -a; whoami|") || die "not connecting $!";
while(<HAN1>) {
print ;
}
close HAN1;
}
Using modules is the right way to go but take an isolated scenario wherein you are not allowed to install perl modules from CPAN on your servers. This is a workaround for that.
root@buntu:~# cat test6.pl
#!/usr/bin/perl -w
open ( HAN1, "ssh -l sa buntu uptime;uname -a; whoami|") || die "not connecting $!";
open (HAN2, ">> outfile.txt") || die "could not write to file $!";
while (<HAN1>) {
print HAN2;
}
close HAN1;
The above example uses the open function to interact with the ssh process & prints the output to file outfile.txt.
Here's another example to iterate through a list of hosts.
root@buntu:~# cat test9.pl
#!/usr/bin/perl -w
@hlist = `cat hostlist` ;
foreach my $list (@hlist) {
# system ("ssh -l sa $list uptime") ;
open ( HAN1, "ssh -l sa $list uptime;uname -a; whoami|") || die "not connecting $!";
while(<HAN1>) {
print ;
}
close HAN1;
}
This iterates through a list of servers contained in the file hostlist. This example uses nested loops.
Here are two more methods of running commands on multiple servers which I found much easier than the ones listed above:
[root@cent6 ~]# cat abc.pl
#!/usr/bin/perl -w
my @hosts = ("cent6", "cent6", "cent6") ;
my $comm = "uname -a;uptime" ;
foreach my $server (@hosts) {
my $command_list = `ssh $server '$comm'` ;
print "$command_list \n" ;
}
[root@cent6 ~]#
[root@cent6 ~]#
[root@cent6 ~]# cat test.pl
#!/usr/bin/perl -w
open (FH, "file") || die ;
my @list = <FH> ; #or use my @list = `cat file` ; to skip file handles
foreach my $server (@list) {
chomp $server ;
system ("ssh -l root $server 'uname -a; uptime'");
}
Here are two more methods of running commands on multiple servers which I found much easier than the ones listed above:
[root@cent6 ~]# cat abc.pl
#!/usr/bin/perl -w
my @hosts = ("cent6", "cent6", "cent6") ;
my $comm = "uname -a;uptime" ;
foreach my $server (@hosts) {
my $command_list = `ssh $server '$comm'` ;
print "$command_list \n" ;
}
[root@cent6 ~]#
[root@cent6 ~]#
[root@cent6 ~]# cat test.pl
#!/usr/bin/perl -w
open (FH, "file") || die ;
my @list = <FH> ; #or use my @list = `cat file` ; to skip file handles
foreach my $server (@list) {
chomp $server ;
system ("ssh -l root $server 'uname -a; uptime'");
}
No comments:
Post a Comment