/    Sign up×
Community /Pin to ProfileBookmark

Get network parameters from server

Hi,
I have a dedicated server where I run LAMP.

I want to get the server’s network parameters: ip address, subnet mask or cidr, broadcast address, packets and bytes sent and received.

I know there is the “ifconfig” command where I can specify the network interface which I want to see, and I will receive the output.

But I can’t parse the output from shell_exec to values I want.

Have I to use regular expression? And how?

Thanks

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmJan 10.2023 —  Can you show us what you do get?
Copy linkTweet thisAlerts:
@AnawilliamJan 10.2023 — Yes, you can use regular expressions (regex) to parse the output of the "ifconfig" command and extract the specific network parameters you are looking for.
Copy linkTweet thisAlerts:
@redagrauthorJan 11.2023 — When I execute this code:

&lt;?<br/>
echo shell_exec('ifconfig eth0');<br/>
?&gt;


This is the output on ubuntu 16.04:
> eth0 Link encap:Ethernet IndirizzoHW 2c:84:fc:b7:1a:7e

indirizzo inet:192.168.6.11 Bcast:192.168.6.63 Maschera:255.255.255.192

indirizzo inet6: fe80::1e1b:dff:febe:1a7e/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:156264568 errors:0 dropped:0 overruns:0 frame:0

TX packets:184487411 errors:0 dropped:0 overruns:0 carrier:0

collisioni:0 txqueuelen:1000

Byte RX:80241096962 (80.2 GB) Byte TX:215452622123 (215.4 GB)


And this is the output on ubuntu 18.04:
> eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

inet 192.168.6.12 netmask 255.255.255.192 broadcast 192.168.6.63

inet6 fe80::576c:2407:2764:1468 prefixlen 64 scopeid 0x20<link>

ether 66:f5:cc:a7:58:41 txqueuelen 1000 (Ethernet)

RX packets 115757 bytes 130941810 (130.9 MB)

RX errors 0 dropped 0 overruns 0 frame 0

TX packets 64956 bytes 15224204 (15.2 MB)

TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0


I noticed that the output changes on different ubuntu server version. If I update the version to 22.04, I think the output will change. But now the server where I need the network parameters is on ubuntu 16.04.

How can I do?

Thanks
Copy linkTweet thisAlerts:
@ginerjmJan 11.2023 — If one assumes that you know how to search that specific version properly, then simply add some code to alter what you are searching for depending upon the version. If your script can recognize that version then it can choose the correct search code.
Copy linkTweet thisAlerts:
@redagrauthorJan 12.2023 — Yes, I can execute different code on different ubuntu server version.

At this point, it takes two ways to pull the data, each done on the specific version.
Copy linkTweet thisAlerts:
@ginerjmJan 12.2023 — Can you recognize the ubuntu version in your script or by noting the first line's contents perhaps? Something?
Copy linkTweet thisAlerts:
@redagrauthorJan 13.2023 — Concerning this, there is no problem. I insert a string, like this:
``<i>
</i>&lt;?
$version = '16.04';
# OR
$version = '18.04';
?&gt;<i>
</i>
``

And on each machine I can insert the correct $version, then we can proceed.
Copy linkTweet thisAlerts:
@ginerjmJan 13.2023 — If that makes you happy

I would think that you would like to have a version of your code that recognizes any given file and knows how to handle it without manual intervention. Or that gives you a response that tells you it doesn't know how to handle it.
Copy linkTweet thisAlerts:
@redagrauthorJan 13.2023 — This is not a problem, currently there are only two servers, then when I update the ubuntu versions, I will also update the value of that variable.
×

Success!

Help @redagr spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...