summaryrefslogtreecommitdiff
path: root/libs/network.php
blob: c24fcd485f4835d38d90e5daafb4e8a2bd932dc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
require '../autoload.php';

$datas    = array();
$network  = array();

// Possible commands for ifconfig and ip
$commands = array(
    'ifconfig' => array('ifconfig', '/sbin/ifconfig', '/usr/bin/ifconfig', '/usr/sbin/ifconfig'),
    'ip'       => array('ip', '/bin/ip', '/sbin/ip', '/usr/bin/ip', '/usr/sbin/ip'),
);

// Returns command line for retreive interfaces
function getInterfacesCommand($commands)
{
    $ifconfig = Misc::whichCommand($commands['ifconfig'], ' | awk -F \'[/  |: ]\' \'{print $1}\' | sed -e \'/^$/d\'');

    if (!empty($ifconfig))
    {
        return $ifconfig;
    }
    else
    {
        $ip_cmd = Misc::whichCommand($commands['ip'], ' -V', false);

        if (!empty($ip_cmd))
        {
            return $ip_cmd.' -oneline link show | awk \'{print $2}\' | sed "s/://"';
        }
        else
        {
            return null;
        }
    }
}

// Returns command line for retreive IP address from interface name
function getIpCommand($commands, $interface)
{
    $ifconfig = Misc::whichCommand($commands['ifconfig'], ' '.$interface.' | awk \'/inet / {print $2}\' | cut -d \':\' -f2');

    if (!empty($ifconfig))
    {
        return $ifconfig;
    }
    else
    {
        $ip_cmd = Misc::whichCommand($commands['ip'], ' -V', false);

        if (!empty($ip_cmd))
        {
            return 'for family in inet inet6; do '.
               $ip_cmd.' -oneline -family $family addr show '.$interface.' | grep -v fe80 | awk \'{print $4}\' | sed "s/\/.*//"; ' .
            'done';
        }
        else
        {
            return null;
        }
    }
}


$getInterfaces_cmd = getInterfacesCommand($commands);

if (is_null($getInterfaces_cmd) || !(exec($getInterfaces_cmd, $getInterfaces)))
{
    $datas[] = array('interface' => 'N.A', 'ip' => 'N.A');
}
else
{
    foreach ($getInterfaces as $name)
    {
        $ip = null;

        $getIp_cmd = getIpCommand($commands, $name);        

        if (is_null($getIp_cmd) || !(exec($getIp_cmd, $ip)))
        {
            $network[] = array(
                'name' => $name,
                'ip'   => 'N.A',
            );
        }
        else
        {
            if (!isset($ip[0]))
                $ip[0] = '';

            $network[] = array(
                'name' => $name,
                'ip'   => $ip[0],
            );
        }
    }

    foreach ($network as $interface)
    {
        // Get transmit and receive datas by interface
        exec('cat /sys/class/net/'.$interface['name'].'/statistics/tx_bytes', $getBandwidth_tx);
        exec('cat /sys/class/net/'.$interface['name'].'/statistics/rx_bytes', $getBandwidth_rx);

        $datas[] = array(
            'interface' => $interface['name'],
            'ip'        => $interface['ip'],
            'transmit'  => Misc::getSize($getBandwidth_tx[0]),
            'receive'   => Misc::getSize($getBandwidth_rx[0]),
        );

        unset($getBandwidth_tx, $getBandwidth_rx);
    }
}


echo json_encode($datas);