Julien Brdy
Julien Brdy

Reputation: 103

How to change ip on the fly without knowing current device

I'm on ArchLinux and I want to change on the fly (whithout config file change) the ip address of my current connection. The command:

ip addr add 192.168.1.57 dev wlan0

seems to be good but I don’t know the current device (wlan0, eth0).

I need to do this from a boot script. I can't check manualy what is the current used device.

Someone would have an idea for me?

Thanks !

Upvotes: 0

Views: 1053

Answers (2)

Julien Brdy
Julien Brdy

Reputation: 103

I have worked with "ExecUpPost" on my profiles /etc/netctl like that:

ExecUpPost='ip addr add $(</var/varIP) dev wlan0 || true'

because I know the device on each profil... but I can't control a config change (/var/varIP) on the fly.

drinkcat answere allow more flexibility. Thanks.

Upvotes: 0

drinkcat
drinkcat

Reputation: 111

ip link show

gives you a list of interfaces, with their status:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: wlp6s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN mode DORMANT group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
25: enp0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

You just need to check which one says state UP (in case of Ethernet, it means that the cable is connected, for wireless, it means that the network is associated). In shell, you would do:

interface="`ip link show | awk '/state UP/ { gsub(/:/, "", $2); print $2; exit }'`"
ip addr add 192.168.1.57 dev "$interface"

Upvotes: 1

Related Questions