Amu-chan/Source/amusend.pl

From SDA Knowledge Base

< Amu-chan‎ | Source
Revision as of 23:37, 11 October 2008 by Njahnke (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
#!/usr/bin/perl
#nathan jahnke <njahnke@gmail.com>

use warnings;
use strict 'subs';

if (!(-e "amu_remotehosts.txt")) {
	die "amu_remotehosts.txt not found";
}

use IO::Socket;
use Sys::Hostname;
use Sys::HostIP;

$port = 56677;
if ($#ARGV == 0) {
	$command = $ARGV[0];
	print "\$command is $command\n\n";
}

if (open(REMOTEHOSTS,"amu_remotehosts.txt")) {
	while (<REMOTEHOSTS>) {
		chomp;
		s/^#.*$//; #kill commented out hosts
		$remote_hosts{$_} = 0 if $_; #a space for amus to sign off that they have seen the list
	}
	close(REMOTEHOSTS);
	
	print "\%remote_hosts:\n";
	foreach $host (keys %remote_hosts) {
		print "\"$host\" => \"$remote_hosts{$host}\"\n";
	}
	print "\n";
}

&contact_amu("AMU HOSTS ".join(";",%remote_hosts)."\n");
&contact_amu("AMU DO $command\n") if $#ARGV == 0;

sub contact_amu {
	my $command = shift;
	foreach $remote_host (keys %remote_hosts) {
		print "trying $remote_host ...\n";
		if (my $socket = IO::Socket::INET->new(PeerAddr => $remote_host,
						PeerPort => $port,
						Proto    => "tcp",
						Type     => SOCK_STREAM,
						Timeout  => 5 )) {
			print $socket $command;
			my @answer = <$socket>;
			print "contact with $remote_host\n";
			close($socket);
		} else {
			print "no contact with $remote_host\n";
		}
	}
}

exit 0;
Personal tools