自动更新 shadowsocks 的 IP 配置

其实之前想复杂了,总想着怎么加密,事实上,密码本身已经足够长,基本不需要修改,所以只要 update IP 就可以了。

重点研究了一下怎么截取和替换字符串,写得比较丑,不过能实现就好。

#!/bin/sh
cd /etc/shadowsocks
ip=$(ping -c 1 icy.asia |grep 'PING' | awk '{print $3}' | sed 's/(//' | sed 's/)://')
origin=$(cat /etc/shadowsocks/config.json | tail -n +2 | head -n 1 |  awk '{print $2}' | sed 's/"//' | sed 's/",//')
echo "ip:"$ip
a=$(echo $ip|wc -L)
if [ $a -gt 10 ]; then
    if [ $ip != $origin ]; then
    echo "origin:"$origin
        sed -i "2 c 	\"server\": \"$ip\"," config.json
        sed -i "2 c 	\"server\": \"$ip\"," ss-redir.json
        /etc/init.d/shadowsocks restart
        echo "config updated"
    else
        echo "no change"
    fi
else
    echo "cannot get ip address"
fi

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.