Home > Net >  How to use bash to get the dhcp server ip from linux / ubuntu specifically?
How to use bash to get the dhcp server ip from linux / ubuntu specifically?

Time:01-29

Would like to return just "123.235.44.1" value from below. how do i do this?

# dhclient -v 
Internet Systems Consortium DHCP Client 4.4.1
Copyright 2004-2018 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/

Listening on LPF/ens3/fa:16:3e:9e:33:25
Sending on   LPF/ens3/fa:16:3e:9e:33:25
Sending on   Socket/fallback
DHCPREQUEST for 123.123.123.123 on ens3 to 255.255.255.255 port 67 (xid=0x77420868)
DHCPACK of 123.123.123.123 from **123.235.44.1** (xid=0x68084277)
RTNETLINK answers: File exists
bound to 123.123.123.123 -- renewal in 33881 seconds.

a bash program that returns the dhcp server ip 123.235.44.1

CodePudding user response:

You don't need grep if you use awk

dhclient -v 2>&1 | awk '/^DHCPACK/ { print $5 }'

CodePudding user response:

You need to redirect stderr to stdout in order to capture and filter to obtain the required value.

dhclient -v 2>&1 | grep '^DHCPACK' | awk '{ print $5 }'
  • Related