Netstat (Network Statistics) is a command-line tool that provides information about network connections, routing paths, interface statistics, and other aspects of the network. It is used to diagnose network problems and monitor network activity.
Netstat Features
- Viewing active connections : Netstat shows a list of all active TCP, UDP, and UNIX connections.
- Port Information : You can get information about open ports and those that are listening.
- Protocol Statistics : Netstat provides statistics for various protocols like TCP, UDP, ICMP.
- Routing Tables : The tool shows the routing table, which is useful for analyzing the routes of packets in the network.
- Network Interfaces : Netstat displays statistics for network interfaces, such as the number of packets sent and received.
Netstat Usage Examples
- Displaying all connections and ports :
netstat -a
- Display only TCP connections :
netstat -at
- Display only UDP connections :
netstat -au
- Displaying listening ports :
netstat -l
Netstat implementation in PHP
To implement Netstat functionality in PHP, you can use a function exec()
to execute a system command and process the results. Below is some sample PHP code:
<?php
// Funkcja do wykonania polecenia Netstat i zwrócenia wyników
function getNetstatOutput() {
// Wykonanie polecenia Netstat
$output = [];
exec('netstat -a', $output);
// Przetworzenie wyników
$connections = [];
foreach ($output as $line) {
// Podział linii na kolumny
$columns = preg_split('/\s+/', $line);
if (count($columns) >= 6) {
$connections[] = [
'Proto' => $columns[0],
'Local Address' => $columns[3],
'Foreign Address' => $columns[4],
'State' => $columns[5]
];
}
}
return $connections;
}
// Wywołanie funkcji i wyświetlenie wyników
$netstatData = getNetstatOutput();
echo '<pre>';
print_r($netstatData);
echo '</pre>';
?>
This code executes the command netstat -a
, processes the results, and displays them in a readable form. You can customize this code to suit your needs, such as filtering the results or formatting them differently.
Here are two additional examples of implementing Netstat functions in PHP:
Example 1: Displaying TCP connections
This example shows how to display only TCP connections using Netstat and PHP.
<?php
// Funkcja do wykonania polecenia Netstat dla połączeń TCP
function getTcpConnections() {
// Wykonanie polecenia Netstat dla połączeń TCP
$output = [];
exec('netstat -at', $output);
// Przetworzenie wyników
$connections = [];
foreach ($output as $line) {
// Podział linii na kolumny
$columns = preg_split('/\s+/', $line);
if (count($columns) >= 6) {
$connections[] = [
'Proto' => $columns[0],
'Local Address' => $columns[3],
'Foreign Address' => $columns[4],
'State' => $columns[5]
];
}
}
return $connections;
}
// Wywołanie funkcji i wyświetlenie wyników
$tcpConnections = getTcpConnections();
echo '<pre>';
print_r($tcpConnections);
echo '</pre>';
?>
Example 2: Displaying listening ports
This example shows how to display listening ports using Netstat and PHP.
<?php
// Funkcja do wykonania polecenia Netstat dla portów nasłuchujących
function getListeningPorts() {
// Wykonanie polecenia Netstat dla portów nasłuchujących
$output = [];
exec('netstat -l', $output);
// Przetworzenie wyników
$ports = [];
foreach ($output as $line) {
// Podział linii na kolumny
$columns = preg_split('/\s+/', $line);
if (count($columns) >= 6) {
$ports[] = [
'Proto' => $columns[0],
'Local Address' => $columns[3],
'Foreign Address' => $columns[4],
'State' => $columns[5]
];
}
}
return $ports;
}
// Wywołanie funkcji i wyświetlenie wyników
$listeningPorts = getListeningPorts();
echo '<pre>';
print_r($listeningPorts);
echo '</pre>';
?>
These examples show how PHP can be used to execute various Netstat commands and process the results in a readable form.
Comments
Post a Comment