Answer the question
In order to leave comments, you need to log in
How to determine ip through bash script?
Hello everyone, tell me how to determine ip in bash? For example, there is such a pwgen utility and in the script I can specify something like this mysql -uroot -p$pass_pwgen
How can I make it so that I can write $ip in the script and so that the ip of the server in the terminal of which the script was launched is registered in the configs. I want to do such a plan:
echo "host $ip" > /etc/rclocal
Answer the question
In order to leave comments, you need to log in
A server can have multiple IP addresses, multiple interfaces, and they can work with different routes.
What exactly do you want?
ifconfig to parse? or ip addr show?
If there is only one external interface, then the usual
hostname -I will do.
If you know the name of the interface, then you can try this
#!/bin/bash
IP=$(ifconfig eth0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
echo "My IP is $IP"
In the simplest case, when there is only one network card, no bells and whistles - first netstat -in to get a list of interfaces and their names and discard lo0, and then ifconfig on the interface that remains.
But this is the simplest case. Even a computer with one network card can have, for example, a virtual machine and a bridge that already combines at least two interfaces:
# netstat -in
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
br0 1500 850276 0 9811 0 138039 0 0 0 BMPRU
eth0 1500 859789 0 1739 0 138070 0 0 0 BMRU
lo 65536 3749 0 0 0 3749 0 0 0 LRU
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question