Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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!!

share|improve this question
    
system-config-network-tui could probably do this. Any particular reason why it has to be a bash script? –  nrolans Nov 20 '11 at 21:30
add comment

2 Answers 2

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.