Last active
December 20, 2015 00:39
-
-
Save DavidWittman/6043706 to your computer and use it in GitHub Desktop.
neutron-scope: Run a command in the network namespace of an OpenStack server by name or ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# neutron-scope | |
# | |
# Run a command in the network namespace of an OpenStack server | |
# | |
# Pro-tip: The string _IP_ is replaced with the first IP address of the server | |
# e.g. neutron-scope my-server ssh root@_IP_ | |
set -e | |
NOVA=${NOVA:-"nova"} | |
NAMESPACE_PREFIX=${NAMESPACE_PREFIX:-"qdhcp-"} | |
function usage() { | |
echo -e "usage:\t${0} <server-id> <command>" | |
} | |
function get_network_id() { | |
local NETWORK_NAME=$(${NOVA} show ${1} | awk '$3 == "network" { print $2; exit }') | |
NETWORK_ID=$(${NOVA} network-show ${NETWORK_NAME} | awk '$2 == "id" { print $4 }') | |
} | |
function get_ip() { | |
IP=$(${NOVA} show ${1} | awk '$3 == "network" { print $5; exit }') | |
} | |
if [[ $# -lt 2 ]]; then | |
usage | |
exit 1 | |
fi | |
SERVER=$1 | |
shift | |
get_network_id ${SERVER} | |
# Fetch the instance IP if _IP_ is in the command | |
if [[ ${@} =~ "_IP_" ]]; then | |
get_ip ${SERVER} | |
else | |
IP="" | |
fi | |
NETWORK_NAMESPACE=${NAMESPACE_PREFIX}${NETWORK_ID} | |
echo ip netns exec ${NETWORK_NAMESPACE} "${@/_IP_/${IP}}" 1>&2 | |
ip netns exec ${NETWORK_NAMESPACE} ${@/_IP_/${IP}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Shouldn't #47 be
echo ip netns exec ${NETWORK_NAMESPACE} ${@/_IP_/${IP}} 1>&2
?