Loading... 通过编写脚本代码查看统计服务器网络流量。 步骤很简单,在/etc/ppp/ip-up文件最下边添加下边的脚本: ``` echo "****************************************************" >> /var/log/VPN-${1}.log echo "username: $PEERNAME" >> /var/log/VPN-${1}.log echo "clientIP: $6" >> /var/log/VPN-${1}.log echo "device: $1" >> /var/log/VPN-${1}.log echo "vpnIP: $4" >> /var/log/VPN-${1}.log echo "assignIP: $5" >> /var/log/VPN-${1}.log echo "logintime: `date -d today +%F_%T`" >> /var/log/VPN-${1}.logCopy ``` 在/etc/ppp/ip-down文件最下边添加下边的脚本: ``` echo "downtime: `date -d today +%F_%T`" >> /var/log/VPN-${1}.log echo "bytes sent: $BYTES_SENT B" >> /var/log/VPN-${1}.log echo "bytes received: $BYTES_RCVD B" >> /var/log/VPN-${1}.log sum_bytes=$(($BYTES_SENT+$BYTES_RCVD)) sum=`echo "scale=2;$sum_bytes/1024/1024"|bc` echo "bytes sum: $sum MB" >> /var/log/VPN-${1}.log ave=`echo "scale=2;$sum_bytes/1024/$CONNECT_TIME"|bc` echo "average speed: $ave KB/s" >> /var/log/VPN-${1}.log echo "connect time: $CONNECT_TIME S" >> /var/log/VPN-${1}.log echo "****************************************************" >> /var/log/VPN-${1}.logCopy ``` 注意这个代码为了计算平局网速用到了dc工具,可能需要提前安装: ``` sudo apt install bcCopy ``` 然后就能在/var/log/VPN-ppp0.log中看到vpn连接记录类似这样: ``` **************************************************** username: XXX clientIP: XXX.XXX.XXX.XXX device: ppp0 vpnIP: 172.16.0.1 assignIP: 172.16.0.10 logintime: 2018-06-10_17:11:20 downtime: 2018-06-10_17:11:21 bytes sent: 3272 B bytes received: 3626 B bytes sum: 0 MB average speed: 6.73 KB/s connect time: 1 S ****************************************************Copy ``` 如果有多个用户同时连接,那么可能会生成多个log文件,通过ppp\*这个星号数字作区分。 侵删转自:[矛盾体](https://blog.shuspieler.com/756/) 最后修改:2024 年 10 月 07 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏