
1arryb
User
Apr 21, 2009, 9:17 AM
Views: 4639
|
|
Re: [richsark] Help needed for my script
|
|
|
Hi Rich, Here you go. This program compiles cleanly, but is untested.
#!/usr/bin/perl use strict; use warnings; use Net::SSH::Expect; open(my $out, "log.txt") or die "Could not open log: $!\n"; ##### Step 1, read subnet.txt open(my $in, "<subnet.txt") or die "Could not open subnet.txt: $!\n"; # Keep track of hosts we've seen before. my %seenHosts; while(my $zone = <$in>) { # In long loops it's better to read <$in> to a local variable instead of relying on $_. chomp($zone); ##### Step 2: run getzoneprof, and keep first 4 lines only my @getzoneprof = `getzoneprof -u Xxx -p Xxx -a $zone -o Orig`; @getzoneprof = @getzoneprof[0..3]; ##### Parse hostnames from the dnsServers line. my $serverLine = $getzoneprof[-1]; # check that $getzoneprof[-1] actually contains a dnsServers line here. unless ( $serverLine =~ /dnsServers/ ) { print "Can't get a dnsServers line from getzoneprof -a $zone\n"; next; } $serverLine =~ s/dnsServers=//; # Remove the prefix. my @servers = (); foreach my $token (split(/,/, $serverLine)) { #split the server line on the commas. $token =~ /(\S+)/; # Look for the first "word" of the token. push (@servers, $1) if $1; } unless (scalar(@servers)) { print "Can't parse a dns server from \"dnsServers=$serverLine\"\n"; next; } foreach my $dns (@servers) { next if exists($seenHosts{$dns}); $seenHosts{$dns} = 1; ##### ssh to server my $ssh = Net::SSH::Expect->new ( host => $dns, password=> '12345', user => 'richsark', ); my $login_output = $ssh->login(); unless($login_output =~ /Last Login/) { warn "Login has failed. Login output was $login_output"; # Make sure we close the ssh session if we abort the loop. $ssh->close; next; } ##### run ls to look for files my $ls = $ssh->exec("ls /opt/rich/sark/*.jnl"); my $found='no'; $found='yes' if $ls =~ m|/opt/rich/sark/|; print $out "From zone $zone containing dnsserver \"$dns\" found=$found of presense of \"jnl\" files\n"; $ssh->close; } } close($out); Cheers, Larry
(This post was edited by 1arryb on Apr 21, 2009, 9:21 AM)
|