Programatically activate and deactivate network interfaces using nmcli - linux

I wrote a script that deactivates all the interfaces available using nmcli commands such as:
for iface in `(nmcli -f GENERAL dev list 2>/dev/null || nmcli -f GENERAL dev show) | grep 'GENERAL.DEVICE' | awk '{print $2}'`
do
nmcli dev disconnect iface $iface
done
I would now like to reactivate these interfaces; there is no nmcli dev connect iface … command, and I'm stuck. In particular, my problem is to reactivate the Ethernet interface.
Thanks in advance for your help!

Use this command:
nmcli -p con up id "interface name" iface eth0
You can also use uuid instead of id.
-p is just for human readable output and can be ignored.
iface eth0 can also be omitted.
Take a look here for more information.

Related

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

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

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)

Extracting MAC address from ifconfig output

I am writing a #!bin/bash shell script to automate MAC spoofing. In the script that I have written I make the outputs of ifconfig -a | grep HWaddr equivalent to two different variables. The command ifconfig -a | grep HWaddr returns
eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
and
wlan0 Link uncap: Ethernet HWaddr 00:00:00:00:00:00
but I want the command to return just the MAC address for wlan0.
Try:
[root#linux ~]$ /sbin/ifconfig wlan0 | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}'
00:25:90:F0:3F:92
By specifying wlan0 as the first argument to ifconfig, you are telling it you only want information about that particular interface, so you should only get a single line returned.
The grep command then searches for a MAC address in the output and prints only the portion of the ifconfig output which matches.
OR
Just for your script you can try follwong:
ifconfig -a | grep HWaddr | awk '{print $5}'
OSX
ifconfig en1 | awk '/ether/{print $2}'

Bash script to bring up and down a wireless interface on loop

If I have found the following code from another post regarding an Ethernet connection. I want to do the same thing with the wireless network card which is wlan0 on my machine. I figured that I would try swapping the iface to wlan0 and then put the ping as my router however it hasn't worked.
#!/bin/bash
timeout=5 # delay between checks
pingip='8.8.8.8' # what to ping
iface="eth0"
LOG_FILE="/var/log/syslog"
isdown=0 # indicate whether the interface is up or down
# start assuming interface is up
while true; do
LOG_TIME=`date +%b' '%d' '%T`
if ping -q -c 2 "$pingip" >> /dev/null ; then # ping is good - bring iface up
if [ "$isdown" -ne 0 ] ; then
ifup $iface && isdown=0
printf "$LOG_TIME $0: Interface brought up: %s\n" "$iface" | tee -a $LOG_FILE
fi
else # ping is bad - bring iface down
beep -f 4000
if [ "$isdown" -ne 1 ] ; then
ifdown $iface && isdown=1
printf "$LOG_TIME $0: Interface brought down: %s\n" "$iface" | tee -a $LOG_FILE
fi
fi
sleep "$timeout"
done
Am I barking up the wrong tree here, or do I just need to edit this a bit further. My bash skills are subpar I'm afraid.
I'm pretty sure that ifconfig presents on your system
determine the name of your interface ls /sys/class/net, likely it's wlan0 or wlp2s0
change ifup $iface and ifdown $iface to ifconfig $iface up and ifconfig $iface down respectively

How to cut ifconfig to get eth* details alone?

I'm writing a script to print ethtool details of ifconfig.
Sample output should be from ifconfig is like,
eth0
eth1
eth2
I have tried using below command,
root#bt# ifconfig | cut -d ":" -f 1
But could not able to achieve the same.
Actually, i need to get these eth* and pass in
root#bt# ethtool <arg1> where arg1=eth*
to get results :-) can you please help me to get crop from ifconfig. ?
No.
$ awk -F: '$1 ~ "eth" { print $1 }' /proc/net/dev
eth0
With grep & ifconfig :
ifconfig | grep -o '^eth[0-9]\+'
Or with only grep :
grep -oP '^ *\Keth[0-9]+' /proc/net/dev

Resources