How to scan docker containers IPs from a docker container that is in the same network? - linux

I am running multiple docker containers with docker-compose and all of them are in the same network.
Once the containers are up and running I would like to access from one of them to all the others through a shell script, where that script retrieves all IP addresses of the other containers from the docker network (first Ethernet interface eth0 inside the container).
I know there's a way to get IP addresses using container name, but I am running docker-compose with --scale flag so the script doesn't know how many containers are in that network, I am confused and don't know how to do that.
The base image of all containers is ubuntu:20.04

Since I couldn't find a simple way for now I created a script using nmap and ifconfig, it consists on getting the IP address of inet (Ethernet interface) from one of the docker machines then lookup on that network using nmap:
#!/bin/bash
EI=$(/sbin/ifconfig eth0 | grep inet | awk '{ print $2}') # Get inet IP of eth0
IPS=($(nmap $EI/24 | grep dindood | awk '{ print $6}' | sed 's/.$//; s/^.//')) # Look for the nodes Ip addresses
NAMES=($(nmap $EI/24 | grep dindood | awk '{ print $5}' | cut -f1 -d".")) # Look for the nodes Containers names
echo "host ip=$EI"
for i in "${!IPS[#]}"; do #loop through IP addresses found
echo "hostname= ${NAMES[$i]} | IP= ${IPS[$i]}"
done
the output looked like this for me:
host ip=192.168.160.3
hostname= my_project_container_2 | IP= 192.168.160.2
hostname= my_project_container_1 | IP= 192.168.160.4

Related

How to use ssh -t command which includes grep with quotes

I am trying to grep the third octet in IP address to an tap device on remote machine.
ssh -t user#host "/sbin/ifconfig tap0 | grep "inet" | /usr/bin/awk -F'[: ]+' '{ print $4 }' | awk -F'[.]' '{print $3}'"
I am resulting this:
inet addr:10.22.66.77 Bcast:10.22.66.255 Mask:255.255.255.0
When i run the command on the remote machine it shows 66
How to make it working with ssh -t?
Sometimes using perl is simpler:
ssh -t user#host "/sbin/ifconfig tap0" | perl -n -e 'if (/inet\saddr:\d+\.\d+\.(\d+)/) { print "$1\n"}'
it runs regular expression pattern on the local machine match on the third octet following addr: and this is then printed via $1
The pattern match is run on the local machine to avoid problems with escaping " (In your example code the " in the grep inet seems to terminate the ssh...)

Printing IP address of Nic card to bash using script

currently I am working on a school assignment on Linux using Vim where I have to write a script that displays the user that is currently logged in, the time and date, and list only the IP address of the Nic card. I have everything working except for the IP address part. If anyone could help I would appreciate it greatly.
Edit to include the code I have at the moment.
#!/bin/bash
Time=$(date)
IP=$(ifconfig ens33)
echo "The following user is currently logged in $USER"
echo ""
echo "The current time is $Time"
echo ""
echo "The IP information is $IP"
You can filter the result of ifconfig using awk like this (IPv4):
$ ifconfig ens33 | awk '/inet addr/{print substr($2, 6)}'
Result:
10.10.xx.xx
inet addr: represents IPv4 address.
inet6 addr: represents IPv6 address.
this line
IP=$(ifconfig ens33| grep inet | sed 's/ */ /' | cut -d" " -f3)

Bash script to update Deluged Interface IP fails to run as cron job, but works when run manually

I'm running a Debian Linux variant, OpenVPN, and Deluge.
My VPN provider has short IP leases, so every few days I need to update Deluge's interface IP so that it downloads only on tun0. I put together a script to automate this - it basically puts the current tun0 IP into $tun0ip, then does a grep check against the deluge daemon config file to see if that string is present (this is a dirty way to do it, but I guess it works).
My problem is this: When I call the script manually, it works as intended - it kills deluged and then relaunches it, specifying the new IP with deluged -i $tun0ip . However, when I run the script as a cron job, it fails - it passes a null or zero value to $tun0ip, and then deluged -i $tun0ip doesn't work without a valid IP specified, so the application fails to launch. Script below. Am I doing something wrong here? I really appreciate any help!
#!/bin/bash
tun0ip=$( ifconfig tun0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1')
if grep -q $tun0ip "/root/.config/deluge/core.conf"; then
#Great, the IP is correct - don't have to do anything.
echo "IP Looks good - no changes were made."
else
echo "tun0 IP not found in config file. Killing deluged and rebooting with $tun0ip as interface."
killall deluged
sleep 5
deluged -i $tun0ip
fi
I had to specifiy /sbin/ifconfig, like this:
tun0ip=$( /sbin/ifconfig tun0 | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'$

Shell script to find the IP address of an Virtual machine created by using KVM/virsh command

I am looking for the shell script to find the IP address of a virtual machine created by using KVM/VIRSH.
I used the following steps to get it so, but couldn´t able to find it.
Ping the IP addresses in range
2.Use Virsh list command to list all the active VM
3.use Virsh dumpxml domainname and project the xml of vm
use grep command and fetch the Hardware address of vm
5.Display the hardware address of each vm
Now I would like to add one more step like fetching the IP address for that particular Hardware address using ¨arp -ne¨
I couldn´t able to figure out how to add the part.
could any one help me on this.
for i in {1..150}
do
ping -c 1 -n -q -r -t 1 -s 1 -W 1 192.168.1.$i > /dev/null &
done
for name in `virsh list | grep running | awk '{ print $2 }'`
do
# printf "\n$name\n "
arp -e | grep "`virsh dumpxml $name | grep "mac address"|sed "s/.*'\(.*\)'.*/\1/g"`" |
awk '{ printf $1 ; printf " " ; printf $3 "\n" }'
done
current output:
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41
vm3 52:54:00:0e:4c:42
The output I am expecting is
$ ./virshshell.sh
vm2 52:54:00:4b:7f:41 192.*.*.*
vm3 52:54:00:0e:4c:42 192.*.*.*
Use nmap to do network discovery instead of ping. It can do what ping does but also much more, plus it runs way faster and takes care of the network-scope scanning that you're doing in your for loop.
$ nmap -T5 -n -PE 192.168.4.0/24 > /dev/null
$ ip neigh show | grep 192.168.4 | grep -v FAILED
192.168.4.92 dev eth0 lladdr 54:52:00:90:90:92 REACHABLE
192.168.4.11 dev eth0 lladdr fa:16:3e:fa:ac:07 REACHABLE
192.168.4.91 dev eth0 lladdr 54:52:00:90:90:91 REACHABLE
192.168.4.90 dev eth0 lladdr 54:52:00:90:90:90 REACHABLE

Command line shell for identifying interfaces that are up and running

Is there any shell command for filtering interface names that are up and running
Run the command /sbin/ifconfig and look for UP.
If you want just the names of the active interfaces, you need some scripting:
ifconfig | awk '/^[^ ]/ { name=$1; } /^ +UP / {print name;}'
You may check it with several commands:
See the content of the network interface file:
cat /sys/class/net/eth0/operstate
Using ip command
ip a | grep -Eq ': eth0:.*state UP'
Or:
ifconfig | grep -Eq ': eth0:.*state UP'
Where eth0 is your interface. Original post
ifconfig | awk -v RS="" '/MULTICAST/ && /UP/ && /RUNNING/ && /BROADCAST/ {print substr($1, 0, length($1)-1)}'

Resources