J'essaie d'automatiser un processus d'un système d'approvisionnement en modems. Le code ci-dessous est ma tentative à l'exception d'un formulaire que je n'ai pas listé et qui passe les variables.
La partie DB du script fonctionne bien. Cependant, la partie telnet ne se rend pas dans le routeur comme je l'espérais. Quelqu'un peut-il m'éclairer sur ce point ? Si je sépare la partie telnet et l'exécute via cli, cela fonctionne.
<?
include("config.php");
//setting up db connection
try{
$dbh = new PDO("mysql:host=$host;dbname=$db", $mun, $mpass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$stmt = $dbh->prepare("INSERT INTO cust_info(firstname, lastname, staddr, city, phone, acct, ipaddr, hwaddr, cliid, bootfile)
VALUES (:firstname, :lastname, :staddr, :city, :phone, :acct, :ipaddr, :hwaddr, :cliid, :bootfile)");
$stmt->bindParam(':firstname', $_POST['firstname']);
$stmt->bindParam(':lastname', $_POST['lastname']);
$stmt->bindParam(':staddr', $_POST['staddr']);
$stmt->bindParam(':city', $_POST['city']);
$stmt->bindParam(':phone', $_POST['phone']);
$stmt->bindParam(':acct', $_POST['acct']);
$stmt->bindParam(':ipaddr', $_POST['ipaddr']);
$stmt->bindParam(':hwaddr', $_POST['hwaddr']);
$stmt->bindParam(':cliid', $_POST['cliid']);
$stmt->bindParam(':bootfile', $_POST['bootfile']);
$stmt->execute();
echo "done";
}
catch(PDOException $e)
{
echo 'failed: ' . $e->getMessage();
}
$bootfile = $_POST['bootfile'];
$cliid = $_POST['cliid'];
$ipaddr = $_POST['ipaddr'];
$hwaddr = $_POST['hwaddr'];
//Process data from form into cmts
$connection = fsockopen($router_ip, $port, $errno, $errstr, $timeout);
if(!$connection)
{
echo "Connection failed\n";
exit();
}
else
{
fputs($connection, "$username\r");
fputs($connection, "$password\r");
// using term length 0 to keep pause or more prompt from eating input
fputs($connection, "term length 0\r");
fputs($connection, "enable\r");
fputs($connection, "$password\r");
fputs($connection, "clear ip dhcp binding $ipaddr\r");
fputs($connection, "configure terminal\r");
fputs($connection, "ip dhcp pool $cliid\r");
fputs($connection, "host $ipaddr 255.255.255.0\r");
fputs($connection, "client-identifier $cliid\r");
fputs($connection, "bootfile $bootfile\r");
fputs($connection, "exit\r\n");
fputs($connection, "exit\r\n");
fputs($connection, "clear cable modem 172.16.16.248 reset\r\n");
fputs($connection, "show cable modem remote | inc $hwaddr\r");
fputs($connection, "exit\r");
}
{
fgets($connection, 128);
}
stream_set_timeout($connection, 2);
$timeoutCount = 0;
while (!feof($connection))
{
$content = fgets($connection, 128);
echo $content."<br>";
}
$info = stream_get_meta_data($connection);
if ($info['timed_out'])
{
// If timeout of connection info has got a value, the router not returning a output.
$timeoutCount++;
// We want to count, how many times repeating.
}
if ($timeoutCount >2)
{
// If repeating more than 2 times,
break;
// the connection terminating..
}
?>