Skip to content

Instantly share code, notes, and snippets.

@ym
Created February 15, 2016 16:50
Show Gist options
  • Save ym/994b5e9e39a613009cff to your computer and use it in GitHub Desktop.
Save ym/994b5e9e39a613009cff to your computer and use it in GitHub Desktop.
ip_rule_add () {
if ip rule list | grep -Fq $1
then
ip rule list | grep -F $1 | awk -F\: -f <(cat - <<-'EOD'
function ltrim(s) { sub(/^[ \t\r\n]+/, "", s); return s }
function rtrim(s) { sub(/[ \t\r\n]+$/, "", s); return s }
function trim(s) { return rtrim(ltrim(s)); }
BEGIN {}
{
rule = trim(trim($2));
if (rule == "") {
next;
}
split(rule, conditions, " ");
if (conditions[2] == "all") {
next;
}
print "ip rule del " $2
}
EOD
) | bash
fi
if [ "$3" != "del" ]; then
ip rule add from $1 table $2
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment