I'm looking for an exact replacement of ifconfig <interface> <ip_address>
command using the ip command.
For example, if I want to change eth0's IP address to 10.0.0.1 (not caring about the netmask), I can just type:
ifconfig eth0 10.0.0.1
For the ip command though, I've looked around a lot and anything I can find is just adding or removing a network, not modifying the current one. So, to modify the current network, I end up having to use the following (or similar) script:
new_ip=$1
ip_addr=`ip addr show eth0 | grep "inet" | cut -d: -f2 | awk '{print $2}'`
ip -4 addr delete $ip_addr dev eth0
ip -4 addr add $new_ip dev eth0
Because if I don't do it this way, when the primary interface ip is deleted later on, it takes all the secondary ip addresses with it.
I'm doing this from inside a program and there are too many system calls that could go wrong here. Is there a way to do this without having to use so many commands?
ip addr change
and ip addr replace
also does the same thing:
Note: I can add any open source program into the system as I want. So, don't shy away from solutions that use other open source programs.