2008-04-09 13:23:44 +00:00
|
|
|
#!/usr/bin/perl -w
|
2008-04-08 21:33:11 +00:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Net::Telnet;
|
|
|
|
use Getopt::Long;
|
|
|
|
|
|
|
|
# Created by: David Van Ginneken
|
|
|
|
# Bird's the Word Technologies
|
|
|
|
# davevg@btwtech.com
|
2008-04-09 13:23:44 +00:00
|
|
|
#
|
|
|
|
# And distributed under the terms of the GPL
|
|
|
|
#
|
|
|
|
my ($user, $pw, $host, $port) = (undef, undef, 'localhost', 5038);
|
|
|
|
|
|
|
|
process_credentials('/etc/astcli.conf');
|
|
|
|
process_credentials("$ENV{HOME}/.astcli");
|
|
|
|
GetOptions("username=s" => \$user, "secret=s" => \$pw, "host=s" => \$host, "port=s" => \$port);
|
|
|
|
|
2008-04-08 21:33:11 +00:00
|
|
|
my $action = join(" ", @ARGV);
|
|
|
|
|
|
|
|
&usage if (!defined $user || !defined $pw);
|
|
|
|
|
|
|
|
my $tc = new Net::Telnet (Timeout => 10,
|
|
|
|
Errmode => "die",
|
2008-04-09 13:23:44 +00:00
|
|
|
Host => $host,
|
|
|
|
Port => $port);
|
2008-04-08 21:33:11 +00:00
|
|
|
# Login with our username and secret.
|
|
|
|
$tc->open ();
|
|
|
|
$tc->print ("Action: Login");
|
|
|
|
$tc->print ("Username: $user");
|
|
|
|
$tc->print ("Secret: $pw");
|
|
|
|
$tc->print ("Events: off");
|
|
|
|
$tc->print ("");
|
|
|
|
# Check for login success.
|
|
|
|
my ($pre, $match) = $tc->waitfor ("/Message: .*/");
|
|
|
|
unless (($pre =~ m/Success/) && ($match =~ m/Authentication/)) {
|
|
|
|
print "Server Authentication failed.\n";
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2008-04-09 13:23:44 +00:00
|
|
|
# Send a single command to the manager connection handle (global $tc).
|
|
|
|
# Assumes things always work well :-)
|
|
|
|
sub send_command($) {
|
|
|
|
my $command = shift;
|
2008-04-08 21:33:11 +00:00
|
|
|
|
2008-04-09 13:23:44 +00:00
|
|
|
$tc->print ("Action: Command");
|
|
|
|
$tc->print ("Command: $command");
|
|
|
|
$tc->print ("");
|
|
|
|
my ($pre, undef) = $tc->waitfor ("/--END COMMAND--.*/");
|
|
|
|
$pre =~ s/^\n\n//g;
|
|
|
|
$pre =~ s/Privilege: Command\n//;
|
|
|
|
$pre =~ s/Response: Follows\n//;
|
|
|
|
print $pre;
|
|
|
|
}
|
|
|
|
|
|
|
|
# If the user asked to send commands from standard input:
|
|
|
|
if ($action eq '-') {
|
|
|
|
while (<>) {
|
|
|
|
chomp;
|
|
|
|
send_command($_)
|
|
|
|
}
|
|
|
|
exit 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Otherwise just send the command:
|
|
|
|
send_command($action);
|
|
|
|
|
|
|
|
# parses a configuration file into the global $user and $pw.
|
2008-04-08 21:33:11 +00:00
|
|
|
sub process_credentials {
|
|
|
|
# Process the credentials found..
|
|
|
|
my $file = shift;
|
2008-04-09 13:23:44 +00:00
|
|
|
|
|
|
|
# silently fail if we can't read the file:
|
|
|
|
return unless (-r $file);
|
|
|
|
open (my $fh, "<$file") or return;
|
2008-04-08 21:33:11 +00:00
|
|
|
while (<$fh>) {
|
|
|
|
chomp;
|
|
|
|
(undef,$user) = split(/[,=]/, $_) if $_ =~ /user(name)?[,=]/i;
|
2008-04-09 13:23:44 +00:00
|
|
|
(undef,$pw) = split(/[,=]/, $_) if $_ =~ /(secret|passw(or)?d|pwd?)[,=]/i;
|
|
|
|
(undef,$host) = split(/[,=]/, $_) if $_ =~ /host(name)?[,=]/i;
|
|
|
|
(undef,$port) = split(/[,=]/, $_) if $_ =~ /port(num|no)?[,=]/i;
|
2008-04-08 21:33:11 +00:00
|
|
|
}
|
2008-04-09 17:56:07 +00:00
|
|
|
close ($fh);
|
2008-04-08 21:33:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub usage {
|
2008-04-09 13:23:44 +00:00
|
|
|
print STDERR "astcli [-u <username> -s <passwd>] [-h host] [-p port] <cli-command>\n";
|
|
|
|
print STDERR " (command '-' - take commands from input)\n";
|
2008-04-08 21:33:11 +00:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|