LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   shellScript tht to extract the interface name if their HWaddr starts with 00:0C (https://www.linuxquestions.org/questions/linux-networking-3/shellscript-tht-to-extract-the-interface-name-if-their-hwaddr-starts-with-00-0c-659477/)

pooppp 07-31-2008 06:33 AM

shellScript tht to extract the interface name if their HWaddr starts with 00:0C
 
The below is the ifconfig interface. I need script tht should extract the interface name like eth1,eth2 etc., only if the interface's HWaddr should start with 00:0C

clue is tht all HWaddr is starting with 00:0C


eth1 Link encap:Ethernet HWaddr 00:0C:FC:00:23:5D
inet addr:1.1.1.3 Bcast:1.255.255.255 Mask:255.0.0.0


eth2 Link encap:Ethernet HWaddr 00:0C:FC:00:23:5E
inet addr:2.1.1.3 Bcast:1.255.255.255 Mask:255.0.0.0


I need to extract the interface name like eth1,eth2 for the above case

Help for me

colucix 07-31-2008 06:47 AM

Luckily the name of the interface and the HW address are on the same line in the output of ifconfig. So you can simply do:
Code:

ifconfig | awk '/HWaddr 00:0C/{print $1}'

pooppp 08-01-2008 06:15 AM

How to extrace the last number in interface name
 
Thanks for ur answer..
How to extract only the last number from interface name whose HW address starts with 00:0C

for eg:
After loaded my driver, if no. of interface is set as 4 in code..at tht in ifconfig -a will show 4 interface right like eth2,eth3,eth4,eth5
Now i want to print only the last number in the interface name whose HWaddr starts with 00:0C
if all four interface name's HW addr starts with 00:0C
I need o/p as :
2
3
4
5

colucix 08-01-2008 06:24 AM

Code:

ifconfig | awk '/HWaddr 00:0C/{print substr($1,length($1))}'
The awk command above prints out the last character of the first field in all the lines matching "HWaddr 00:0C". Just out of curiosity, what are you trying to do? Maybe there are some better ways.

pooppp 08-01-2008 07:14 AM

Help me...Shell script for ping
 
if i loaded my driver with 4 interfaces like eth2,eth3,eth4,eth5
my script should ping with all the interfaces like eth2,eth3,eth4,eth5 before unload it..
Is it possible to achive through array?
Help me..

Below is the idea i m having but syntax is not correct..just idea

insmod filename //Loading a Driver

m=4 //If No of interface for loaded driver is set in code as 4 just assigning 4 to m
n=1 //Initial value to a first interface eth2
k[m] //Array for 4 interface like eth2,eth3,eth4,eth5

k[z]=(ifconfig -a | awk '/HWaddr 00:0C/{print $1}') //This array have to store an interface name whose HW address starts with 00:0c
if all my interfaces HW address starts with 00:0C K array should store eth2,eth3,eth4,eth5

while [ $n != ($m+1) ] //loop for all interfaces
do
y=(Need to extract the last digit from interface name).In this case, eth1 is the interface name so, y should be 1
ifconfig k[n] y.1.1.3 up //making the particular interface "UP" & also assigning the ip.
ping -c10 y.1.1.2 //try to ping with dest of the same format
sleep 5
ifconfig k[n] down
n='expr $n+1'
done

rmmod xge

pooppp 08-01-2008 08:38 AM

line1:insmod ./xge.ko
line2: ifconfig -a | awk '/HWaddr 00:0C/{print substr($1,length($1))}'
line3: ifconfig eth$i $i.1.1.3 up
line4: ping -c10 $i.1.1.2
line5: sleep 20
line6: ifconfig eth$i down
line7: done
line8: rmmod xge.ko

Line 2 is the idea u gave..
1.Is line will print if my driver is loaded with 24 interfaces?
2.Try to modify script as follows:
a. if no. of interface is 24, line2 will give 1-24 right..so array is needed..
b. Process the array one by one tht means first take interface 1 (i.e., eth1)
line3 - line6 will something like below
line3: ifconfig eth1 1.1.1.3 up
line4: ping -c10 1.1.1.2
line5: sleep 20
line6: ifconfig eth1 down

Loop is neeed to process from 1-24.Second loop execution for eth2 will be like below:
line3: ifconfig eth2 2.1.1.3 up
line4: ping -c10 2.1.1.2
line5: sleep 20
line6: ifconfig eth2 down
Like tht it should do upto 24 pinging before unload a driver i.e.,before rmmod xge

In script: i is nothing but no. of interface whose HWaddr starts with 00:0C tht is decided at runtime.

Plz help me

colucix 08-01-2008 09:19 AM

You don't really need to assign an array if you're going to use a loop: you can explicitly loop over the result of a command. Arrays are very useful for a lot of tasks, so you can always have a look at the Advanced Bash Scripting Guide, chapter 26, to learn their usage. Anyway, you can do something like this
Code:

#!/bin/bash

for interface in $(ifconfig -a | awk '/HWaddr 00:0C/{print $1}')
do
  echo ifconfig $interface ${interface#eth}.1.1.3 up
  echo ping -c10 ${interface#eth}.1.1.2
  echo sleep 20
  echo ifconfig $interface down
done

Here, the shell loops over the output of the ifconfig/awk command, through command substitution. Then it performs tasks using the loop variable interface. To extract the number of the interfaces (provided they are all eth) I used parameter substitution and in particular I stripped out the leading eth from the strings eth0, eth1... eth24. For example:
Code:

# echo $interface
eth16

# echo ${interface#eth}
16

Also I put an echo statement in front of each line of code for testing purposes. This will output the commands inside the loop without actually executing them. Check the results, adapt the script to your needs, then strip out the echo commands.

colucix 08-01-2008 09:26 AM

Just a little alternative: if you want to extract the number of the eth interface and use it as the loop variable (as in your example) you can do something like this:
Code:

#!/bin/bash

for i in $(ifconfig -a | awk '/HWaddr 00:0C/{print gensub("eth","",1,$1)}')
do
  echo ifconfig eth$i $i.1.1.3 up
  echo ping -c10 $i.1.1.2
  echo sleep 20
  echo ifconfig eth$i down
done

Here the awk function gensub acts more or less as the parameter substitution in my previous code: it substitutes the first occurence of eth with an empty string, leaving the number alone (whatever be the number of digits).

pooppp 08-04-2008 04:03 AM

Hai Colucix,
Thank u very much..extact answer...

~pooppp

colucix 08-04-2008 06:27 AM

You're welcome! :)


All times are GMT -5. The time now is 10:55 PM.