1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
<?
function printError ($message)
{
echo "<font color=red><code><strong>" . $message . "</strong></code></font><br>\n";
}
function safeOutput ($string)
{
return htmlentities (substr ($string, 0, 50));
}
//Exécution sur la base, Table_Switch de requêtes pour récupérer les infos des switch
$Host_Name = "*";
$User_Login = "*";
$User_Password = "*";
mysql_connect("$Host_Name", "$User_Login", "$User_Password")or die ("Impossible de se connecter");
mysql_select_db ("raps")or die ("Impossible d'accéder à la base de données");
function printSwitchList ($switch, $type)
// Préparation du select
echo '<select name="Nom_Switch">';
// Préparation de la requête
$query = 'SELECT * from Table_Switch';
list ($ID, $attribute) = each ($switch)
if (strcmp ($ID, "Nom_Switch") && !empty($attribute["Adresse_IP_Switch"]))
// Exécution de la requête
$result = mysql_query($query) or die($query . "-" . mysql_error());
while ($tab = mysql_fetch_array($result))
{
echo "<option value=".$tab['Nom_Switch'].'">'.$tab['Nom_Switch'];
}
echo '</select>';
//mysql_close();
//exécution sur la base de requétes pour récupérer les commandes telnet à éxécuter sur les switch.
function printRequestList ($request, $type)
{
while (list($id, $attribute) = each ($request))
if (!empty ($attribute["command"]) && !empty ($attribute["handler"]) && isset ($attribute["argc"]))
{
if ($type == "radio") echo "<input type=radio name=requestid value={$id}";
if ($_REQUEST["requestid"] == $id)
{
if ($type == "radio") echo " checked=on";
}
echo ">";
echo $attribute["title"] ? $attribute["title"] : $attribute["command"];
if ($type == "radio") echo "</input><br>\n";
}
echo "</{$type}>\n";
}
function execPreviousRequest ($switch, $request)
{
if (!isset($_REQUEST["Nom_Switch"])) return;
$Nom_Switch = $_REQUEST["Nom_Switch"];
if (!isset ($switch[$Nom_Switch]["Adresse_IP_Switch"])) return;
if (!isset($_REQUEST["requestid"])) return;
$requestid = $_REQUEST["requestid"];
if (!isset ($request[$requestid]["argc"])) return;
$handler = $request[$requestid]["handler"];
if (empty ($handler) || strpos ($switch[$Nom_Switch]["services"], $handler) === false)
{
printError ("Cette requête n'est pas autorisé par l'administrateur.");
return;
}
if ($request[$requestid]["argc"] > 0)
{
if (trim ($_REQUEST["argument"]) == '')
{
$switch_defined = isset ($switch[$Nom_Switch]["ignore_argc"]);
$switch_permits = $switch[$Nom_Switch]["ignore_argc"] == 1;
$default_defined = isset ($switch["default"]["ignore_argc"]);
$default_permits = $switch["default"]["ignore_argc"] == 1;
$final_permits =
(!$switch_defined && $default_defined && $default_permits) ||
($switch_defined && $switch_permits);
if (!$final_permits)
{
printError ("Full table view is denied on this switch");
return;
}
}
else $argument = trim ($_REQUEST["argument"]);
}
// All Ok, prepare to connect.
$address = $switch[$Nom_Switch]["Adresse_IP_Switch"];
if (!empty ($switch[$Nom_Switch][$handler . "_port"]))
$port = $switch[$Nom_Switch][$handler . "_port"];
else
$port = $switch["default"][$handler . "_port"];
if (!empty ($switch[$Nom_Switch][$handler . "_password"]))
$password = $switch[$Nom_Switch][$handler . "_password"];
elseif (!empty ($switch[$Nom_Switch]["password"]))
$password = $switch[$Nom_Switch]["password"];
else
$password = $switch["default"]["password"];
$command = $request[$requestid]["command"] . (!empty ($argument) ? (" " . safeOutput ($argument)) : "");
global $socket_timeout;
$link = fsockopen ($address, $port, $errno, $errstr, $socket_timeout);
if (!$link)
{
printError ("Erreur de connection au switch");
return;
}
socket_set_timeout ($link, $socket_timeout);
$username = $switch[$Nom_Switch]["username"];
if (!empty ($username)) fputs ($link, "{$username}\n");
fputs ($link, "{$password}\nterminal length 0\n{$command}\n");
// let daemon print bulk of records uninterrupted
if (empty ($argument) && $request[$requestid]["argc"] > 0) sleep (2);
fputs ($link, "quit\n");
echo "<pre>\n";
// Skip text up to the line following out command.
while (!feof ($link)
&& (strpos (fgets ($link, 1024), $command) === FALSE));
// Skip everything up to the 'quit' command.
while (!feof ($link)
&& (strpos (($buf = fgets ($link, 1024)), "quit") === FALSE))
{
echo $buf;
}
echo "</pre>\n";
fclose ($link);
}
mysql_close();
?> |
Partager