#!/usr/bin/perl
use strict;
use warnings;
use Net::OpenSSH;

# Constants
my $VERSION = "1.0.0";
my $HOSTLIST = "/root/.guesthosts";
my $DEFEDITOR = "/usr/bin/nano";
my $EDITOR = "";

if ($ENV{'EDITOR'})
{
        $EDITOR = $ENV{'EDITOR'};
}
else
{
        $EDITOR = $DEFEDITOR;
}


print "pushguest v$VERSION\n";
print "=================\n";
if (! -f $HOSTLIST)
{
	print "Host list file ($HOSTLIST) does not exist. Please create it. One server per line.\n";
	sleep(3);
	system ("$EDITOR $HOSTLIST");
	print "Rerun this script to try again\n";
	exit 0;
};

chdir "/home/hotlineuser/HotlineFiles/Users";

print "Enter the password to use (or \"quit\"): ";
my $USEPASSWORD = <>;
chop($USEPASSWORD);
if ($USEPASSWORD eq "quit")
{
	exit 0;
}

# Open the host list
open( my $fh => $HOSTLIST) || die "Cannot open $HOSTLIST: $!";

# Loop for each line
while(my $curline = <$fh>)
{
	chop($curline);
	print "Working on server '$curline'\n";
	my $ssh = Net::OpenSSH->new($curline, user => "hotlineuser", password => $USEPASSWORD);
	$ssh->error and die "Couldn't establish SSH connection: ". $ssh->error;
	$ssh->scp_put("guest.yaml", "/home/hotlineuser/HotlineFiles/Users") or die "scp failed: " . $ssh->error;
}
close($fh);

exit 0;

