Bash script to get all IP addresses
There's no need for grep
. Here's one way using awk
:
List only addr:
ifconfig | awk -F "[: ]+" '/inet addr:/ { if ($4 != "127.0.0.1") print $4 }'
List device and addr:
ifconfig | awk -v RS="\n\n" '{ for (i=1; i<=NF; i++) if ($i == "inet" && $(i+1) ~ /^addr:/) address = substr($(i+1), 6); if (address != "127.0.0.1") printf "%s\t%s\n", $1, address }'
Linux bash script to extract IP address
To just get your IP address:
echo `ifconfig eth0 2>/dev/null|awk '/inet addr:/ {print $2}'|sed 's/addr://'`
This will give you the IP address of eth0.
Edit: Due to name changes of interfaces in recent versions of Ubuntu, this doesn't work anymore. Instead, you could just use this:
hostname --all-ip-addresses
or hostname -I
, which does the same thing (gives you ALL IP addresses of the host).
How to get all of the IP addresses in a string into a bash array
I ended up being able to do it by using awk:
IPAddresses=($(dig redis A | grep redis | grep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | awk '{print $5}'))
The IPAddresses array has all of the IP addresses in it, and can be printed out by executing echo "${IPAddresses[@]}"
How to launch a script for a list of ip addresses (bash script)
#!/bin/bash/
while read -r line; do
echo -e "$line\n"
sshpass -p 'Password' ssh -o StrictHostKeyChecking=no root@$line -t \
"cd /exemple/ && sh restart-launcher.sh;exec \$SHELL -l"
sleep 5
done < Ip.txt
Now, we could have a discussion about using sshpass (or rather, why you shouldn't), but it feels out of scope.
So, all commands you wish to be looped over need to be inside the loop.
As you read sets the variable $line
, that is what you need to use. In your example, you used the variable $ip
which you haven't set anywhere.
Can anyone help me to write a bash script to list all ip address in network and save in a file?
nmap -sn $1"/"$2 | awk '/report for/ { print $5 }' > ipadds
This will give you an easy list of all the active hosts on the network using a given ip address and sbnet prefix.
Using Bash to Extract IP Addresses From Each Line of Log File
To print IPs next to each other, try the below command:
cat first | grep -o '[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}\.[0-9]\{0,3\}' | awk 'NR%2{printf $0"\t";next;}1'
Bash script find sequence numbers of ip address
Would you please try the bash script:
#!/bin/bash
report() {
local seg=${1%.*}
for (( i = $2 - 2; i <= $2; i++ )); do
echo "$seg.$i"
done
}
prev=255 # initial value as a workaround
while IFS= read -r ip; do
n=${ip##*.} # extract the last octet
if (( n == prev + 1 )); then
(( count++ ))
if (( count >= 2 )); then # found three sequences
report "$ip" "$n" # then report them
fi
else
count=0 # reset the count if fail
fi
prev=$n
done < <(tr -s ' ' '\n' < input_file.txt | sort -t. -nk4,4)
where input_file.txt
is the filename which contain the ip addresses.
Output with the provided file:
172.15.0.6
172.15.0.7
172.15.0.8
tr -s ' ' '\n'
replaces the whitespaces with newline characters so the
output can be fed tosort
command.sort -t. -nk4,4
sorts the input with the last octet in numerical order.- The output is fed to the
while
loop via the process substitution< <(commands)
. - Now the
while
loop process the input line by line (ip by ip). - The variable
n
is assigned to the last (4th) octet. n
is compared with theprev
which holds the number in the last line.
Ifn == prev + 1
holds, the numbers are in sequence.
Thencount
is accumulated.- If
count
reaches 2, the three lines including the current line
are in the sequence. Then parameters are passed to the functionreport
.
Linux Bash: List interfaces with IPs and Mac addresses
Write a script like this:
#!/bin/bash
printf '%10s %32s %32s\n' interface ipaddress macaddress
printf '%s\n' '----------------------------------------------------------------------------'
for each in $(ip address | grep -oP '(^[\d]+:\s)\K[\d\w]+'); do
mac=$(ip address show ${each} | grep -oP '(?<=link/ether\s)\K[\da-f:]+|(?<=link/loopback\s)\K[\da-f:]+')
for address in $(ip address show ${each} | grep -oP '(?<=inet\s)\K[\d.]+|(?<=inet6\s)\K[\da-f:]+'); do
printf '%10s %32s %32s\n' ${each} ${address} ${mac}
done
done
Output:
interface ipaddress macaddress
----------------------------------------------------------------------------
lo 127.0.0.1 00:00:00:00:00:00
lo ::1 00:00:00:00:00:00
wlo1 192.168.0.7 c8:aa:bb:cc:dd:ee
wlo1 fe80::7aec:8287:9f45:d833 c8:aa:bb:cc:dd:ee
docker0 172.17.0.1 02:42:0d:0d:0d:0d
tap0 10.1.30.2 16:50:cc:cc:cc:cc
tap0 fe80::10a9:d3ff:fece:57d7 16:50:cc:cc:cc:cc
Related Topics
Bluetooth Low Energy in C - Using Bluez to Create a Gatt Server
How to Prefill Command Line Input
How to Repeat a Dash (Hyphen) in Shell
Install.Packages("Devtools") on R 3.0.2 Fails in Ubuntu 14.04
Simulate Network Latency on Specific Port Using Tc
How to Redirect from Audio Output to Mic Input Using Pulseaudio
Shell Script Password Security of Command-Line Parameters
How to Make Perl Wait for Child Processes Started in the Background with System()
How to Manipulate Page Cache in Linux
Print Kernel's Page Table Entries
Elf Program Header Virtual Address and File Offset
How Does Stat Command Calculate the Blocks of a File
How to Change the Soname of a Binary Directly
Compile Swift Script with Static Swift Core Library