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.
12##
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.
17##
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.
22
23use Net::Telnet ();
24use Getopt::Std;
25
26#use strict;
27
28my $host = `hostname -s`; $host =~ s/\s//g;
29my $port = 'zebra';
30my $server = 'localhost';
31
32# Check arguments
33&getopts ('l:e:czborh');
34
35&usage () if $opt_h;
36
37# main
38{
39  my $login_pass = $opt_l || $ENV{ZEBRA_PASSWORD} || 'zebra';
40  my $enable_pass = $opt_e || $ENV{ZEBRA_ENABLE} || '';
41
42  my $port = ($opt_z ? 'zebra' : 0) ||
43	     ($opt_b ? 'bgpd' : 0) ||
44             ($opt_o ? 'ospfd' : 0) ||
45	     ($opt_r ? 'ripd' : 0) || 'zebra';
46
47  my $cmd = join (' ', @ARGV);
48
49  my $t = new Net::Telnet (Timeout => 10,
50			   Prompt  => '/[\>\#] $/',
51			   Port    => $port);
52
53  $t->open ($server);
54
55  $t->cmd ($login_pass);
56  if ($enable_pass) {
57      $t->cmd (String => 'en',
58	       Prompt => '/Password: /');
59      $t->cmd ($enable_pass);
60  }
61  $t->cmd ('conf t') if "$opt_c";
62
63  if ($cmd)
64    {
65      docmd ($t, $cmd);
66      exit (0);
67    }
68
69  my $prompt = sprintf ("%s%s# ", $host,
70			($port eq 'zebra') ? '' : "/$port");
71
72  print "\nZEBRA interactive console ($port)\n\n" if -t STDIN;
73
74  while (1)
75    {
76      $| = 1;
77      print $prompt if -t STDIN;
78      chomp ($cmd = <>);
79      if (!defined ($cmd))
80        {
81	  print "\n" if -t STDIN;
82	  exit(0);
83        }
84      exit (0) if ($cmd eq 'q' || $cmd eq 'quit');
85
86      docmd ($t, $cmd) if $cmd !~ /^\s*$/;
87    }
88
89  exit(0);
90}
91
92sub docmd
93{
94  my ($t, $cmd) = @_;
95  my @lines = $t->cmd ($cmd);
96  print join ('', grep (!/[\>\#] $/, @lines)), "\n";
97}
98
99sub usage
100{
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);
111}
112