Jump to content

creating WLC users via custom made PHP scipt which uses SNMP


Recommended Posts

Posted

Hello,

so basically i am not very experienced in PHP and this script is becoming the biggest challenge for me, probably because I lack of knowledge in PHP, so I am asking for some help and explanations.

Explanation of what I am doing:

I am trying to create a php script which gonna have a html form to create WIFI user on cisco WLC.

I know that WLC it self got this kind of thing, but the reason I want to do it is that I dont want to allow any staff members to connect to WLC even knowing that they are going to have limited rights and so on.

So I came-up with this idea that I am going to make this PHP script and HTML form which uses SNMP to create wifi user accounts, that's how I am planning to bypass using WLC interface.

 

So the first thing which drives me nuts is that I need to convert entered time from HTML form to seconds, this form has days, hours, minutes.

See attachment.

Basically I want to "echo" entered time period in seconds at the bottom of php script but I dont know how.

 

I know that this code is messy and ugly, but the idea itself covers that :)

 

Code looks like that:

           Lifetime: 
           
             
               
                   
                       days  
               
               
                   
                       hours  
               
               
                   
                       mins  
               
               
           
         

 

overall code looks like that(it is still missing PHP_SNMP functions):

 




</pre><form method="POST" action="index.php">
    bgColor="#ffffcc" align=center>
Host: 
        
wlc-1
wlc-2
Username:   
        
Password:   
    

           Lifetime: 
           
             
               
                   
                       days  
               
               
                   
                       hours  
               
               
                   
                       mins  
               
               
           
         
Vlan: 
    
ISM Studies

    
   



<

 

This part of PHP script converts (username, password) input for HTML table to ascii decimal numbers that's because SNMP requires that.

 

$user = $_POST['user']; 
$pass = $_POST['password'];
$host = $_POST['host'];
$arr = array($user);
$arr2 = array($pass);
foreach($arr as $value)
   {
       $index = 0;
       while($index < strlen($value))
   {
$ascii .= '.' . ord($value[$index]);
$dot_separated = implode(".", $value)."
";
$index++;
   }
echo $ascii."
";
   }
foreach($arr2 as $value)
   {
       $index = 0;
       while($index < strlen($value))
   {
$ascii2 .= '.' . ord($value[$index]);
$dot_separated = implode(".", $value)."
";
$index++; 
   }
echo $ascii2."
";
   }
echo $host."
";

?>

lifetime.png

Posted

Can you just pass the input values to a php function to return the value in seconds?

is this what you're looking to achieve?

the user enters days hours mins and then you want this returned as how many seconds this is?

 

Its early i need more coffee!

Posted

I assume that yes.

Lets say that user enter 10 days 0 hours 0 minutes.

And i want to use that time in my php script as seconds.

so 10 days = 864000 seconds.

I would really appreciate you help.

Posted (edited)

this little function i've just coded and tested, it works for in my test environment:

 

function convert_to_sec($days,$hours,$mins) {
$sec = 0;
   if(is_numeric($days)) {
       $sec += 86400*$days;
   } 
   if(is_numeric($hours)) {
       $sec += 3600*$hours;
   } 
   if(is_numeric($mins)) {
       $sec += 60*$mins;
   }
return $sec;    
}
?>

 

so if you call the function and then pass it the days, hours, mins to be converted it will return the correct value in seconds from my testing.

 

This is very simple though, no error trapping and probably a better way to do this but its what I've quickly come up with. Will look at a better way to do it shortly.

Edited by ThomL
Posted (edited)

This should work with your current html page and provide some echos to show it is working:

$user = $_POST['user']; 
$pass = $_POST['password'];
$host = $_POST['host'];
$days = $_POST['lifetime_days']; //retrieve days from user submission
$hours = $_POST['lifetime_hours']; //retrieve hours from user submission
$mins = $_POST['lifetime_mins']; //retrieve mins from user submission
$sec = 0; //set seconds - stops loops from throwing error but also used for validation
$arr = array($user);
$arr2 = array($pass);
foreach($arr as $value)
   {
       $index = 0;
       while($index < strlen($value))
   {
$ascii .= '.' . ord($value[$index]);
$dot_separated = implode(".", $value)."
";
$index++;
   }
echo $ascii."
";
   }
foreach($arr2 as $value)
   {
       $index = 0;
       while($index < strlen($value))
   {
$ascii2 .= '.' . ord($value[$index]);
$dot_separated = implode(".", $value)."
";
$index++; 
   }
echo $ascii2."
";
   }
echo $host."
";
if(is_numeric($days)) { //if users submitted days...
       $sec += 86400*$days; //convert users submitted value from days to seconds, add resulting value to current total of seconds 
} 
if(is_numeric($hours)) { //if users submitted hours...
   $sec += 3600*$hours; //convert users submitted value from days to seconds, add resulting value to current total of seconds 
} 
if(is_numeric($mins)) { //if users submitted mins...
   $sec += 60*$mins; //convert users submitted value from days to seconds, add resulting value to current total of seconds 
}
if($sec>0){ //If values received from html and converted...
echo $sec."
";
} else { //if no values were received from html...
 echo "User did not input a time value
";
}
?>

 

The conditions on the if statements only work with numeric vales so you may want to add some validation for this.

Edited by ThomL
  • Thanks 1
Posted (edited)

You're welcome :yo:

 

This is a slightly optimised version of the above:

 

$user = $_POST['user']; 
$pass = $_POST['password'];
$host = $_POST['host'];
$days = $_POST['lifetime_days']; //retrieve days from user submission
$hours = $_POST['lifetime_hours']; //retrieve hours from user submission
$mins = $_POST['lifetime_mins']; //retrieve mins from user submission
$sec = 0; //set seconds - stops loops from throwing error but also used for validation
$arr = array($user);
$arr2 = array($pass);
foreach($arr as $value)
   {
       $index = 0;
       while($index < strlen($value))
   {
$ascii .= '.' . ord($value[$index]);
$dot_separated = implode(".", $value)."
";
$index++;
   }
echo $ascii."
";
   }
foreach($arr2 as $value)
   {
       $index = 0;
       while($index < strlen($value))
   {
$ascii2 .= '.' . ord($value[$index]);
$dot_separated = implode(".", $value)."
";
$index++; 
   }
echo $ascii2."
";
   }
echo $host."
";
if(is_numeric($days) || is_numeric($hours) || is_numeric($mins)) { //checks foruser submitted numeric values...
   $sec = ((86400*$days)+(3600*$hours)+(60*$mins)); // conversion to seconds
   echo $sec."
";  //echo used to display converted data
} else { //if invalid or no user submitted numeric values...
   echo "User did not input a time value
"; //alerts invalid or no user submitted numeric values
}
?>

Edited by ThomL

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...