1
0

netlog.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env bash
  2. ####### 安装使用原理 本脚本实现自动安装################
  3. # apt-get install vnstat
  4. # ip addr 查看网卡名称是否是 eth0,如果是网卡名是 ens3 或者 venet0
  5. # 编辑/etc/vnstat.conf 替换,重启vnstat服务(本脚本自动能自动修改网卡名)
  6. # sed -i "s/eth0/ens3/g" /etc/vnstat.conf
  7. # systemctl restart vnstat
  8. # crontab -e 修改定时任务, 添加运行脚本
  9. # # netlog.sh 定时执行转html脚本,每10分钟一次,为了修改方便和多台机器用,直接到github更新
  10. # */10 * * * * wget -qO- git.io/fxxlb | bash
  11. ######################################################
  12. # 输出网络流量日志到html
  13. output_html(){
  14. # html 写文件位置
  15. if [ ! -e '/etc/redhat-release' ]; then
  16. INDEX_HTML=/var/www/html/index.html
  17. mkdir -p /var/www/html/
  18. else
  19. INDEX_HTML=/usr/share/nginx/html/index.html
  20. fi
  21. echo '<!DOCTYPE html><meta charset=utf-8><pre>' > ${INDEX_HTML}
  22. top -b | head -5 >> ${INDEX_HTML}
  23. vnstat -u
  24. vnstat -m >> ${INDEX_HTML}
  25. vnstat -d >> ${INDEX_HTML}
  26. vnstat -h >> ${INDEX_HTML}
  27. echo ' ' >> ${INDEX_HTML}
  28. echo ' netlog.sh Source code: https://git.io/fxxlb' >> ${INDEX_HTML}
  29. }
  30. # 安装 vnstat 添加定期运行
  31. vnstat_install(){
  32. # 判断系统 安装软件
  33. if [ ! -e '/etc/redhat-release' ]; then
  34. # debian 系安装
  35. apt -y install vnstat nginx
  36. else
  37. # centos 系安装 vnstat nginx,如果web没法访问,需要关防火墙
  38. yum -y install vnstat nginx
  39. systemctl enable nginx
  40. systemctl restart nginx
  41. fi
  42. # vps网卡如果不是eth0,修改成实际网卡
  43. ni=$(ls /sys/class/net | awk {print} | grep -e eth. -e ens. -e venet.)
  44. if [ $ni != "eth0" ]; then
  45. sed -i "s/eth0/${ni}/g" /etc/vnstat.conf
  46. fi
  47. systemctl restart vnstat
  48. # 设置定时运行脚本
  49. crontab -l >> crontab.txt
  50. echo "*/10 * * * * wget -qO- git.io/fxxlb | bash" >> crontab.txt
  51. crontab crontab.txt
  52. sleep 2
  53. if [ ! -e '/etc/redhat-release' ]; then
  54. systemctl restart cron
  55. else
  56. systemctl restart crond
  57. fi
  58. rm -f crontab.txt
  59. }
  60. # 首次运行脚本需要安装
  61. if [ ! -f '/usr/bin/vnstat' ]; then
  62. vnstat_install
  63. fi
  64. # 输出网络流量信息到html文件
  65. output_html