Skip to content

Instantly share code, notes, and snippets.

@kchr
Created August 9, 2018 21:18
Show Gist options
  • Save kchr/1749a67f66d8c20f8ddc9ac2acc4d807 to your computer and use it in GitHub Desktop.
Save kchr/1749a67f66d8c20f8ddc9ac2acc4d807 to your computer and use it in GitHub Desktop.
Network-Manager CLI wrapper for Qubes
#!/usr/bin/env bash
#
# Simple wrapper for Network-Manager CLI on VM
#
target_vm=sys-net
qvm_run() {
local vm=$1; shift
local cmd="$@"
qvm-run \
--no-gui \
--pass-io \
--no-autostart \
--no-colour-output \
--no-colour-stderr \
--filter-escape-chars \
"${vm}" "${cmd}"
}
exit_msg() {
local msg="$1" status="${2:-1}"
echo "ERROR: ${msg}"; exit $status
}
nmcli_run() {
local cmd=$@
qvm_run $target_vm nmcli $@
}
nmcli_print_data() {
nmcli_run --mode tabular --terse $@
}
nmcli_status() {
nmcli_run general
}
nmcli_list_conns() {
local filter="$@"
nmcli_print_data --fields name connection \
| grep -i "${filter}"
}
nmcli_list_iface() {
local filter="$@"
nmcli_print_data device | grep -i "${filter}"
}
nmcli_conn() {
local action=$1; shift
local conn="$@"
if [ -z "${action}" ]; then
exit_msg 'Missing action'
fi
if [ -z "${conn}" ]; then
exit_msg 'Missing connection id'
fi
matched=`nmcli_list_conns "${conn}"`
if [ -n "${matched}" ]; then
echo "Matched connection: ${matched}"
conn=$matched
fi
nmcli_run connection "${action}" "\"${conn}\""
}
command=$1; shift
if [ -z "${command}" ]; then
exit_msg 'Missing command'
fi
case "${command}" in
up|down)
nmcli_conn $command $@
;;
rfkill)
nmcli_run radio all off
;;
status)
nmcli_run general
;;
link)
nmcli_run net connectivity
;;
ls)
nmcli_run connection
;;
*)
nmcli_run $command $@
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment