summaryrefslogtreecommitdiffstats
path: root/tools/zc.pl
blob: 026e8fe5ba920d48a455a47831f6af6dca9b85fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#! /usr/bin/perl
##
## Zebra interactive console
## Copyright (C) 2000 Vladimir B. Grebenschikov <vova@express.ru>
##
## This file is part of GNU Zebra.
##
## GNU Zebra is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by the
## Free Software Foundation; either version 2, or (at your option) any
## later version.
##
## GNU Zebra is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with GNU Zebra; see the file COPYING.  If not, write to the
## Free Software Foundation, Inc., 59 Temple Place - Suite 330,
## Boston, MA 02111-1307, USA.

use Net::Telnet ();
use Getopt::Std;

#use strict;

my $host = `hostname -s`; $host =~ s/\s//g;
my $port = 'zebra';
my $server = 'localhost';

# Check arguments
&getopts ('l:e:czborh');

&usage () if $opt_h;

# main 
{
  my $login_pass = $opt_l || $ENV{ZEBRA_PASSWORD} || 'zebra';
  my $enable_pass = $opt_e || $ENV{ZEBRA_ENABLE} || '';

  my $port = ($opt_z ? 'zebra' : 0) ||
	     ($opt_b ? 'bgpd' : 0) ||
             ($opt_o ? 'ospfd' : 0) ||
	     ($opt_r ? 'ripd' : 0) || 'zebra';

  my $cmd = join (' ', @ARGV);

  my $t = new Net::Telnet (Timeout => 10,
			   Prompt  => '/[\>\#] $/',
			   Port    => $port);

  $t->open ($server);

  $t->cmd ($login_pass);
  if ($enable_pass) {
      $t->cmd (String => 'en',
	       Prompt => '/Password: /');
      $t->cmd ($enable_pass);
  }
  $t->cmd ('conf t') if "$opt_c";

  if ($cmd)
    {
      docmd ($t, $cmd);
      exit (0); 
    }

  my $prompt = sprintf ("%s%s# ", $host,
			($port eq 'zebra') ? '' : "/$port");

  print "\nZEBRA interactive console ($port)\n\n" if -t STDIN;

  while (1)
    {
      $| = 1;
      print $prompt if -t STDIN;
      chomp ($cmd = <>);
      if (!defined ($cmd))
        {
	  print "\n" if -t STDIN;
	  exit(0);
        }
      exit (0) if ($cmd eq 'q' || $cmd eq 'quit');
 
      docmd ($t, $cmd) if $cmd !~ /^\s*$/;
    }

  exit(0);
}

sub docmd
{
  my ($t, $cmd) = @_;
  my @lines = $t->cmd ($cmd);
  print join ('', grep (!/[\>\#] $/, @lines)), "\n";
}

sub usage
{
  print "USAGE: $0 [-l LOGIN_PASSWORD] [-e ENABLE_PASSWORD] [-z|-b|-o|-r|-h] [<cmd>]\n",
        "\t-l - specify login password\n",
        "\t-e - specify enable password\n",
        "\t-c - execute command in configure mode\n",
        "\t-z - connect to zebra daemon\n",
        "\t-b - connect to bgpd  daemon\n",
        "\t-o - connect to ospfd daemon\n",
        "\t-r - connect to ripd  daemon\n",
        "\t-h - help\n";
  exit (1);
}