Skip to content

Instantly share code, notes, and snippets.

@DavidWittman
Last active December 20, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidWittman/6043706 to your computer and use it in GitHub Desktop.
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
#!/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}}
@JCallicoat
Copy link

Shouldn't #47 be echo ip netns exec ${NETWORK_NAMESPACE} ${@/_IP_/${IP}} 1>&2 ?

@DavidWittman
Copy link
Author

@JCallicoat That would in fact be more intelligent. Fixed. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment