]> git.puffer.fish Git - mirror/frr.git/commitdiff
[vtysh] Make vtysh more useable for scripting
authorStephen Hemminger <stephen.hemminger@vyatta.com>
Mon, 28 Jul 2008 19:19:04 +0000 (12:19 -0700)
committerPaul Jakma <paul@quagga.net>
Thu, 18 Jun 2009 19:18:24 +0000 (20:18 +0100)
Add environment variable (VTYSH_LOG) for logging.
If a command fails, exit with non-zero exit code and don't
continue multipart commands.

vtysh/vtysh.c
vtysh/vtysh.h
vtysh/vtysh_main.c

index 32b626c70bd10c5ef1d3cd15b696c97a7773feb9..20f7df445fd9ce1fdc984cbb33abe31380bd8c59 100644 (file)
@@ -272,10 +272,10 @@ vtysh_pager_init (void)
 }
 
 /* Command execution over the vty interface. */
-static void
+static int
 vtysh_execute_func (const char *line, int pager)
 {
-  int ret, cmd_stat;
+  int ret, cmd_stat = CMD_SUCCESS;
   u_int i;
   vector vline;
   struct cmd_element *cmd;
@@ -288,7 +288,7 @@ vtysh_execute_func (const char *line, int pager)
   vline = cmd_make_strvec (line);
 
   if (vline == NULL)
-    return;
+    return CMD_SUCCESS;
 
   saved_ret = ret = cmd_execute_command (vline, vty, &cmd, 1);
   saved_node = vty->node;
@@ -394,7 +394,7 @@ vtysh_execute_func (const char *line, int pager)
                          }
                        fp = NULL;
                      }
-                   return;
+                   return CMD_SUCCESS;
                  }
 
                ret = cmd_execute_command (vline, vty, &cmd, 1);
@@ -435,18 +435,19 @@ vtysh_execute_func (const char *line, int pager)
        }
       fp = NULL;
     }
+  return cmd_stat;
 }
 
-void
+int
 vtysh_execute_no_pager (const char *line)
 {
-  vtysh_execute_func (line, 0);
+  return vtysh_execute_func (line, 0);
 }
 
-void
+int
 vtysh_execute (const char *line)
 {
-  vtysh_execute_func (line, 1);
+  return vtysh_execute_func (line, 1);
 }
 
 /* Configration make from file. */
index 3ed0dd328ed13e180af0474e5f0dfbf00e2af085..e711d59338b0f25277fa8ac97056fd1ede5fdd48 100644 (file)
@@ -42,8 +42,8 @@ extern int vtysh_connect_all (const char *optional_daemon_name);
 void vtysh_readline_init (void);
 void vtysh_user_init (void);
 
-void vtysh_execute (const char *);
-void vtysh_execute_no_pager (const char *);
+int vtysh_execute (const char *);
+int vtysh_execute_no_pager (const char *);
 
 char *vtysh_prompt (void);
 
index d655e07317b8f61fad538677edd777c5d7a9c646..55a430d5b7689338d3825c49a03e993ed5aa0a90 100644 (file)
@@ -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;