zc.pl 2.7 KB

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