3

我正在尝试自动化调制解调器配置系统的过程。下面的代码是我的尝试,除了我没有列出的传递变量的表单。

脚本的数据库部分运行良好。但是 telnet 部分并没有像我希望的那样进入路由器,因为我希望有人能对此有所了解。如果我打破 telnet 部分并通过 cli 运行它,它就可以工作。

<?

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..
 }

 ?> 
4

1 回答 1

0

布拉德给了我获得所需结果所需的提示,再次感谢

于 2013-05-17T03:36:01.263 回答