PPTP VPN之日志功能实现【记录登陆用户、登陆时间、登陆IP、在线时间、流量统计等信息】

 

其实以前也搞过PPTP VPN的配置,但是当时都是存在于建立,很多细节的方面并没有做好。今天来搞下这个日志系统吧,主要实现是通过ip-up 和ip-down两个脚本来实现的。这里说下原理吧,原理是通过pptp建立连接的时候都会执行ip-up,然后断线会执行ip-down。首先看看man pppd里面的一些内容:

  1. SCRIPTS 
  2.        Pppd  invokes  scripts at various stages in its processing which can be 
  3.        used to perform site-specific ancillary processing.  These scripts  are 
  4.        usually  shell  scripts,  but  could  be executable code files instead. 
  5.        Pppd does not wait for the scripts to finish (except for the  ip-pre-up 
  6.        script).  The scripts are executed as root (with the real and effective 
  7.        user-id set to 0), so that they can do things such  as  update  routing 
  8.        tables  or  run  privileged  daemons.   Be careful that the contents of 
  9.        these scripts do not compromise your system's security.  Pppd runs  the 
  10.        scripts  with standard input, output and error redirected to /dev/null, 
  11.        and with an environment that is empty except for some environment vari- 
  12.        ables  that give information about the link.  The environment variables 
  13.        that pppd sets are: 
  14.  
  15.        DEVICE The name of the serial tty device being used. 
  16.  
  17.        IFNAME The name of the network interface being used. 
  18.  
  19.        IPLOCAL 
  20.               The IP address for the local end of the link.  This is only  set 
  21.               when IPCP has come up. 
  22.  
  23.        IPREMOTE 
  24.               The IP address for the remote end of the link.  This is only set 
  25.               when IPCP has come up. 
  26.  
  27.        PEERNAME 
  28.               The authenticated name of the peer.  This is  only  set  if  the 
  29.               peer authenticates itself. 
  30.  
  31.        SPEED  The baud rate of the tty device. 
  32.  
  33.        ORIG_UID 
  34.               The real user-id of the user who invoked pppd. 
  35.  
  36.        PPPLOGNAME 
  37.               The  username  of  the  real  user-id that invoked pppd. This is 
  38.               always set. 
  39.  
  40.        For the ip-down and auth-down scripts, pppd  also  sets  the  following 
  41.        variables giving statistics for the connection: 
  42.  
  43.        CONNECT_TIME 
  44.               The  number  of  seconds  from  when the PPP negotiation started 
  45.               until the connection was terminated. 
  46.  
  47.        BYTES_SENT 
  48.               The number of bytes sent (at the level of the serial port)  dur- 
  49.               ing the connection. 
  50.  
  51.        BYTES_RCVD 
  52.               The  number  of bytes received (at the level of the serial port) 
  53.               during the connection. 
  54.  
  55.        LINKNAME 
  56.               The logical name of the link, set with the linkname option. 
  57.  
  58.        CALL_FILE 
  59.               The value of the call option. 
  60.  
  61.        DNS1   If the peer supplies DNS server addresses, this variable is  set 
  62.               to the first DNS server address supplied. 
  63.  
  64.        DNS2   If  the peer supplies DNS server addresses, this variable is set 
  65.               to the second DNS server address supplied. 
  66.  
  67.        Pppd invokes the following scripts, if they exist.  It is not an  error 
  68.        if they don't exist. 
  69.  
  70.        /etc/ppp/auth-up 
  71.               A  program  or  script which is executed after the remote system 
  72.               successfully authenticates itself.   It  is  executed  with  the 
  73.               parameters 
  74.  
  75.               interface-name peer-name user-name tty-device speed 
  76.  
  77.               Note  that  this  script  is  not  executed  if the peer doesn't 
  78.               authenticate itself, for example when the noauth option is used. 
  79.  
  80.        /etc/ppp/auth-down 
  81.               A program or script which is executed when the link  goes  down, 
  82.               if  /etc/ppp/auth-up was previously executed.  It is executed in 
  83.               the same manner with the same parameters as /etc/ppp/auth-up. 
  84.  
  85.        /etc/ppp/ip-pre-up 
  86.               A program or script which is executed just before the  ppp  net- 
  87.               work  interface  is  brought  up.   It is executed with the same 
  88.               parameters as the ip-up  script  (below).   At  this  point  the 
  89.               interface  exists  and  has  IP  addresses assigned but is still 
  90.               down.  This can be used to add  firewall  rules  before  any  IP 
  91.               traffic can pass through the interface.  Pppd will wait for this 
  92.               script to finish before  bringing  the  interface  up,  so  this 
  93.               script should run quickly. 
  94.  
  95.        /etc/ppp/ip-up 
  96.               A program or script which is executed when the link is available 
  97.               for sending and receiving IP packets (that  is,  IPCP  has  come 
  98.               up).  It is executed with the parameters 
  99.  
  100.               interface-name       tty-device      speed      local-IP-address 
  101.               remote-IP-address ipparam 
  102.  
  103.        /etc/ppp/ip-down 
  104.               A program or script which is executed when the link is no longer 
  105.               available for sending and receiving IP packets.  This script can 
  106.               be used for  undoing  the  effects  of  the  /etc/ppp/ip-up  and 
  107.               /etc/ppp/ip-pre-up  scripts.   It  is invoked in the same manner 
  108.               and with the same parameters as the ip-up script. 
  109.  
  110.        /etc/ppp/ipv6-up 
  111.               Like /etc/ppp/ip-up, except that it is executed when the link is 
  112.               available for sending and receiving IPv6 packets. It is executed 
  113.               with the parameters 
  114.  
  115.               interface-name   tty-device    speed    local-link-local-address 
  116.               remote-link-local-address ipparam 
  117.  
  118.        /etc/ppp/ipv6-down 
  119.               Similar  to /etc/ppp/ip-down, but it is executed when IPv6 pack- 
  120.               ets can no longer be transmitted on the  link.  It  is  executed 
  121.               with the same parameters as the ipv6-up script. 
  122.  
  123.        /etc/ppp/ipx-up 
  124.               A program or script which is executed when the link is available 
  125.               for sending and receiving IPX packets (that is, IPXCP  has  come 
  126.               up).  It is executed with the parameters 
  127.  
  128.               interface-name       tty-device       speed       network-number 
  129.               local-IPX-node-address  remote-IPX-node-address  local-IPX-rout- 
  130.               ing-protocol  remote-IPX-routing-protocol  local-IPX-router-name 
  131.               remote-IPX-router-name ipparam pppd-pid 
  132.  
  133.               The local-IPX-routing-protocol  and  remote-IPX-routing-protocol 
  134.               field may be one of the following: 
  135.  
  136.               NONE      to indicate that there is no routing protocol 
  137.               RIP       to indicate that RIP/SAP should be used 
  138.               NLSP      to indicate that Novell NLSP should be used 
  139.               RIP NLSP  to indicate that both RIP/SAP and NLSP should be used 
  140.  
  141.        /etc/ppp/ipx-down 
  142.               A program or script which is executed when the link is no longer 
  143.               available for sending and receiving IPX  packets.   This  script 
  144.               can  be  used  for  undoing  the  effects of the /etc/ppp/ipx-up 
  145.               script.  It is invoked in the same  manner  and  with  the  same 
  146.               parameters as the ipx-up script. 

这就是ppp的脚本内容,里面当然有变量咯,这些变量就是我们需要的。

好了,那么开始进行日志整理吧,首先是ip-up

  1. vim /etc/ppp/ip-up 
  2. #!/bin/sh 
  3. # This script is run by the pppd after the link is established. 
  4. # It uses run-parts to run scripts in /etc/ppp/ip-up.d, so to add routes, 
  5. # set IP address, run the mailq etc. you should create script(s) there. 
  6. # Be aware that other packages may include /etc/ppp/ip-up.d scripts (named 
  7. # after that package), so choose local script names with that in mind. 
  8. # This script is called with the following arguments: 
  9. #    Arg  Name                          Example 
  10. #    $1   Interface name                ppp0 
  11. #    $2   The tty                       ttyS1 
  12. #    $3   The link speed                38400 
  13. #    $4   Local IP number               12.34.56.78 
  14. #    $5   Peer  IP number               12.34.56.99 
  15. #    $6   Optional ``ipparam'' value    foo 
  16.  
  17. # The  environment is cleared before executing this script 
  18. # so the path must be reset 
  19. PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 
  20. export PATH 
  21.  
  22. # These variables are for the use of the scripts run by run-parts 
  23. PPP_IFACE="$1" 
  24. PPP_TTY="$2" 
  25. PPP_SPEED="$3" 
  26. PPP_LOCAL="$4" 
  27. PPP_REMOTE="$5" 
  28. PPP_IPPARAM="$6" 
  29. export PPP_IFACE PPP_TTY PPP_SPEED PPP_LOCAL PPP_REMOTE PPP_IPPARAM 
  30.  
  31. # as an additional convenience, $PPP_TTYNAME is set to the tty name, 
  32. # stripped of /dev/ (if present) for easier matching. 
  33. PPP_TTYNAME=`/usr/bin/basename "$2"` 
  34. export PPP_TTYNAME 
  35.  
  36. # If /var/log/ppp-ipupdown.log exists use it for logging. 
  37. if [ -e /var/log/ppp-ipupdown.log ]; then 
  38.   exec > /var/log/ppp-ipupdown.log 2>&1 
  39.   echo $0 $@ 
  40.   echo 
  41. fi 
  42.  
  43. # This script can be used to override the .d files supplied by other packages. 
  44. if [ -x /etc/ppp/ip-up.local ]; then 
  45.   exec /etc/ppp/ip-up.local "$@" 
  46. fi 
  47.  
  48. run-parts /etc/ppp/ip-up.d \ 
  49.   --arg="$1" --arg="$2" --arg="$3" --arg="$4" --arg="$5" --arg="$6" 
  50.  
  51. # if pon was called with the "quick" argument, stop pppd 
  52. if [ -e /var/run/ppp-quick ]; then 
  53.   rm /var/run/ppp-quick 
  54.   wait 
  55.   kill $PPPD_PID 
  56. fi 
  57.  
  58. /sbin/iptables -t nat -A POSTROUTING -s  172.16.36.0/24 -j SNAT --to-source "serverip" 这里设置每次登陆时候都设定一次路由转发,避免失效。 
  59.  
  60. echo "****************************************************" > /var/log/pptpd-${1}.log 将所有内容导入到pptpd-${1}.log,以防多用户登陆时候造成日志混乱 
  61. echo "username: $PEERNAME" >> /var/log/pptpd-${1}.log 
  62. echo "clientIP: $6" >> /var/log/pptpd-${1}.log 
  63. echo "device: $1" >> /var/log/pptpd-${1}.log 
  64. echo "vpnIP: $4" >> /var/log/pptpd-${1}.log 
  65. echo "assignIP: $5" >> /var/log/pptpd-${1}.log 
  66. echo "logintime: `date -d today +%F_%T`" >> /var/log/pptpd-${1}.log 

然后是ip-up的处理:

  1. vim /etc/ppp/ip-down 
  2. #!/bin/sh 
  3. # This script is run by the pppd _after_ the link is brought down. 
  4. # It uses run-parts to run scripts in /etc/ppp/ip-down.d, so to delete 
  5. # routes, unset IP addresses etc. you should create script(s) there. 
  6. # Be aware that other packages may include /etc/ppp/ip-down.d scripts (named 
  7. # after that package), so choose local script names with that in mind. 
  8. # This script is called with the following arguments: 
  9. #    Arg  Name                          Example 
  10. #    $1   Interface name                ppp0 
  11. #    $2   The tty                       ttyS1 
  12. #    $3   The link speed                38400 
  13. #    $4   Local IP number               12.34.56.78 
  14. #    $5   Peer  IP number               12.34.56.99 
  15. #    $6   Optional ``ipparam'' value    foo 
  16.  
  17. # The  environment is cleared before executing this script 
  18. # so the path must be reset 
  19. PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin 
  20. export PATH 
  21.  
  22. # These variables are for the use of the scripts run by run-parts 
  23. PPP_IFACE="$1" 
  24. PPP_TTY="$2" 
  25. PPP_SPEED="$3" 
  26. PPP_LOCAL="$4" 
  27. PPP_REMOTE="$5" 
  28. PPP_IPPARAM="$6" 
  29. export PPP_IFACE PPP_TTY PPP_SPEED PPP_LOCAL PPP_REMOTE PPP_IPPARAM 
  30.  
  31. # as an additional convenience, $PPP_TTYNAME is set to the tty name, 
  32. # stripped of /dev/ (if present) for easier matching. 
  33. PPP_TTYNAME=`/usr/bin/basename "$2"` 
  34. export PPP_TTYNAME 
  35.  
  36. # If /var/log/ppp-ipupdown.log exists use it for logging. 
  37. if [ -e /var/log/ppp-ipupdown.log ]; then 
  38.   exec >> /var/log/ppp-ipupdown.log 2>&1 
  39.   echo $0 $@ 
  40.   echo 
  41. fi 
  42.  
  43.  
  44. echo "downtime: `date -d today +%F_%T`" >> /var/log/pptpd-${1}.log 
  45. echo "bytes sent: $BYTES_SENT" >> /var/log/pptpd-${1}.log 
  46. echo "bytes received: $BYTES_RCVD" >> /var/log/pptpd-${1}.log 
  47. echo "connect time: $CONNECT_TIME" >> /var/log/pptpd-${1}.log 
  48. echo "****************************************************" >> /var/log/pptpd-${1}.log 
  49. cat /var/log/pptpd-${1}.log >> /var/log/pptpd.log
  50.  
  51.  
  52.  
  53. # This script can be used to override the .d files supplied by other packages. 
  54. if [ -x /etc/ppp/ip-down.local ]; then 
  55.   exec /etc/ppp/ip-down.local "$@" 
  56. fi 
  57.  
  58. run-parts /etc/ppp/ip-down.d \ 
  59.   --arg="$1" --arg="$2" --arg="$3" --arg="$4" --arg="$5" --arg="$6" 

最后就可以通过/var/log/pptpd.log文件进行查看PPTP VPN日志了。日志形式如下,需要更美观的自行修改代码:

  1. **************************************************** 
  2. username: nenew 
  3. clientIP: XXX.XXX.XXX.XXX 
  4. device: ppp0 
  5. vpnIP: 172.16.36.1 
  6. assignIP: 172.16.36.2 
  7. logintime: 2015-08-03_17:06:05 
  8. downtime: 2015-08-03_17:06:23 
  9. bytes sent: 164143 
  10. bytes received: 51606 
  11. connect time: 18 
  12. **************************************************** 
奶牛 | 2015年08月3日