7 # Created by: David Van Ginneken
8 # Bird's the Word Technologies
11 # And distributed under the terms of the GPL
13 my ($user, $pw, $host, $port) = (undef, undef, 'localhost', 5038);
15 process_credentials('/etc/astcli.conf');
16 process_credentials("$ENV{HOME}/.astcli");
17 GetOptions("username=s" => \$user, "secret=s" => \$pw, "host=s" => \$host, "port=s" => \$port);
19 my $action = join(" ", @ARGV);
21 &usage if (!defined $user || !defined $pw);
23 my $tc = new Net::Telnet (Timeout => 10,
27 # Login with our username and secret.
29 $tc->print ("Action: Login");
30 $tc->print ("Username: $user");
31 $tc->print ("Secret: $pw");
32 $tc->print ("Events: off");
34 # Check for login success.
35 my ($pre, $match) = $tc->waitfor ("/Message: .*/");
36 unless (($pre =~ m/Success/) && ($match =~ m/Authentication/)) {
37 print "Server Authentication failed.\n";
41 # Send a single command to the manager connection handle (global $tc).
42 # Assumes things always work well :-)
46 $tc->print ("Action: Command");
47 $tc->print ("Command: $command");
49 my ($pre, undef) = $tc->waitfor ("/--END COMMAND--.*/");
51 $pre =~ s/Privilege: Command\n//;
52 $pre =~ s/Response: Follows\n//;
56 # If the user asked to send commands from standard input:
65 # Otherwise just send the command:
66 send_command($action);
68 # parses a configuration file into the global $user and $pw.
69 sub process_credentials {
70 # Process the credentials found..
73 # silently fail if we can't read the file:
74 return unless (-r $file);
75 open (my $fh, "<$file") or return;
78 (undef,$user) = split(/[,=]/, $_) if $_ =~ /user(name)?[,=]/i;
79 (undef,$pw) = split(/[,=]/, $_) if $_ =~ /(secret|passw(or)?d|pwd?)[,=]/i;
80 (undef,$host) = split(/[,=]/, $_) if $_ =~ /host(name)?[,=]/i;
81 (undef,$port) = split(/[,=]/, $_) if $_ =~ /port(num|no)?[,=]/i;
87 print STDERR "astcli [-u <username> -s <passwd>] [-h host] [-p port] <cli-command>\n";
88 print STDERR " (command '-' - take commands from input)\n";