简易的默认网关保存+Bash补全解决方案 For Arch Linux

嗯, 本猫的脚本有多烂大大们都知道的, 所以本文旨在抛砖引玉, 简单介绍利用 pppd hook 和 dhcpcd hook 做到记忆网关, 以及 bash_completion 补全的实现.

保存用的脚本:
/usr/local/bin/froute-save

#!/bin/bash
interface=${interface:-$1}
gateway=${new_routers:-$5}

if [ "$reason" = 'BOUND' ] && ! ([ -z "$interface" ] || [ -z "$gateway" ])
then
    mkdir -p /tmp/froute/
    chmod 755 /tmp/froute
    echo $gateway > /tmp/froute/$interface
    chmod 644 /tmp/froute/$interface
elif ([ "$reason" = 'STOP' ] || [ "$reason" = 'RELEASE' ]) && ! [ -z "$interface
" ]
then
    rm -f /tmp/froute/$interface
fi

嗯..然后首先是pppd的hook, 代码如下:
/etc/ppp/ip-up.d/02-route.sh

reason=BOUND froute-save $1 0 0 0 $5

/etc/ppp/ip-down.d/02-route.sh

reason=RELEASE froute-save $1

然后是dhcpcd的hook.
/etc/dhcpcd.enter-hook

froute-save

如果有用 netcfg 设置静态地址连接, 可以参考如下配置:

POST_UP="env reason=BOUND froute-save $INTERFACE 0 0 0 $GATEWAY || true"
PRE_DOWN="env reason=RELEASE froute-save $INTERFACE || true"

嗯, 现在记忆网关的部分就完成了 ><


接下来, Bash补全:
/etc/bash_completion.d/froute

_froute()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(cat /tmp/froute/*|xargs echo)" -- $cur) )
}
complete -F _froute cnroutes
complete -F _froute cnroutes_add

嗯当然可以添加自己常用的需要把默认网关做参数的命令在这里…

至此, 完工!

(谢谢 @jimmy_xu_wrk 大大的鼎力相助><)

2 thoughts on “简易的默认网关保存+Bash补全解决方案 For Arch Linux”

Leave a Reply

Your email address will not be published. Required fields are marked *

QR Code Business Card