From ce1876b4efa0542355c457916f260f35ed843659 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Wed, 31 Mar 2021 14:21:52 -0700 Subject: [PATCH] vpnc-script-win: dump stdout and stderr when a command fails Technique borrowed from https://github.com/horar/vpnc-scripts/blob/master/vpnc-script-win.js Signed-off-by: Daniel Lenski --- vpnc-script-win.js | 41 +++++++++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/vpnc-script-win.js b/vpnc-script-win.js index c671eeb..d69f5ed 100644 --- a/vpnc-script-win.js +++ b/vpnc-script-win.js @@ -3,6 +3,21 @@ // Sets up the Network interface and the routes // needed by vpnc. +// -------------------------------------------------------------- +// Initial setup +// -------------------------------------------------------------- + +var accumulatedExitCode = 0; +var ws = WScript.CreateObject("WScript.Shell"); +var env = ws.Environment("Process"); +var comspec = ws.ExpandEnvironmentStrings("%comspec%"); + +// How to add the default internal route +// 0 - As interface gateway when setting properties +// 1 - As a 0.0.0.0/0 route with a lower metric than the default route +// 2 - As 0.0.0.0/1 + 128.0.0.0/1 routes (override the default route cleanly) +var REDIRECT_GATEWAY_METHOD = 0; + // -------------------------------------------------------------- // Utilities // -------------------------------------------------------------- @@ -14,7 +29,19 @@ function echo(msg) function run(cmd) { - return (ws.Exec(cmd).StdOut.ReadAll()); + var oExec = ws.Exec(comspec + " /C \"" + cmd + "\" 2>&1"); + oExec.StdIn.Close(); + + var s = oExec.StdOut.ReadAll(); + + var exitCode = oExec.ExitCode; + if (exitCode != 0) { + echo("\"" + cmd + "\" returned non-zero exit status: " + exitCode + ")"); + echo(" stdout+stderr dump: " + s); + } + accumulatedExitCode += exitCode; + + return s; } function getDefaultGateway() @@ -29,17 +56,6 @@ function getDefaultGateway() // Script starts here // -------------------------------------------------------------- -var internal_ip4_netmask = "255.255.255.0" - -var ws = WScript.CreateObject("WScript.Shell"); -var env = ws.Environment("Process"); - -// How to add the default internal route -// 0 - As interface gateway when setting properties -// 1 - As a 0.0.0.0/0 route with a lower metric than the default route -// 2 - As 0.0.0.0/1 + 128.0.0.0/1 routes (override the default route cleanly) -var REDIRECT_GATEWAY_METHOD = 0; - switch (env("reason")) { case "pre-init": break; @@ -196,3 +212,4 @@ case "disconnect": // FIXME: handle IPv6 split-excludes } +WScript.Quit(accumulatedExitCode); -- 2.50.1