0

I would like to have a bash script for Centos machine. I want the script to show me the current IP Address /subnet mask / Default GW. and give me option to enter a new IP Address /subnet Mask / Default GW. It should do it for ETH0.

Can someone have an idea how to do it?

Thanks!!

1
  • system-config-network-tui could probably do this. Any particular reason why it has to be a bash script?
    – nrolans
    Nov 20, 2011 at 21:30

2 Answers 2

1

I'm not going to write it all for you, but here's something that works in Ubuntu:

ifconfig eth0 | grep "inet addr:" | awk -F: '{ print $2 }' | awk '{ print $1 }'

That will echo the current ip address in eth0.

1

ifconfig(1) is the standard command for manipulating IP parameters of interfaces. See man ifconfig for how to use it, but e.g.

ifconfig eth0 192.168.0.1 netmask 255.255.255.0

will set the IP address and netmask for eth0.

To read the interface parameters, you can use grep/sed/perl to parse the output of ifconfig eth0, but if you have the ifdata command, a less painful way to get e.g. just the IP address is

ifdata -pa eth0

In Ubuntu, ifdata is part of the moreutils package.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .