Netgate Discussion Forum
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Search
    • Register
    • Login

    Using cron to disconnect all users at midnight

    Scheduled Pinned Locked Moved Captive Portal
    4 Posts 2 Posters 783 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • J
      jeebsion 0
      last edited by

      Hi all,

      Is there a way to issue a command (perhaps wrapped via php) to disconnect all captive portal connected users at midnight via cron. Has anyone done this before?

      I came through this, found it via google dated back in 2015:

      require("captiveportal.inc");
      
      if (!is_array($config['captiveportal'])) {
      	$config['captiveportal'] = array();
      }
      
      $a_cp =& $config['captiveportal'];
      foreach ($a_cp as $captiveportalzone) {
      	// set $cp_zone so the correct database will use used
      	$cpzone = $captiveportalzone['zone'];
      	// also surface the global $cpzoneid
      	$cpzoneid = $captiveportalzone['zoneid'];
      	// Read the corresponding database
      	$cpdb = captiveportal_read_db();
      	foreach ($cpdb as $cpent) {
      		captiveportal_disconnect_client($cpent[5]);
      	}
      	unset($cpdb);
      }
      unset($a_cp);
      

      cat it to /root/test.php and run it like so:

      /usr/local/bin/php -q /root/test.php
      

      However to no avail. Would appreciate any pointers at all!

      GertjanG 1 Reply Last reply Reply Quote 0
      • GertjanG
        Gertjan @jeebsion 0
        last edited by

        @jeebsion-0

        Dono who wrote that code snippet, but it's incomplete ;)

        Here you have the entire file :

        #!/usr/local/bin/php -q
        <?php
        	/* Disconnect all clients on all captive portal instances */
        
        	require_once("/etc/inc/util.inc");
        	require_once("/etc/inc/functions.inc");
        	require_once("/etc/inc/captiveportal.inc");
        
        	global $g, $config, $cpzone, $cpzoneid;
        
        	/* Are there any portals  ? */
        	if (is_array($config['captiveportal'])) {
        		/* For every portal (cpzone), do */
        		foreach ($config['captiveportal'] as $cpkey => $cp)
        			/* Sanity check */
        			if (is_array($config['captiveportal'][$cpkey])) 
        				/* Is zone enabled ? */
        				if (array_key_exists('enable', $config['captiveportal'][$cpkey])) {
        					$cpzone = $cpkey;
        					$cpzoneid = $cp['zoneid'];
        					captiveportal_disconnect_all();
        				}
        	}
        ?>
        

        The file is updated, as portal 'code' evolved somewhat over the last 7 years.
        Global variables have to be setup correctly.
        The starting <?php is important.
        And instead of the function captiveportal_disconnect_client() we have now captiveportal_disconnect_all() that removes all users from a portal instance.

        I tested it.
        Thanks ;)
        All my connected users (a hotel) were thrown of the portal.

        No "help me" PM's please. Use the forum, the community will thank you.
        Edit : and where are the logs ??

        J 2 Replies Last reply Reply Quote 1
        • J
          jeebsion 0 @Gertjan
          last edited by jeebsion 0

          @gertjan Oh my .. sorry I haven't been able to get to this channel for quite some time .. I'll try this out and report back .. thank you, sir!

          PS: I don't quite understand the global part .. should be descended from captiveportal.inc yes?

          1 Reply Last reply Reply Quote 0
          • J
            jeebsion 0 @Gertjan
            last edited by

            @gertjan I tried the code and it worked! .. and dropped it directly into cron .. thank you, sir!

            1 Reply Last reply Reply Quote 0
            • First post
              Last post
            Copyright 2025 Rubicon Communications LLC (Netgate). All rights reserved.