Skip to content
Browse files

* Initial checkin of Net_Portscan class.

git-svn-id: http://svn.php.net/repository/pear/packages/Net_Portscan/trunk@62530 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 5988d6c commit ac14894b04b8cdaad2dc143922464435e9ad9e74 @mj mj committed Nov 15, 2001
Showing with 183 additions and 0 deletions.
  1. +123 −0 Portscan.php
  2. +3 −0 README
  3. +28 −0 package.xml
  4. +29 −0 test.php
View
123 Portscan.php
@@ -0,0 +1,123 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4: */
+// +----------------------------------------------------------------------+
+// | PHP version 4.0 |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group |
+// +----------------------------------------------------------------------+
+// | This source file is subject to version 2.0 of the PHP license, |
+// | that is bundled with this package in the file LICENSE, and is |
+// | available at through the world-wide-web at |
+// | http://www.php.net/license/2_02.txt. |
+// | If you did not receive a copy of the PHP license and are unable to |
+// | obtain it through the world-wide-web, please send a note to |
+// | license@php.net so we can mail you a copy immediately. |
+// +----------------------------------------------------------------------+
+// | Authors: Martin Jansen <mj@php.net> |
+// | |
+// +----------------------------------------------------------------------+
+//
+// $Id$
+
+define("NET_PORTSCAN_SERVICE_FOUND", true);
+define("NET_PORTSCAN_NO_SERVICE", false);
+
+/**
+ * Portscan class
+ *
+ * This class provides methods to scan ports on machines,
+ * that are connected to the internet. See README for more
+ * information on how to use it.
+ *
+ * @author Martin Jansen <mj@php.net>
+ * @version $Revision$
+ */
+class Net_Portscan
+{
+ // {{{ checkPort()
+
+ /**
+ * Check if there is a service available at a certain port.
+ *
+ * This function tries to open a connection to the port
+ * $port on the machine $host. If the connection can be
+ * established, there is a service listening on the port.
+ * If the connection fails, there is no service.
+ *
+ * @access public
+ * @param string Hostname
+ * @param integer Portnumber
+ * @param integer Timeout for socket connection in seconds (default is 30).
+ * @return string
+ */
+ function checkPort($host, $port, $timeout = 30)
+ {
+ $socket = @fsockopen($host, $port, $errorNumber, $errorString, $timeout);
+
+ if (!$socket) {
+ return NET_PORTSCAN_NO_SERVICE;
+ }
+
+ return NET_PORTSCAN_SERVICE_FOUND;
+ }
+
+ // }}}
+ // {{{ checkPortRange()
+
+ /**
+ * Check a range of ports at a machine
+ *
+ * This function can scan a range of ports (from $minPort
+ * to $maxPort) on the machine $host for running services.
+ *
+ * @access public
+ * @param string Hostname
+ * @param integer Lowest port
+ * @param integer Highest port
+ * @param integer Timeout for socket connection in seconds (default is 30).
+ * @return array Associative array containing the result
+ */
+ function checkPortRange($host, $minPort, $maxPort, $timeout = 30)
+ {
+ for ($i = $minPort; $i <= $maxPort; $i++) {
+ $retVal[$i] = Net_Portscan::checkPort($host, $i, $timeout);
+ }
+
+ return $retVal;
+ }
+
+ // }}}
+ // {{{ getService()
+
+ /**
+ * Get name of the service that is listening on a certain port.
+ *
+ * @access public
+ * @param integer Portnumber
+ * @param string Protocal (default is tcp)
+ * @return string Name of the service
+ */
+ function getService($port, $protocol = "tcp")
+ {
+ return @getservbyport($port, $protocol);
+ }
+
+ // }}}
+ // {{{ getPort()
+
+ /**
+ * Get port that a certain service uses.
+ *
+ * @access public
+ * @param string Name of the service
+ * @param string Protocal (default is tcp)
+ * @return integer Portnumber
+ */
+ function getPort($service, $protocol = "tcp")
+ {
+ return @getservbyname($service, $protocol);
+ }
+
+ // }}}
+}
+?>
View
3 README
@@ -0,0 +1,3 @@
+The documentation for Net_Portscan is available at
+
+ http://pear.php.net/manual/
View
28 package.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<!-- do not use the "Type" attribute here, that one is only for
+ generated package.xml files -->
+<package>
+ <name>Net_Portscan</name>
+ <summary>Portscanner utilities.</summary>
+ <maintainer>
+ <user>MJ</user>
+ <name>Martin Jansen</name>
+ <email>mj@php.net</email>
+ </maintainer>
+
+ <release>
+ <version>1.0</version>
+ <date>2001-11-15</date>
+ <notes>
+ This is the first stable release.
+ </notes>
+ </release>
+
+ <filelist>
+ <dir name="/" baseinstalldir="Net_Portscan">
+ <file role="php">Portscan.php</file>
+ <file role="php">test.php</file>
+ <file role="text">README</file>
+ </dir>
+ </filelist>
+</package>
View
29 test.php
@@ -0,0 +1,29 @@
+<?php
+/** $Id$ */
+require_once "Net_Portscan/Portscan.php";
+
+/** Test for checkPort() and getService() */
+if (Net_Portscan::checkPort("localhost", 80) == NET_PORTSCAN_SERVICE_FOUND) {
+ echo "There is a service on your machine on port 80 (" . Net_Portscan::getService(80) . ").\n";
+}
+
+/** Test for checkPortRange() */
+echo "Scanning localhost ports 70-90\n";
+$result = Net_Portscan::checkPortRange("localhost", 70, 90);
+
+foreach ($result as $port => $element) {
+ if ($element == NET_PORTSCAN_SERVICE_FOUND) {
+ echo "On port " . $port . " there is running a service.\n";
+ } else {
+ echo "On port " . $port . " there is no service running.\n";
+ }
+}
+
+
+/** Test for getService() */
+echo "On port 22, there service " . Net_Portscan::getService(22) . " is running.\n";
+
+/** Test for getPort() */
+echo "The finger service usually runs on port " . Net_Portscan::getPort("finger") . ".\n";
+
+?>

0 comments on commit ac14894

Please sign in to comment.
Something went wrong with that request. Please try again.