Connect to Cisco Device

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Connect to Cisco Device

Postby egami » Thu Apr 14, 2011 11:26 am

A little script I have put together to connect to Cisco devices. I guess it could be used to connect to any device, but I wrote this specifically to connect to them and pull configs, check signal levels, errors, etc.

Code: Select all

function connect($host,$cmd,$username,$password) {

    
$host "$host.bloombroadband.com";
    
$port 23;
    
$timeout 10;

    
$usenet fsockopen($host,$port,$errno,$timeout);

    if (
$usenet) {
        
fputs($usenet"$username\r\n");
        
fputs($usenet"$password\r\n");
        
fputs($usenet"terminal length 0\r\n");
        
fputs($usenet"!BEGIN!\r\n");
        
fputs($usenet"$cmd\r\n");
        
fputs($usenet"!END!\r\n");
        
fputs($usenet"exit\r\n");
        while (!
feof($usenet)) {
            
$lame[] = fgets($usenet256)."\r";
        }
    }
    if (
is_array($lame)) {
        foreach(
$lame as $key => $line) {
            
$line preg_replace('/[\r\n]/','',$line);
            
$lame[$key] = $line;
        }
        return 
$lame;
    } else {
        
$out['error'] = $errno;
        
$out['mesg']    = "ERROR: Dont know why.";
        return 
$out;
    }
}
 


it would be called as such..

$config_array = connect('router.mydomain.com','show running-config','username','password');

At this point, you have a cludge and a mess to deal with.. you would have to pass it through a filter, but this is why I put !BEGIN! and !END!

so, you could inevitably clean up your configuration to store it into a db like so..

Code: Select all

foreach($config_array as $key => $line) { 
  if (
$record !== TRUE) { 
    if (
preg_match('/\!BEGIN\!/',$line)) { 
      
$record TRUE;
    } 
  }
  if (
preg_match('/\!END\!/',$line)) { 
    
$record FALSE;
  }

  if (
$record === TRUE) { 
    
$array[] = $line;
  }
}
unset(
$array['0']); // this will be the command you asked for
 

Return to “Your Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest