LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-21-2009, 11:00 AM   #1
dafuzzbudd
LQ Newbie
 
Registered: May 2009
Posts: 3

Rep: Reputation: 0
Set MAC as variable with GREP


Im writing a bas hscript and im trying to get the user's MAC as a variable. I think this can be done with GREP. The macchanger command outputs "Your MAC is 00:00:00:00:00:00"

Is this possible? Thanks guys.
 
Old 05-21-2009, 12:38 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

There are several ways you can go. Here's one quick'n'dirty alternative:
Code:
/sbin/ifconfig|awk '/HWaddr/ {print}'
eth0      Link encap:Ethernet  HWaddr 00:40:F6:2C:6B:CC

/sbin/ifconfig|awk '/HWaddr/ {print $5}'
00:40:F6:2C:6B:CC
Statement two becomes:
Code:
MAC=`/sbin/ifconfig|awk '/HWaddr/ {print $5}'`
echo My MAC address is $MAC...
My MAC address is 00:40:F6:2C:6B:CC

Last edited by paulsm4; 05-21-2009 at 12:41 PM.
 
Old 05-21-2009, 07:22 PM   #3
dafuzzbudd
LQ Newbie
 
Registered: May 2009
Posts: 3

Original Poster
Rep: Reputation: 0
nice, thank you very much. i added my interface to the ifconfig line to get the specific MAC.

i don't exactly understand the syntax, can you quickly explain it for me please?
 
Old 05-21-2009, 07:32 PM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi, sure -

1. I happen to use "awk" instead of "grep".

2. I get the MAC address from this command:
Quote:
/sbin/ifconfig
eth0 Link encap:Ethernet HWaddr 00:40:F6:2C:6B:CC
inet addr:192.168.1.150 Bcast:192.168.1.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:399879 errors:0 dropped:10 overruns:0 frame:0
TX packets:30434 errors:1 dropped:0 overruns:1 carrier:0
collisions:2749 txqueuelen:100
Interrupt:10 Base address:0xe800

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:3924 Metric:1
RX packets:74159 errors:0 dropped:0 overruns:0 frame:0
TX packets:74159 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
3. "awk" does two things:
a) This finds the line(s) that match "HWaddr" (much like "grep"):
Quote:
awk ... /HWaddr/
b) This prints column 5 of the matching line(s):
Quote:
awk ... {print $5}
'Hope that helps .. PSM
PS:
Please feel free to click the little blue "thumbs up" icon if this was useful.
 
Old 05-22-2009, 08:33 AM   #5
dafuzzbudd
LQ Newbie
 
Registered: May 2009
Posts: 3

Original Poster
Rep: Reputation: 0
thanks for the explanation

sometimes the MAC sets to 12:23:45:67:78:89:00:00:00:00:00:00
is there any way to take the first 17 characters?

i looked up the options for awk and when i seeing some of the instructions linked together my head starts to spin.
 
Old 05-22-2009, 09:12 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by dafuzzbudd View Post
i looked up the options for awk and when i seeing some of the instructions linked together my head starts to spin.
see here for substr() function. head starting to spin is not an excuse for not trying. My head hurts too when i first used awk.
 
Old 05-22-2009, 11:07 AM   #7
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

In Linux generally, if you can imagine it, you can do it. Usually in many different ways - you get to pick your favorite ;-)

ANYWAY:
Ghostdog is correct. The awk "substr()" function is probably the best way to get the left 17 characters from a string. "printf ("%-17s", $1)" might be another way.

Here's a nice, relatively painless tutorial on "awk". It barely scratches the surface - but it'll get you started:

http://www.oracle.com/technology/pub...laney_awk.html


Thanx for the "thumbs up", too. Deeply appreciated!

Sincerely .. PSM
 
  


Reply



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
Redirecting grep output to a variable dimako83 Linux - Software 6 11-17-2008 07:22 AM
Grep a file with a variable from an array..... OldGaf Programming 6 06-15-2008 06:16 PM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM
Grep result passed to variable cornish Programming 5 11-15-2007 05:05 PM
How to use grep to search for a specific variable.. memo007 Linux - Software 4 02-24-2007 05:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 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
Open Source Consulting | Domain Registration