3 # moncmd - send a command to the mon server
5 # original file is modified to suit for the operation in this webmin module of msclinux--dt 09 Sept 2001
7 # Jim Trocki, trockij@transmeta.com
9 # $Id: moncmd 1.2 Fri, 12 Jan 2001 08:13:31 -0800 trockij $
11 # Copyright (C) 1998, Jim Trocki
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 getopts ("ahf:l:s:p:rd");
36 $MONSERVER = $ENV{"MONHOST"}
37 if (defined ($ENV{"MONHOST"}));
38 $MONSERVER = $opt_s if ($opt_s);
39 $MONPORT = $opt_p || getservbyname ("mon", "tcp") || 2583;
45 if (!defined ($MONSERVER)) {
46 die "No host specified or found in MONHOST\n";
49 $SIG{INT} = \&handle_sig;
50 $SIG{TERM} = \&handle_sig;
53 # does the input come from stdin or a file?
58 #print LOG "READING H from STDIN<br>";
61 die "could not open input file: $!\n";
63 #print LOG "READING H from IN<br>";
69 You did not give a command on the command line nor a -f flag and
70 the program is running interactively (e.g. reading from terminal).
71 This is not supported. Exiting
84 #open (LOG,">/tmp/monlog");
87 #print LOG "USER READ FROM -l OPTION =$USER\n";
89 die "could not determine username\n"
90 unless defined ($USER = getpwuid($EUID));
91 #print LOG "USER DEFAULT TAKEN=$USER\n";
95 #print LOG "READING PASSWD FROM STDIN\n";
98 chop ($PASS = <STDIN>);
101 die "invalid password\n" if ($PASS =~ /^\s*$/);
105 #print LOG "READING CMD FROM $H\n";
106 #print LOG "CMD:$cmd\n";
107 while (defined ($cmd) && $cmd =~ /user=|pass=/i) {
108 #while (defined ($cmd) && $cmd =~ /user|pass/i) {
110 #print LOG "CMD AFTER CHOMP:$cmd\n";
111 if ($cmd =~ /^user=(\S+)$/i) {
112 $USER=$1 if (!defined ($USER));
113 #print LOG "READING USER FROM $H:$USER\n";
114 } elsif ($cmd =~ /^pass=(\S+)$/i) {
116 #print LOG "READING PASSWD FROM $H:$PASS\n";
121 #print LOG "FINAL CMD: $cmd\n";
126 die "inadequate authentication information supplied\n"
127 if ($USER eq "" || $PASS eq "");
133 $iaddr = inet_aton ($MONSERVER) ||
134 die "Unable to find server '$MONSERVER'\n";
136 if ($MONPORT =~ /\D/) { $MONPORT = getservbyname ($MONPORT, 'tcp') }
137 $paddr = pack_sockaddr_in ($MONPORT, $iaddr);
138 $proto = getprotobyname ('tcp');
140 socket (MON, PF_INET, SOCK_STREAM, $proto) ||
141 die "could not create socket: $!\n";
142 connect (MON, $paddr) ||
143 die "could not connect: $!\n";
145 select (MON); $| = 1; select (STDOUT);
147 #if( defined(my $line = <MON>)) {
149 # unless( $line =~ /^220\s/) {
150 # die "didn't receive expected welcome message\n";
153 # die "error communicating with mon server: $!\n";
157 # authenticate self to the server if necessary
160 ($l, @out) = do_cmd(MON, "login $USER $PASS");
161 die "Could not authenticate\n"
166 if ($opt_f or !@ARGV) {
167 #$cmd = <$H> if ($opt_f || !@ARGV);
168 $cmd = (<$H>||$cmd1) if ($opt_f || !@ARGV);
170 #print LOG "ENTERING TO SEND THE CMD:$cmd\n";
171 while (defined ($cmd) && defined ($l)) {
176 #print LOG "SENDING THE CMD:$cmd\n";
177 ($l, @out) = do_cmd (MON, $cmd);
178 last if (!defined ($l));
189 ($l, @out) = do_cmd (MON, "@ARGV");
199 do_cmd (MON, "quit");
206 # submit a command to the server, wait for a response
212 return ("", undef) if ($cmd =~ /^\s*$/);
216 #print LOG "SUBMITTING CMD:$cmd\n";
217 while (defined($l = <$fd>)) {
219 if ($l =~ /^(\d{3}\s)/) {
235 usage: moncmd [-a] [-l login] [-s host] [-p port] [-f file] commands
241 list group "groupname"
254 set "group" "service" "variable" "value"
255 get "group" "service" "variable"
256 disable service "group" "service"
257 disable host "host" ["host"...]
258 disable watch "watch"
259 enable service "group" "service"
260 enable host "host" ["host"...]