Board index   FAQ   Search  
Register  Login
Board index PHP Your Scripts

Connect to Cisco Device

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

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 "$";
$port 23;
$timeout 10;

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

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

it would be called as such..

$config_array = connect('','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;
$array['0']); // this will be the command you asked for
User avatar
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Return to Your Scripts

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.