LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 07-31-2008, 07:33 AM   #1
pooppp
Member
 
Registered: May 2008
Posts: 32

Rep: Reputation: 15
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
 
Old 07-31-2008, 07:47 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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}'
 
Old 08-01-2008, 07:15 AM   #3
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
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

Last edited by pooppp; 08-01-2008 at 07:29 AM.
 
Old 08-01-2008, 07:24 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 08-01-2008, 08:14 AM   #5
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
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
 
Old 08-01-2008, 09:38 AM   #6
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
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
 
Old 08-01-2008, 10:19 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 08-01-2008, 10:26 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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).
 
Old 08-04-2008, 05:03 AM   #9
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
Hai Colucix,
Thank u very much..extact answer...

~pooppp
 
Old 08-04-2008, 07:27 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
You're welcome!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shellscript to extract multiple archives quackyo Programming 13 04-27-2009 06:39 AM
Script for IFCONFIG tht PING with destination ip address in same format pooppp Linux - Networking 1 07-31-2008 11:19 AM
extract the IP address from ppp0 interface culin Programming 16 04-20-2007 01:59 PM
dhcp server starts up unwanted interface kirmet Fedora 2 10-22-2005 12:20 AM
RH 9, nvidia FX5600, x starts, can't get GNOME interface b2tech Linux - Newbie 2 10-04-2003 08:23 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration