欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

一个用于网络的工具函数库

程序员文章站 2022-05-14 12:09:12
<?php

/*
php net toolpack v0.1 08.05.2000,
by waddler(@netlife.fi)
phpnettoolpack.sourceforge.net
to be distributed under gnu gpl
*/

// whois(hostname [,username, [port]])
function whois ($a_server, $a_query="", $a_port=43) {
$sock = fsockopen($a_server, $a_port, &$errno, &$errstr, 10);
if (!$sock)
{
echo "$errstr ($errno)<br>n";
} else {
fputs($sock, "$a_queryrn");
while(!feof($sock))
{
$buf = fgets($sock,128);
if (ereg( "whois server:", $buf))
{
$a_server = str_replace( "whois server: ", "", $buf);
$a_server = trim($a_server);
}
}
fclose($sock);

if ($a_server)
{
print "<b>$a_query is registered at $a_server:</b><br>";
$sock = fsockopen($a_server, 43, &$errno, &$errstr, 10);
if(!$sock)
{
echo "could not open connection to $a_server on port $a_port.n";
echo "$errstr ($errno)<br>n";
} else {
fputs($sock, "$a_queryrn");
while(!feof($sock))
{
echo fgets($sock,128);
}
fclose($sock);
}
} else {
echo "<b>$a_query was not found.</b><br>";
}
}
}


// finger(hostname [,username, [port]])
function finger ($a_server, $a_query="", $a_port=79) {
$sock=fsockopen($a_server,$a_port, &$errno, &$errstr, 10);
if (!$sock)
{
$ret_str = "$errstr ($errno)<br>n";
} else {
fputs($sock,"$a_queryn");
while (!feof($sock)) { $ret_str .= fgets($sock,128); }
fclose($sock);
}
echo $ret_str;
return $ret_str;
}


// traceroute(hostname)
function traceroute ($a_query) {
exec("traceroute $a_query",$ret_strs);
$str_count = count($ret_strs);
for ($count=0; $count < $str_count; $count++)
print "$count/$str_count".$ret_strs[$count]."n";
}


// -----------------------------------------------------------


$app_name = "php net toolpack";
$app_version = "0.1";

$tools = array(
"finger" => "finger",
"traceroute" => "traceroute",
"whois" => "whois?"
);

// when included inside <select name="tool"> on a html file ..
if ($tool=="listtools")
{
while (list($key, $val) = each($tools)) {
print " <option value="".$key."">".$val."</option>n";
}
exit;
}

// print appropriate html header
print "<html>";
if ($tool)
{
print "<head><title>".$tool." for ".$query."</title></head>n";
print "<body>n<h3>".$tool." for ".$query." ..</h3>n";
} else {
print "<head><title>".$app_name."</title></head>n";
print "<body>n<h3>".$app_name."</h3>n";
}

// check what tool they want to use and do what is necessary
switch($tool) {
case "finger":
if ($query)
{
print "<pre>n";
finger($server, $query);
print "</pre>";
} else {
?>

<form action="<?php echo($php_self. "?tool=".$tool); ?>" method="post">
server : <input type="text" name="server" value="localhost"> <br>
query : <input type="text" name="query" size="40" maxlength="100"> <br>
<input type="submit" value="finger">
</form>

<?php
}
break;

case "traceroute":
if ($query)
{
print "<pre>n";
traceroute($query);
print "</pre>";
} else {
?>

<form action="<?php echo($php_self. "?tool=".$tool); ?>" method="post">
query : <input type="text" name="query" size="40" maxlength="100"> <br>
<input type="submit" value="trace route">
</form>

<?php
}
break;


case "whois":
if ($query)
{
print "<pre>n";
whois($server,$query);
print "</pre>";
} else {
?>  

<!-- <ul>
to look up a nic handle, host name, or registrant,
use one of the keywords below:<br>
<li>to search by nic handle (or contact), type "handle wa3509"</li><br>
<li>to search by name, type "name lastname, firstname" </li><br>
<li>to search by company name, type "name the sample corporation" </li><br>
<li>to search by domain name, type "example.com" </li><br>
<li>to search by ip address, type "host 121.23.2.7" </li><br>
<li>to search by host or nameserver name, type "host ns1.worldnic.com" </li><br>
(examples are from networksolutions.com)
</ul> -->

<form action="<?php echo($php_self. "?tool=".$tool); ?>" method="post">
this will find .com, .org, and .net domains<br>
server : <input type="text" name="server" value="rs.internic.net"> <br>
query : <input type="text" name="query" size="40" maxlength="100"> <br>
<input type="submit" value="<?php echo $tools[$tool]; ?>">
</form>

<?php
}
break;

default:
print "<ul>currently supported tools are:n";
while (list($key, $val) = each($tools)) {
echo "<li><a href="".$php_self."?tool=".$key."">".$val."</a></li>n";
}
print "</ul>n";
break;
}

print "n<hr><small>".$app_name." v".$app_version."</small>n";
print "<body>n</html>";

?>