all: check return value from daemon() call
[jleu-quagga.git] / tools / zc.pl
blob026e8fe5ba920d48a455a47831f6af6dca9b85fe
1 #! /usr/bin/perl
2 ##
3 ## Zebra interactive console
4 ## Copyright (C) 2000 Vladimir B. Grebenschikov <vova@express.ru>
5 ##
6 ## This file is part of GNU Zebra.
7 ##
8 ## GNU Zebra is free software; you can redistribute it and/or modify it
9 ## under the terms of the GNU General Public License as published by the
10 ## Free Software Foundation; either version 2, or (at your option) any
11 ## later version.
13 ## GNU Zebra is distributed in the hope that it will be useful, but
14 ## WITHOUT ANY WARRANTY; without even the implied warranty of
15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ## General Public License for more details.
18 ## You should have received a copy of the GNU General Public License
19 ## along with GNU Zebra; see the file COPYING. If not, write to the
20 ## Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 ## Boston, MA 02111-1307, USA.
23 use Net::Telnet ();
24 use Getopt::Std;
26 #use strict;
28 my $host = `hostname -s`; $host =~ s/\s//g;
29 my $port = 'zebra';
30 my $server = 'localhost';
32 # Check arguments
33 &getopts ('l:e:czborh');
35 &usage () if $opt_h;
37 # main
39 my $login_pass = $opt_l || $ENV{ZEBRA_PASSWORD} || 'zebra';
40 my $enable_pass = $opt_e || $ENV{ZEBRA_ENABLE} || '';
42 my $port = ($opt_z ? 'zebra' : 0) ||
43 ($opt_b ? 'bgpd' : 0) ||
44 ($opt_o ? 'ospfd' : 0) ||
45 ($opt_r ? 'ripd' : 0) || 'zebra';
47 my $cmd = join (' ', @ARGV);
49 my $t = new Net::Telnet (Timeout => 10,
50 Prompt => '/[\>\#] $/',
51 Port => $port);
53 $t->open ($server);
55 $t->cmd ($login_pass);
56 if ($enable_pass) {
57 $t->cmd (String => 'en',
58 Prompt => '/Password: /');
59 $t->cmd ($enable_pass);
61 $t->cmd ('conf t') if "$opt_c";
63 if ($cmd)
65 docmd ($t, $cmd);
66 exit (0);
69 my $prompt = sprintf ("%s%s# ", $host,
70 ($port eq 'zebra') ? '' : "/$port");
72 print "\nZEBRA interactive console ($port)\n\n" if -t STDIN;
74 while (1)
76 $| = 1;
77 print $prompt if -t STDIN;
78 chomp ($cmd = <>);
79 if (!defined ($cmd))
81 print "\n" if -t STDIN;
82 exit(0);
84 exit (0) if ($cmd eq 'q' || $cmd eq 'quit');
86 docmd ($t, $cmd) if $cmd !~ /^\s*$/;
89 exit(0);
92 sub docmd
94 my ($t, $cmd) = @_;
95 my @lines = $t->cmd ($cmd);
96 print join ('', grep (!/[\>\#] $/, @lines)), "\n";
99 sub usage
101 print "USAGE: $0 [-l LOGIN_PASSWORD] [-e ENABLE_PASSWORD] [-z|-b|-o|-r|-h] [<cmd>]\n",
102 "\t-l - specify login password\n",
103 "\t-e - specify enable password\n",
104 "\t-c - execute command in configure mode\n",
105 "\t-z - connect to zebra daemon\n",
106 "\t-b - connect to bgpd daemon\n",
107 "\t-o - connect to ospfd daemon\n",
108 "\t-r - connect to ripd daemon\n",
109 "\t-h - help\n";
110 exit (1);