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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#!/usr/bin/perl -w
#
# Module: vyatta-vtysh.pl
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program 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.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2007 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Stig Thormodsrud
# Date: December 2007
# Description: Wrapper script between vyatta cfg templates and vtysh
#
# **** End License ****
#
use warnings;
use strict;
use POSIX;
my $vtysh;
if ( -x '/usr/bin/vyatta-vtysh' && -S '/var/run/vyatta/quagga/zebra.vty' ) {
$vtysh = '/usr/bin/vyatta-vtysh';
} else {
$vtysh = '/usr/bin/vtysh';
}
my $logfile = '/tmp/vtysh.log';
my $ignore_error = 0;
sub log_it {
my ($cmdline) = @_;
if (substr($cmdline,0,2) eq "-c" and $ignore_error) {
$cmdline = "-n $cmdline";
}
my $timestamp = strftime("%Y%m%d-%H:%M.%S", localtime);
my $user = $ENV{'USER'};
$user = "boot" if !defined $user;
my $rc = open(my $fh, ">>", $logfile);
if (!defined $rc) {
print "Can't open $logfile: [$!]\n";
return;
}
print $fh "$timestamp:$user [$cmdline]\n";
close($fh);
}
sub parse_cmdline {
my $cmdline = "";
foreach my $arg (@ARGV) {
if (substr($arg, 0, 2) eq "-n") {
$ignore_error = 1;
next;
}
if (substr($arg,0, 2) eq "-c") {
#
# This script expects a space between the -c and the command,
# but try to handle it anyway
#
if ($arg ne "-c") {
my $tmp = substr($arg,2);
$cmdline .= "-c \"$tmp\" ";
next;
}
} else {
$arg = " \"$arg\" ";
}
$cmdline .= $arg;
}
return $cmdline
}
#
# Send the config to quagga
#
# Note: Quagga never exits with an error code, but it does print an
# error message to stdout. So if there is output, print it and
# exit with an error. Certain error messages we dont' care about
# such as issuing a "no <foo>" when foo doesn't exist. In those
# cases it is up to the template file whether or not to fail on
# the error code.
#
sub send_cmds_to_quagga {
my ($cmdline) = @_;
my $output = `$vtysh $cmdline`;
if (defined $output && $output ne "") {
if ($ignore_error) {
log_it("Ignoring: $output");
return 0;
}
log_it("Error: $output");
print "%$output\n";
return 1;
}
return 0;
}
sub usage {
print "usage: $0 [-n] -c \"<quagga command>\"\n";
}
#
# main
#
my ($cmdline, $rc);
$cmdline = parse_cmdline();
log_it($cmdline);
if (! defined($cmdline) or $cmdline eq "") {
usage();
exit 1;
}
$rc = send_cmds_to_quagga($cmdline);
exit $rc
#end of file
|