HP Procurve – Backup Script (PHP)
Hallo zusammen,
ich habe kürzlich aus „Langeweile“ mal eine PHP-Klasse zusammengeschrieben, welche ein Backup der Konfiguration eines HP-Switches macht.
Wichtig: Für diese Klasse muss „libssh2“ installiert sein!
Anbei das Script:
<?php
//
//
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//
// HP PROCURVE - BACKUP SCRIPT
//
// This class fetches configuration from HP Procurve Switches
// and is able to store result in database or something like that.
//
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//
// Initial Author:
// Simon Brecht
// simon(a)brecht.email
//
//////////////////////////////////////////////////////////////////
//
// CHANGELOG:
//
// v0.1
// 03.09.2016, Brecht - Initial Script
//
//////////////////////////////////////////////////////////////////
//
// BUGLIST:
//
//
//
//////////////////////////////////////////////////////////////////
class SCPBackup
{
// Internal Variable to set Host
private $host = '127.0.0.1';
// Internal Variable to set Config
public $config = "";
// Set Username as String
public $_scp_user = "admin";
// Set-up multiple passwords in array to try
public $_scp_pass = array("password1", "password2", "password3");
//
// Construct with $host like IPADDR:
// $z = new SCPBackup("1.2.3.4");
//
function __construct($host)
{
$this->host = $host;
}
//
// Run with or without Filename like running-config OR startup-config:
// $z->run(); # fetches running-config
// $z->run("startup-config"); # fetches startup-config
//
// print($z->config); # prints fetched config
//
public function run($filename = 'running-config')
{
$_scp_pass_count = 1;
foreach($this->_scp_pass as $pass)
{
$_scp_host = $this->host;
$_scp_conn = ssh2_connect($this->host, 22);
$_scp_auth = ssh2_auth_password($_scp_conn, $this->_scp_user, $pass);
print("Versuche SSH-Verbindung mit Parametern, Host: ".$_scp_host.", User: ".$this->_scp_user.", Pass: ".$_scp_pass_count);
if($_scp_auth)
{
print("Verbindung erfolgreich, lese Konfiguration...");
$this->config = "";
$_scp_sftp = ssh2_sftp($_scp_conn);
$_scp_config = @fopen("ssh2.sftp://{$_scp_sftp}/cfg/${filename}", 'r');
clearstatcache();
while(!feof($_scp_config))
{
if(!$this->config .= fread($_scp_config, 1024))
{
print("Kann Konfiguration nicht lesen, Verbindung abgebrochen!");
$this->config = "";
break;
}
}
fclose($_scp_config);
if($this->config!="")
{
print($this->config);
print("Backup der Konfiguration erfolgreich, Host: ".$_scp_host);
break;
}
else
{
print("Auf die Konfiguration konnte nicht zugegriffen werden!!! Host: ".$_scp_host.", User: ".$this->_scp_user);
}
}
$_scp_pass_count++;
}
}
}
?>


