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 |
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}' |
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 |
Code:
ifconfig | awk '/HWaddr 00:0C/{print substr($1,length($1))}' |
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 |
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 |
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 Code:
# echo $interface |
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 |
Hai Colucix,
Thank u very much..extact answer... ~pooppp |
You're welcome! :)
|
All times are GMT -5. The time now is 10:55 PM. |