The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   applescript to just get the ethernet address fom en0 (http://hintsforums.macworld.com/showthread.php?t=74626)

demck85 07-02-2007 04:11 PM

applescript to just get the ethernet address fom en0
 
I've been working on an applescript to just get the ethernet address from ethernet 1 (en0) and put it on the clipboard. Here's what I've got so far:

tell application "Terminal"
do script with command "ifconfig en0 ether | pbcopy"
tell window 1
set background color to "black"
set cursor color to "red"
set normal text color to "yellow"
set bold text color to "red"
set title displays custom title to true
set custom title to "Ethernet Address"
end tell
quit
end tell
set clip to the clipboard


How do I trim to the result down to the ethernet address (xx:xx:xx:xx:xx:xx) only? OR, is there easier way...

cwtnospam 07-02-2007 04:27 PM

Try this:
do shell script "ifconfig | grep ether | sed -e 's/ether //g'"

demck85 07-02-2007 04:39 PM

that helps some, but i only need, then, is the top ethernet address and have on the clipboard ready to be pasted

tlarkin 07-02-2007 04:51 PM

Quote:

Originally Posted by demck85 (Post 390201)
that helps some, but i only need, then, is the top ethernet address and have on the clipboard ready to be pasted

well you can output it to a text file not sure if that is exactly what you want but its worth a shot maybe

using cwtnospam's script you can add one more pipe out to output it to a text file

Code:

ifconfig | grep ether | sed -e 's/ether //g | open -a -f

cwtnospam 07-02-2007 04:59 PM

Quote:

Originally Posted by demck85 (Post 390201)
that helps some, but i only need, then, is the top ethernet address and have on the clipboard ready to be pasted

Does this work?
do shell script "ifconfig | grep ether | sed -e 's/ether //g'"
set the clipboard to the result

demck85 07-02-2007 05:02 PM

Quote:

Originally Posted by cwtnospam (Post 390197)
Try this:
do shell script "ifconfig | grep ether | sed -e 's/ether //g'"

if i add a "| pbcopy" to this code, i can get three to the clipboard, but I only want the top one (which is en0)

cwtnospam 07-02-2007 05:07 PM

So you've got two ethernets? If you run this command in the terminal, what is the result?
ifconfig | grep ether

cwtnospam 07-02-2007 05:08 PM

By the way, it seems there are some extra characters left over after the sed command, so I did this in Applescript:

do shell script "ifconfig | grep ether | sed -e 's/ether //g'"
set x to the result
set y to characters 2 through 18 of x as string
set the clipboard to y

demck85 07-02-2007 05:09 PM

ether 00:17:f2:xx...
ether 00:17:cc:xx...
ether 00:19:xx...

demck85 07-02-2007 05:10 PM

ifconfig en0 | grep ether | sed -e 's/ether //g' | pbcopy

that will get me just the one, but there is some space in front of it, can that be removed

demck85 07-02-2007 05:17 PM

this what i have for the final code:

do shell script "ifconfig en0 | grep ether | sed -e 's/ether //g'"
set x to the result
set y to characters 2 through 18 of x as string
set the clipboard to y


thanks for all the help everyone, it's just something to help cut down on human error when entering address into DHCP

cwtnospam 07-02-2007 05:20 PM

Ok, since you only want the first line, use this:
do shell script "ifconfig | grep -m 1 ether | sed -e 's/ether //g'"

Of course, what you have works too! :o

H =:o) L G I 07-03-2007 02:16 AM

&
Quote:

Originally Posted by cwtnospam (Post 390218)
Ok, since you only want the first line, use this ...

And this is a saver applescript-way to do the same without spaces:
set AppleScript's text item delimiters to ":"
set IPAdresse to text items of (words 1 thru 6 of first paragraph of (do shell script "ifconfig | grep ether | sed -e 's/ether //g';)) as text
set AppleScript's text item delimiters to ""

but a lot easier (without spaces) is this pure-applescript-way:
primary Ethernet address of (system info)

Do you know how to get the IP-address in this form: "192.168.1.1"?

demck85 07-03-2007 07:52 AM

it's not necessary for me to get the IP. But, I think i got it now. This didn't seem hard to do, but it finding the right commands to do it.

primary Ethernet address of (system info)
set x to the result
set the clipboard to x


that works great and a little quicker

do shell script "ifconfig en0 | grep ether | sed -e 's/ether //g'"
set x to the result
set y to characters 2 through 18 of x as string
set the clipboard to y


that works great to, plus it allows me to make sure that i get the right ethernet and could modify it to get others easier.

cwtnospam 07-03-2007 07:54 AM

Quote:

Originally Posted by H =:o) L G I (Post 390306)
Do you know how to get the IP-address in this form: "192.168.1.1"?

Is this a test?

Sorry, I don't know a pure Applescript method:

Code:

do shell script "ifconfig en0 | perl -pe 's/ netmask/\n/g' | grep inet | perl -pe 's/\tinet //g'"

NovaScotian 07-03-2007 09:16 AM

I use this. It returns a record: {Inside:"192.168.1.xxx", Outside:"xxx:yyy:zzz:abc"}

Quote:

to getInOutIP()
set the_interface to 0
repeat
set ip_internal to ""
try
set ip_internal to do shell script ("ipconfig getifaddr en" & the_interface)
end try
if ip_internal is not "" then exit repeat
set the_interface to the_interface + 1
if the_interface = 5 then
set ip_internal to " unknown "
exit repeat
end if
end repeat
try
set ip_external to word -7 of (do shell script "curl http://checkip.dyndns.org")
if ip_external = "" then set ip_external to " unknown "
on error
set ip_external to " unknown "
end try
return {Inside:ip_internal, Outside:ip_external}
end getInOutIP

set IPA to getInOutIP()

H =:o) L G I 07-04-2007 06:17 AM

Quote:

Originally Posted by NovaScotian (Post 390336)
I use this. It returns a record: {Inside:"192.168.1.xxx", Outside:"xxx:yyy:zzz:abc"}

hey, that's cool. may i put this handler as a solution to my website?

and the "perl-sausage" from cwtnospam is also cool, but it dosn't work correctly! (it was no test ;) )
here is my solution: word 2 of (do shell script "ifconfig en1 | grep 'inet '")

and here my completed applescript-handler to get the internal ip:
on getInternalIP()
***repeat with interface from 0 to 5
******try
*********set myIP to word 2 of (do shell script "ifconfig en" & interface & " | grep 'inet '")
*********set result_check to true
******on error
*********set result_check to false
******end try
******if result_check then exit repeat
***end repeat
***if interface is 5 then
******return "unknown"
***else
******return myIP
***end if
end getInternalIP

cwtnospam 07-04-2007 09:06 AM

1 Attachment(s)
Quote:

Originally Posted by H =:o) L G I (Post 390571)
...and the "perl-sausage" from cwtnospam is also cool, but it dosn't work correctly!

You need to copy and paste it exactly as posted. Applescript will replace the \n and the \t with a carriage return and a tab, but it will work.

cwtnospam 07-04-2007 09:17 AM

I think you want to use this line

Code:

set myIP to (do shell script "ipconfig getifaddr en" & interface)
instead of this one:
set myIP to word 2 of (do shell script "ifconfig en" & interface & " | grep 'inet '")

NovaScotian 07-04-2007 09:18 AM

Quote:

Originally Posted by H =:o) L G I (Post 390571)
hey, that's cool. may i put this handler as a solution to my website?

Sure. Attribute it to Adam Bell, please, rather than NovaScotian

H =:o) L G I 07-04-2007 10:29 AM

all this solutions are needed, because the as-command:
IPv4 address of (system info)
only returns "missing value"
the reason for this is the virtual IP given from my router

now i found another solution for this problem:
last word of (do shell script "system_profiler -detailLevel basic | grep 'IPv4 Addresses'")
i think, this is the most dependable way, but also the slowest.

@NovaScotian (Adam Bell!): Thanx for your permission


All times are GMT -5. The time now is 05:51 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.