Project

General

Profile

Download (5.66 KB) Statistics
| Branch: | Revision:
1
#!/usr/bin/perl
2
#
3
# moncmd - send a command to the mon server
4
#
5
# original file is modified to suit for the operation in this webmin module of msclinux--dt 09 Sept 2001
6
#
7
# Jim Trocki, trockij@transmeta.com
8
#
9
# $Id: moncmd 1.2 Fri, 12 Jan 2001 08:13:31 -0800 trockij $
10
#
11
#    Copyright (C) 1998, Jim Trocki
12
#
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.
17
#
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.
22
#
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
26
#
27
use Getopt::Std;
28
use Socket;
29
use English;
30

    
31
getopts ("ahf:l:s:p:rd");
32

    
33
sub usage;
34
sub do_cmd;
35

    
36
$MONSERVER = $ENV{"MONHOST"}
37
    if (defined ($ENV{"MONHOST"}));
38
$MONSERVER = $opt_s if ($opt_s);
39
$MONPORT   = $opt_p || getservbyname ("mon", "tcp") || 2583;
40

    
41
if ($opt_h) {
42
    usage;
43
}
44

    
45
if (!defined ($MONSERVER)) {
46
    die "No host specified or found in MONHOST\n";
47
}
48

    
49
$SIG{INT} = \&handle_sig;
50
$SIG{TERM} = \&handle_sig;
51

    
52
#
53
# does the input come from stdin or a file?
54
#
55
if ($opt_f) {
56
    if ($opt_f eq "-") {
57
    	$H = STDIN;
58
#print LOG "READING H from STDIN<br>";
59
    } else {
60
    	open (IN, $opt_f) ||
61
	    die "could not open input file: $!\n";
62
	$H = IN;
63
#print LOG "READING H from IN<br>";
64
    }
65

    
66
} elsif (!@ARGV) {
67
    if (-t STDIN) {
68
      print <<EOF
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
72
EOF
73
    ;
74
        exit 1;
75
    }
76

    
77
    $H = STDIN;
78
}
79

    
80
#
81
# get auth info
82
#
83
if ($opt_a) {
84
#open (LOG,">/tmp/monlog");
85
    if ($opt_l) {
86
    	$USER = $opt_l;
87
#print LOG "USER READ FROM -l OPTION =$USER\n";
88
    } else {
89
	die "could not determine username\n"
90
	    unless defined ($USER = getpwuid($EUID));
91
#print LOG "USER DEFAULT TAKEN=$USER\n";
92
    }
93

    
94
    if (-t STDIN) {
95
#print LOG "READING PASSWD FROM STDIN\n";
96
	system "stty -echo";
97
	print "Password: ";
98
	chop ($PASS = <STDIN>);
99
	print "\n";
100
	system "stty echo";
101
	die "invalid password\n" if ($PASS =~ /^\s*$/);
102

    
103
    } elsif (!@ARGV) {
104
	$cmd = <$H>;
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) {
109
	    chomp $cmd;
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) {
115
		$PASS=$1;
116
#print LOG "READING PASSWD FROM $H:$PASS\n";
117
	    }
118
	    
119
	    $cmd = <$H>;
120
	    $cmd1=$cmd;	
121
#print LOG "FINAL CMD: $cmd\n";
122
	}
123

    
124
    }
125
     
126
    die "inadequate authentication information supplied\n"
127
    	if ($USER eq "" || $PASS eq "");
128
}
129

    
130
#
131
# set up TCP socket
132
#
133
$iaddr = inet_aton ($MONSERVER) ||
134
	die "Unable to find server '$MONSERVER'\n";
135

    
136
if ($MONPORT =~ /\D/) { $MONPORT = getservbyname ($MONPORT, 'tcp') }
137
$paddr = pack_sockaddr_in ($MONPORT, $iaddr);
138
$proto = getprotobyname ('tcp');
139

    
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";
144

    
145
select (MON); $| = 1; select (STDOUT);
146

    
147
#if( defined(my $line = <MON>)) {
148
#    chomp $line;
149
#    unless( $line =~ /^220\s/) {
150
#	die "didn't receive expected welcome message\n";
151
#    }
152
#} else {
153
#    die "error communicating with mon server: $!\n";
154
#}
155

    
156
#
157
# authenticate self to the server if necessary
158
#
159
if ($opt_a) {
160
    ($l, @out) = do_cmd(MON, "login $USER $PASS");
161
    die "Could not authenticate\n"
162
	if ($l =~ /^530/);
163
}
164

    
165

    
166
if ($opt_f or !@ARGV) {
167
    #$cmd = <$H> if ($opt_f || !@ARGV);
168
    $cmd = (<$H>||$cmd1) if ($opt_f || !@ARGV);
169
    $l = "";
170
#print LOG "ENTERING TO SEND THE CMD:$cmd\n";
171
    while (defined ($cmd) && defined ($l)) {
172
	#
173
	# send the command
174
	#
175
	chomp $cmd;
176
#print LOG "SENDING THE CMD:$cmd\n";
177
	($l, @out) = do_cmd (MON, $cmd);
178
	last if (!defined ($l));
179
	for (@out) {
180
	    print "$_\n";
181
	}
182
	print "$l\n";
183

    
184
	$cmd = <$H>;
185
    }
186
    close ($H);
187

    
188
} else {
189
    ($l, @out) = do_cmd (MON, "@ARGV");
190
    for (@out) {
191
	print "$_\n";
192
    }
193
    print "$l\n";
194
}
195

    
196
#
197
# log out
198
#
199
do_cmd (MON, "quit");
200

    
201
close(MON);
202

    
203
#close(LOG);
204

    
205
#
206
# submit a command to the server, wait for a response
207
#
208
sub do_cmd {
209
    my ($fd, $cmd) = @_;
210
    my ($l, @out);
211

    
212
    return ("", undef) if ($cmd =~ /^\s*$/);
213

    
214
    @out = ();
215
    print $fd "$cmd\n";
216
#print LOG "SUBMITTING CMD:$cmd\n";
217
    while (defined($l = <$fd>)) {
218
        chomp $l;
219
        if ($l =~ /^(\d{3}\s)/) {
220
            last;
221
        }
222
        push (@out, $l);
223
    }
224

    
225
    ($l, @out);
226
}
227

    
228

    
229
#
230
# usage
231
#
232
sub usage {
233
    print <<EOF;
234

    
235
usage: moncmd [-a] [-l login] [-s host] [-p port] [-f file] commands
236

    
237
Valid commands are:
238
    quit
239
    reset [stopped]
240
    term
241
    list group "groupname"
242
    list disabled
243
    list alerthist
244
    list failurehist
245
    list successes
246
    list failures
247
    list opstatus
248
    list pids
249
    list watch
250
    stop
251
    start
252
    loadstate
253
    savestate
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"...]
261
    enable watch "watch"
262
EOF
263
    exit 0;
264
}
265

    
266

    
267
#
268
# signal handler
269
#
270
sub handle_sig {
271
    system "stty echo";
272
    exit;
273
}
(34-34/46)