diff options
author | Stephen Hemminger <stephen.hemminger@vyatta.com> | 2008-07-28 21:19:04 +0200 |
---|---|---|
committer | Paul Jakma <paul@quagga.net> | 2009-06-18 21:18:24 +0200 |
commit | 57fb974856b9ae143ca5642e279d181a45bbdd10 (patch) | |
tree | 5008d2b2fc2b061747d7afaf27c4f3f197f8ed93 /vtysh/vtysh_main.c | |
parent | [vtysh] Force line buffered mode. (diff) | |
download | frr-57fb974856b9ae143ca5642e279d181a45bbdd10.tar.xz frr-57fb974856b9ae143ca5642e279d181a45bbdd10.zip |
[vtysh] Make vtysh more useable for scripting
Add environment variable (VTYSH_LOG) for logging.
If a command fails, exit with non-zero exit code and don't
continue multipart commands.
Diffstat (limited to 'vtysh/vtysh_main.c')
-rw-r--r-- | vtysh/vtysh_main.c | 45 |
1 files changed, 42 insertions, 3 deletions
diff --git a/vtysh/vtysh_main.c b/vtysh/vtysh_main.c index d655e0731..55a430d5b 100644 --- a/vtysh/vtysh_main.c +++ b/vtysh/vtysh_main.c @@ -58,6 +58,9 @@ static char *line_read; /* Master of threads. */ struct thread_master *master; +/* Command logging */ +FILE *logfile; + /* SIGTSTP handler. This function care user's ^Z input. */ void sigtstp (int sig) @@ -191,6 +194,18 @@ vtysh_rl_gets () return (line_read); } +static void log_it(const char *line) +{ + time_t t = time(NULL); + struct tm *tmp = localtime(&t); + char *user = getenv("USER") ? : "boot"; + char tod[64]; + + strftime(tod, sizeof tod, "%Y%m%d-%H:%M.%S", tmp); + + fprintf(logfile, "%s:%s %s\n", tod, user, line); +} + /* VTY shell main routine. */ int main (int argc, char **argv, char **env) @@ -210,6 +225,10 @@ main (int argc, char **argv, char **env) /* Preserve name of myself. */ progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); + /* if logging open now */ + if ((p = getenv("VTYSH_LOG")) != NULL) + logfile = fopen(p, "a"); + /* Option handling. */ while (1) { @@ -299,19 +318,39 @@ main (int argc, char **argv, char **env) while (cmd != NULL) { + int ret; char *eol; while ((eol = strchr(cmd->line, '\n')) != NULL) { *eol = '\0'; + if (echo_command) - printf("%s%s\n", vtysh_prompt(), cmd->line); - vtysh_execute_no_pager(cmd->line); + printf("%s%s\n", vtysh_prompt(), cmd->line); + + if (logfile) + log_it(cmd->line); + + ret = vtysh_execute_no_pager(cmd->line); + if (ret != CMD_SUCCESS + && ret != CMD_SUCCESS_DAEMON + && ret != CMD_WARNING) + exit(1); + cmd->line = eol+1; } + if (echo_command) printf("%s%s\n", vtysh_prompt(), cmd->line); - vtysh_execute_no_pager (cmd->line); + + if (logfile) + log_it(cmd->line); + + ret = vtysh_execute_no_pager(cmd->line); + if (ret != CMD_SUCCESS + && ret != CMD_SUCCESS_DAEMON + && ret != CMD_WARNING) + exit(1); { struct cmd_rec *cr; |