* Implemented 'exec' action to run a command in an existing network namespace context.
authorUrban Wallasch <urban.wallasch@freenet.de>
Mon, 22 Apr 2019 17:55:29 +0000 (19:55 +0200)
committerUrban Wallasch <urban.wallasch@freenet.de>
Mon, 22 Apr 2019 17:55:29 +0000 (19:55 +0200)
nns.sh

diff --git a/nns.sh b/nns.sh
index b6e0831e324552ca996467016a2c975283b2ec4f..7fa81ade0d7310415ba47e1ce37470d2c8d11741 100755 (executable)
--- a/nns.sh
+++ b/nns.sh
@@ -61,9 +61,9 @@ set -x
 SHORTNAME=${0##*/}
 
 usemsg() {
-    echo "Usage:  $1 name action"
-    echo "where name is the name of a config file or the network"
-    echo "namespace name and action is one of start | stop | reload."
+    echo "Usage:  $1 name start|stop|restart|exec cmd [arg ...]"
+    echo "  where name is the name of a config file or a network namespace name,"
+    echo "  and cmd is a command to execute in the context of an existing namespace."
 }
 
 if [ $# -lt 2 ]; then
@@ -216,6 +216,19 @@ stop_nns() {
     /bin/rmdir $RUNDIR
 }
 
+# Run a specified command inside a network namespace:
+run_nns() {
+    if ! exist_nns $1 ; then
+        echo "Network namespace $1 does not exist,"
+        echo "please choose another name"
+        exit 1
+    fi
+    NNS=$1
+    shift
+    # Run the specified command inside the namespace:
+    $IP netns exec $NNS su -p -c "$*" $SUDO_USER &
+}
+
 
 case $2 in
     start)
@@ -229,7 +242,9 @@ case $2 in
         start_nns $NNSNAME
         ;;
     run|exec)
-        echo "TO-DO: implement '$SHORTNAME run <command>' action."
+        shift
+        shift
+        run_nns $NNSNAME "$@"
         ;;
     *)
         usemsg $SHORTNAME