c2h2
c2h2

Reputation: 12369

shell script acquire gateway address of eth0

I would like to use a short shell script to get my default gateway address. so that it would do

$> whatismygateway.sh
192.168.100.10

I have tried ifconfig eth0, but it just does not contain any information about the gateway.

Upvotes: 1

Views: 397

Answers (2)

DonCallisto
DonCallisto

Reputation: 29942

If eth0 is your interface, you could try something like this

 cat /etc/sysconfig/network-scripts/ifcfg-eth0|grep GATEWAY|sed 's/^[A-Z].*=//'

output:

95.174.29.225

you can also use route command

Upvotes: 1

Paul Fenney
Paul Fenney

Reputation: 1733

route | grep default should give you what you're looking for, although if you have multiple interfaces you may need to filter the results further.

Upvotes: 1

Related Questions