Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shirshak's avatar

Anyway to run sudo command and getting output?

In ubuntu I can get the networking traffic details via

$ sudo iftop -t -s 1 -L 10

which gives result like this

interface: eth0
IP address is: 192.168.150.2
MAC address is: c0:3f:d5:f6:cb:d3
Listening on eth0
   # Host name (port/service if enabled)            last 2s   last 10s   last 40s cumulative
--------------------------------------------------------------------------------------------
   1 192.168.203.115                          =>      128Kb      128Kb      128Kb     31.9KB
     13.107.4.50                              <=     10.0Mb     10.0Mb     10.0Mb     2.50MB
   2 192.168.205.7                            =>      163Kb      163Kb      163Kb     40.9KB
     202.166.193.140                          <=     8.08Mb     8.08Mb     8.08Mb     2.02MB
   3 192.168.204.103                          =>      342Kb      342Kb      342Kb     85.5KB
     64.15.121.18                             <=     6.38Mb     6.38Mb     6.38Mb     1.60MB
   4 192.168.204.17                           =>     81.0Kb     81.0Kb     81.0Kb     20.2KB
     13.107.4.50                              <=     5.07Mb     5.07Mb     5.07Mb     1.27MB
   5 192.168.205.86                           =>     78.7Kb     78.7Kb     78.7Kb     19.7KB
     13.107.4.50                              <=     5.06Mb     5.06Mb     5.06Mb     1.26MB
   6 192.168.203.83                           =>     55.2Kb     55.2Kb     55.2Kb     13.8KB
     64.71.142.229                            <=     3.36Mb     3.36Mb     3.36Mb      859KB
   7 192.168.205.23                           =>     52.5Kb     52.5Kb     52.5Kb     13.1KB
     139.5.69.13                              <=     3.05Mb     3.05Mb     3.05Mb      781KB
   8 192.168.203.87                           =>     58.0Kb     58.0Kb     58.0Kb     14.5KB
     139.5.70.14                              <=     2.62Mb     2.62Mb     2.62Mb      671KB
   9 192.168.204.224                          =>     74.1Kb     74.1Kb     74.1Kb     18.5KB
     202.166.193.145                          <=     2.23Mb     2.23Mb     2.23Mb      571KB
  10 192.168.204.16                           =>     47.5Kb     47.5Kb     47.5Kb     11.9KB
     139.5.70.13                              <=     1.90Mb     1.90Mb     1.90Mb      487KB
--------------------------------------------------------------------------------------------
Total send rate:                                     3.01Mb     3.01Mb     3.01Mb
Total receive rate:                                  67.7Mb     67.7Mb     67.7Mb
Total send and receive rate:                         70.7Mb     70.7Mb     70.7Mb
--------------------------------------------------------------------------------------------
Peak rate (sent/received/total):                     3.01Mb     67.7Mb     70.7Mb
Cumulative (sent/received/total):                     771KB     16.9MB     17.7MB
============================================================================================

But we know we used sudo there. I am working for page in university where i can show this result interactivly probably on charts. But I cannot get the output as it need sudo command .

I made command like this

$ php artisan university:internet 

Which in turn calls that sudo command

I know it will give permissions error? Any way or hack to make it possible?

Thanks

0 likes
6 replies
burlresearch's avatar

To peek in on the interface traffic with this tool you need user permission to view that traffic. This is why you have to sudo. Allowing other users on the system to see this traffic would be a huge security hole - don't try that.

One thing you could do, is install a process, as root, that will run this command for you and log the output somehow. This way, you could have your artisan process with permission to read that log file, and then do the processing you want from that. This would make more sense to me - and would be much more secure.

1 like
burlresearch's avatar

Using cron could certainly be one way to do it. Seems that iftop has different output modes - some of those alternatives may be better for your purposes.

I'm not sure what you're shooting for here...

Shirshak's avatar

@burlresearch i just want those text to be in some file and updated regularly like 10 second so i can show live updates in website.

burlresearch's avatar
Level 40

Sure, then you could create a shell-script like this, that will write a bunch of files for you:

#!/bin/bash

i=3

while [ $i -gt 0 ]; do
    s=`date +%s`
    iftop -i eth0 -t -s 1 -L 10 > iftop-${s}.dat
    echo $i
    ((i--))
done

and when you're done tweaking that you can replace with while true; do

1 like

Please or to participate in this conversation.