Check MAC from log - linux

I currently have a script that checks the MAC address, tt looks like this
"m"|"-m" )
if [ ! -n "$2" ]
then
echo "Enter MAC address"
exit 0
fi
out1=`cat $dhcp_files/* | grep -i "$2" 2>/dev/null`
ip=`cat $dhcp_files/* | grep -i "$2" | awk '{print $8}' | sed s/\;//g 2>/dev/null`
out=` grep -i $ip $shapy/* | awk '{print $8}' 2>/dev/null`
echo -en ''$out1' on '$out' \n'
this is done like that: ./mac_check -m 00:00:00:00:00:00
The result of this script:
host Jon_Rosewelt_6_5 { hardware ethernet 00:1d:4e:b5:d4:10; fixed-address 192.168.101.19; } on eth4
but I would like to do to the script received any form of mac address
aka. ./check_mac -m 001d.4eb5.d410 **or** 001d4eb5d410 **or** 00-1d-4e-b5-d4-10
How can I do that?

With GNU sed and Solaris sed:
echo 001d.4eb5.d410 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 001d4eb5d410 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 00-1d-4e-b5-d4-10 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
echo 00:1d:4e:b5:d4:10 | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//'
Output:
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
00:1d:4e:b5:d4:10
With your code:
"m"|"-m" )
mac="$2" # added
if [ ! -n "$mac" ] # changed
then
echo "Enter MAC address"
exit 0
fi
mac="$(echo "$mac" | sed 's/[^0-9a-f]//g;s/../&:/g;s/:$//')" # added
out1=`cat $dhcp_files/* | grep -i "$mac" 2>/dev/null` # changed
ip=`cat $dhcp_files/* | grep -i "$mac" | awk '{print $8}' | sed s/\;//g 2>/dev/null` # changed
out=` grep -i $ip $shapy/* | awk '{print $8}' 2>/dev/null`
echo -en ''$out1' on '$out' \n'

Related

how to use sed to tansfer virsh xml data to PCI BDF format

I have two praised xml data as following 2 lines:
<address type='pci' domain='0x0000' bus='0x04' slot='0x10' function='0x0 '/>
<address type='pci' domain='0x0000' bus='0x82' slot='0x10' function='0x1 '/>
I want to combine the value of domain,bus, slot and function of each line with ":" as below result:
$line1=0000:04:10:0
$line2=0000:82:10:1
above PCI BDF format (0000:00:00:0) is for next checking.
thanks,
finally, I added partial code into a intel example script for sr-iov host VFs management, it works for me.
the output result looks like below:
here is the script:
#!/bin/bash
DOMAINS=$(virsh list --all | grep "running" | awk '{print $2}')
vm_list="VM_Name VF_BDF"
for DOMAIN in $DOMAINS; do
temp=("$(virsh dumpxml $DOMAIN | grep -w hostdev -A9 |sed -n '/source>/,/<\/source>/p' |grep "<address" | sed "s/.*domain='0x\([^']\+\)'.*bus='0x\([^']\+\)'.*slot='0x\([^']\+\)'.*function='0x\([^']\+\)'.*/\1:\2:\3.\4/")")
vm_list+="\\n${DOMAIN} ${temp}"
done
#echo -e $vm_list
NIC_DIR="/sys/class/net"
for i in $( ls $NIC_DIR) ;
do
if [ -d "${NIC_DIR}/$i/device" -a ! -L "${NIC_DIR}/$i/device/physfn" ]; then
declare -a VF_PCI_BDF
declare -a VF_INTERFACE
k=0
for j in $( ls "${NIC_DIR}/$i/device" ) ;
do
if [[ "$j" == "virtfn"* ]]; then
VF_PCI=$( readlink "${NIC_DIR}/$i/device/$j" | cut -d '/' -f2 )
VF_PCI_BDF[$k]=$VF_PCI
#get the interface name for the VF at this PCI Address
for iface in $( ls $NIC_DIR );
do
link_dir=$( readlink ${NIC_DIR}/$iface )
if [[ "$link_dir" == *"$VF_PCI"* ]]; then
VF_INTERFACE[$k]=$iface
fi
done
((k++))
fi
done
NUM_VFs=${#VF_PCI_BDF[#]}
if [[ $NUM_VFs -gt 0 ]]; then
#get the PF Device Description
PF_PCI=$( readlink "${NIC_DIR}/$i/device" | cut -d '/' -f4 )
PF_VENDOR=$( lspci -vmmks $PF_PCI | grep ^Vendor | cut -f2)
PF_NAME=$( lspci -vmmks $PF_PCI | grep ^Device | cut -f2).
echo "Virtual Functions on $PF_VENDOR $PF_NAME ($i):"
echo -e "PCI BDF\t\tInterface\tVM_Name"
echo -e "=======\t\t=========\t==============="
for (( l = 0; l < $NUM_VFs; l++ )) ;
do
guestVM=$(echo -e $vm_list | grep ${VF_PCI_BDF[$l]} | awk '{print $1}')
if [ $? -ne 0 ]; then
echo "Fail to find the VF_BDF with guestVM. "
exit
fi
if [[ $guestVM == "" ]]; then
echo -e "${VF_PCI_BDF[$l]}\t${VF_INTERFACE[$l]}\t\t${guestVM}"
else
echo -e "${VF_PCI_BDF[$l]}\t" used "\t\t${guestVM}"
fi
done
unset VF_PCI_BDF
unset VF_INTERFACE
echo " "
fi
fi
done
Try this:
cat input | sed "s/.*domain='0x\([^']\+\)'.*bus='0x\([^']\+\)'.*slot='0x\([^']\+\)'.*function='0x\([^']\+\)'.*/\0=\1:\2:\3:\4/"
Where input is the file with the data

Searching a specific file system in bash

I have a task which asks to write a script which displays all partitions formatted with a specific file system, given as parameter.
I have written the script but when i run it it displays '0'. What am i doing wrong?
This is my code:
#!/bin/bash
n=sudo parted -l | tail -n +8 | awk '{print $5}' | wc | awk '{print $2}'
m=sudo parted -l | tail -n +8 | awk '{print $5}'
q=sudo parted -l | tail -n +8
for i in $n; do
if [ "[ $m | sed -n ip ]" = "$1" ]; then
echo "$q | sed -n ip"
fi
done
Different approach from yours, but does it do what you need?
lsblk -f | awk '$0 ~ fs {print $NF}' fs=ext2

variable not holding output of command

The output of $ip coming blank
#!/bin/bash
for i in illinrcmsg{002..003}
do
echo " ####################### $i #################### "
ssh -o StrictHostKeyChecking=no $i "
echo $i
cp /etc/hosts /etc/hosts_bak
ip=`ifconfig | head -n 2 | tail -n 1 | awk '{print $2}' | cut -d':' -f2`
echo $ip
echo "$ip $i.corp.amdocs.com $i" >> /etc/hosts
"
done
I am trying to fix /etc/hosts entry on multiple servers with a bash script, but it is dumping on hostname values on the file.
This worked:
#!/bin/bash
for i in illinrcmsg{002..003}
do
echo " ####################### $i #################### "
ssh -o StrictHostKeyChecking=no $i "
echo $i
cp /etc/hosts /etc/hosts_bak
#ip=`ifconfig | head -n 2 | tail -n 1 | awk '{print $2}' | cut -d':' -f2`
#echo $ip
echo "`nslookup illinrcmsg002 | tail -n 2 | head -n 1|awk '{print $2}'`
$i.corp.amdocs.com $i" >> /etc/hosts
"
done

Can I use hexdump in a shell script?

Can I use hexdump in a shell script?
When I use it I keep getting an error .
syntax error near unexpected token 'hexdump'
#!/bin/bash
#bash-hexdump
# Quick script to check delay of the shotpoints
echo " please enter the complete line name as mentioned in the RAID2 "
read $line
cd /argus/raid2/"$line"
echo
echo " Entering the directory "
echo
for file in /argus/raid2/"$line"/*.ffid
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list1
done
for filename in 'cat list1'
do
sed -n 6p | awk '{print $1}' = $wd
cat list.txt | sed -n 1p | cut -c13-14 = $hh
cat list.txt | sed -n 1p | cut -c15-16 = $mm
cat list.txt | sed -n 2p | cut -c1-2 = $ss
done
while [ true ]
do
$FFID=`ls -1rt $1 | grep -i ffid | tail -1`
echo " FFID value is : $FFID"
while [ $FFID = `ls -1rt $1 | grep -i ffid | tail -1` ]
do
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list
done
for filename in 'cat list'
do
cat list.txt | sed -n 1p | cut -c13-14 = $hh1
cat list.txt | sed -n 1p | cut -c15-16 = $mm1
cat list.txt | sed -n 2p | cut -c1-2 = $ss1
done
$time1 = "$hh"":""$mm"":""$ss" ;
$time2 = "$hh1"":""$mm1"":""$ss1" ;
$former_seconds = $(date --date= "$time1" +%s);
$later_seconds = $(date --date= "$time2" +%s);
$time_delay = ( "$later_seconds" - "$former_seconds" )
$wb_time = ( "$wd" * 1.33 )
if
(("$wb_time" + "$time_delay")) < 12.0
then
echo "please slow down"
fi
if [ -e EOL.ffid ]
then
echo "EOL.ffid detected, exiting script"
exit
fi
done
I am not able to figure out why the hexdump code is giving me an error . Please help .
You are missing the do in your for loop:
for file in /argus/raid2/"$line"/*.ffid
do
hexdump -e "16 \"%_p\" \"\\n\"" $FFID | sed -n '68,73p' > list1
done

How to place the output of an echo statement and an arp statement on one line?

How can I get the output "echo" and "macaddress" on one line?
This is what I've got:
ipRange="192.168.0."
macaddress= arp | grep -w "$ipRange$1" | awk '{print $3,$1}'
ping -c1 "$ipRange$1" > /dev/null
if [ $? -eq 0 ]; then
echo "deze host met mac address en ip address is up $macaddress"
else
echo "het is down"
fi
This is the output:
VirtualBox ~ $ bash test2.sh 149
e0:b9:a5:f8:24:c3 192.168.0.149
deze host met mac address en ip address is up
this should do output in single line
ipRange="192.168.0."
macaddress=$(arp | grep -w "$ipRange$1" | awk '{print $3,$1}')
ping -c1 "$ipRange$1" > /dev/null
if [ $? -eq 0 ]; then
echo "deze host met mac address en ip address is up $macaddress"
else
echo "het is down"
fi
Just replace macaddress= arp | grep -w "$ipRange$1" | awk '{print $3,$1}'
with macaddress=$(arp | grep -w "$ipRange$1" | awk '{print $3,$1}')

Resources