-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtysh.c
205 lines (179 loc) · 4.79 KB
/
vtysh.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Virtual terminal interface shell.
* Copyright (C) 2000 Kunihiro Ishiguro
*
* 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.
*/
#include <common.h>
#include <sys/un.h>
#include <setjmp.h>
#include <sys/wait.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include "command.h"
#include "memory.h"
#include "vtysh.h"
/* Struct VTY. */
struct vty *vty;
/* result of cmd_complete_command() call will be stored here
and used in new_completion() in order to put the space in
correct places only */
int complete_status;
struct cmd_node interface_node =
{
INTERFACE_NODE,
"%s(config-if)# ",
};
/* Execute command in child process. */
int
execute_command (char *command, int argc, char *arg1, char *arg2)
{
int ret;
pid_t pid;
int status;
/* Call fork(). */
pid = fork ();
if (pid < 0)
{
/* Failure of fork(). */
fprintf (stderr, "Can't fork: %s\n", strerror (errno));
exit (1);
}
else if (pid == 0)
{
/* This is child process. */
switch (argc)
{
case 0:
ret = execlp (command, command, NULL);
break;
case 1:
ret = execlp (command, command, arg1, NULL);
break;
case 2:
ret = execlp (command, command, arg1, arg2, NULL);
break;
}
/* When execlp suceed, this part is not executed. */
fprintf (stderr, "Can't execute %s: %s\n", command, strerror (errno));
exit (1);
}
else
{
/* This is parent. */
execute_flag = 1;
ret = wait4 (pid, &status, 0, NULL);
execute_flag = 0;
}
return 0;
}
DEFUN (vtysh_ping,
vtysh_ping_cmd,
"ping WORD",
"send echo messages\n"
"Ping destination address or hostname\n")
{
execute_command ("ping", 1, argv[0], NULL);
return CMD_SUCCESS;
}
DEFUN (vtysh_traceroute,
vtysh_traceroute_cmd,
"traceroute WORD",
"Trace route to destination\n"
"Trace route to destination address or hostname\n")
{
execute_command ("traceroute", 1, argv[0], NULL);
return CMD_SUCCESS;
}
DEFUN (vtysh_telnet,
vtysh_telnet_cmd,
"telnet WORD",
"Open a telnet connection\n"
"IP address or hostname of a remote system\n")
{
execute_command ("telnet", 1, argv[0], NULL);
return CMD_SUCCESS;
}
DEFUN (vtysh_ls,
vtysh_ls_cmd,
"ls",
"list file\n"
"list current file\n")
{
execute_command ("ls", 1, "-l", NULL);
return CMD_SUCCESS;
}
DEFUN (vtysh_telnet_port,
vtysh_telnet_port_cmd,
"telnet WORD PORT",
"Open a telnet connection\n"
"IP address or hostname of a remote system\n"
"TCP Port number\n")
{
execute_command ("telnet", 2, argv[0], argv[1]);
return CMD_SUCCESS;
}
DEFUN (vtysh_start_shell,
vtysh_start_shell_cmd,
"start-shell",
"Start UNIX shell\n")
{
execute_command ("sh", 0, NULL, NULL);
return CMD_SUCCESS;
}
DEFUN (vtysh_start_bash,
vtysh_start_bash_cmd,
"start-shell bash",
"Start UNIX shell\n"
"Start bash\n")
{
execute_command ("bash", 0, NULL, NULL);
return CMD_SUCCESS;
}
DEFUN (vtysh_start_zsh,
vtysh_start_zsh_cmd,
"start-shell zsh",
"Start UNIX shell\n"
"Start Z shell\n")
{
execute_command ("zsh", 0, NULL, NULL);
return CMD_SUCCESS;
}
void vtysh_init_vty ()
{
/* Make vty structure. */
vty = vty_new ();
vty->type = VTY_TERM;
vty->node = VIEW_NODE;
/* Initialize commands. */
cmd_init (1);
/* Install nodes. */
install_node (&interface_node, NULL);
install_element (VIEW_NODE, &vtysh_ping_cmd);
install_element (VIEW_NODE, &vtysh_traceroute_cmd);
install_element (VIEW_NODE, &vtysh_telnet_cmd);
install_element (VIEW_NODE, &vtysh_telnet_port_cmd);
install_element (VIEW_NODE, &vtysh_ls_cmd);
install_element (ENABLE_NODE, &vtysh_ping_cmd);
install_element (ENABLE_NODE, &vtysh_traceroute_cmd);
install_element (ENABLE_NODE, &vtysh_telnet_cmd);
install_element (ENABLE_NODE, &vtysh_telnet_port_cmd);
install_element (ENABLE_NODE, &vtysh_start_shell_cmd);
install_element (ENABLE_NODE, &vtysh_start_bash_cmd);
install_element (ENABLE_NODE, &vtysh_start_zsh_cmd);
nm_if_init();
}