LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 03-13-2011, 10:20 AM   #1
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Rep: Reputation: 3
bash scripting and tr


Hello I am tring to get the MAC address from windows xp and remove it from a file on a unix box. This is what I have so far:

getmac | plink.exe -ssh -pw "my pw" -noagent -m commands.txt root@192.168.1.82

commands.txt
cd /root
tr '-' ':' | tr -d '[:blank:]'

now this is what I get

10:9A:CD:5F:04:8A\Device\Tcpip_{5A79FE8D:F227:4B91:9459:2701486C5D94}

All I need is the MAC. Everything after \Device* is not needed.

The file is /var/db/captiveportal_mac.db. The MAC address are listed like this.
MAC1
MAC2
MAC3
MAC4
MAC5
If there is anything that you can help with, I would love.

Last edited by zunder1990; 03-13-2011 at 10:30 AM.
 
Old 03-13-2011, 10:37 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Assuming I'm understanding your request, try:
Code:
cut -d '\' -f 1
Also, please use [code][/code] tags around your code, to preserve formatting and to improve readability.

Last edited by David the H.; 03-13-2011 at 10:40 AM. Reason: whoops, have to use single quotes to protect a backslash
 
Old 03-13-2011, 10:53 AM   #3
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Original Poster
Rep: Reputation: 3
Thank you for you help. I forgot about the cut command. Now what about taking the now clean MAC address and removing it from that .db file.
 
Old 03-13-2011, 11:02 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Code:
sed "/address/d" filename
..deletes all lines containing that address. Use the -i option to edit the file in place.

I'm still not completely sure what you're doing though. It would be better if we had actual examples of all the text involved, both before and after.

If you use a goal-oriented "I have A, and I want it to look like B" approach, rather than asking about using specific tools, you'll probably get better advice, as there are usually several ways to accomplish such tasks. I'd be willing to bet that you could do it all with a single sed or awk command, instead of a chain of smaller steps.
 
Old 03-13-2011, 11:27 AM   #5
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Original Poster
Rep: Reputation: 3
Can sed take input from a | or >.
What I am trying to do.
I am have 40 public computer at a local library. We need to show the user a page with a usage page before they can get online. We have been using pfsense 1.2.3 with pfsense captive portal and love it. We want the user to seat down at the computer and after opening IE to get a page, after hitting accept they can get on the internet. Pfsense will record the MAC address in a file on pfsense. I need something that will read the MAC for the computer, then login to pfsense and remove the MAC from the file.
 
Old 03-13-2011, 11:38 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Why don't you try it and find out? Or check out the man page?

Giving actual representative examples of the text you want to process is many times more useful than just explaining vaguely what you want to do.

And don't forget the [code][/code] tags.
 
Old 03-13-2011, 01:12 PM   #7
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Original Poster
Rep: Reputation: 3
This is what I have and I am stuck.



commands.txt
Code:
tr '-' ':' | tr -d '[:blank:]' | cut -c1-17 | sed -e /var/db/captiveportal_mac.db
My problem is with sed but I don't know how to fix it.
 
Old 03-13-2011, 08:12 PM   #8
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Original Poster
Rep: Reputation: 3
anyone got any help?
 
Old 03-14-2011, 09:33 AM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947


For the third time, we really need to see what the input text looks like. Knowing what commands you're using doesn't mean much if we don't know the format of the text they are being applied to.

Please show us everything you want to change, step by step, and in detail. Give us the exact output of the command(s) you're using and the exact contents of the text file(s) you want to process, before you've done anything to them, as well as how you want them to look after you're finished.
 
Old 03-14-2011, 10:33 AM   #10
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Original Poster
Rep: Reputation: 3
This is what win xp gives me after running "getmac"
Code:
00-26-B9-19-B8-0D   \Device\Tcpip_{2EA74C88-F5A0-4614-87F7-47B3E1713485}
Now I take that and send it to my unix box with
Code:
getmac | plink.exe -ssh -pw (my pw) -noagent -m commands.txt root@192.168.1.82
in commands.txt I have
Code:
cd /root
tr '-' ':' | tr -d '[:blank:]' | cut -c1-17 | sed -e -d /var/db/captiveportal_mac.db
under /var/db/captiveportal_mac.db the mac look like.
Code:
00:26:B9:19:B8:0D
00:50:56:C0:00:01
00:50:56:C0:00:08
C4:17:FE:2B:2F:CE
C4:17:FE:2B:2F:CE
00:FF:12:D2:ED:2B
Now I want the win xp to remove it's MAC from the mac.db file.

I have got all the way thur and after the
Code:
cut -c1-17
the output looks like
Code:
00:26:B9:19:B8:0D
. But what I cant get to work is sed to remove the MAC from the file. And I cant rewrite the file because more then one computer will run this script at one time.
 
Old 03-15-2011, 01:30 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Ok, that's starting to get us closer. But your procedure is still highly confusing.

1. You want to collect the mac address from the output of getmac. -- but how? Where and how do you actually store this output? And how are you running this anyway--from a cygwin terminal, for example?

2. You then send it to the unix box. -- how? Your commands.txt doesn't contain any of the output from above. So what is this file anyway? Is it a script that gets executed on the unix box? What exactly is it supposed to do?

If I'm following it, it's supposed to contain or retrieve the mac address somehow, then once transferred to the other box it's supposed to edit the mac.db file somehow. Am I correct?

By the way, the command you have there is completely wrong. sed can work either on the contents of a file, or on text coming into it from stdin, but not on both at the same time.

It seems to me that you really have to think this thing through a bit more carefully. Again, if I'm following you, what you really want to do is get the mac address from one computer, and use it to edit a file on a second computer. So there are really three separate operations involved:

a) Retrieve the mac address from the Win box.
b) Transfer it to the Unix box.
c) Use it to edit the db file on the Unix box.

So please tell me how you plan to handle each separate step.


Edit: One more thing. Your last line says you can't rewrite the file? How can you expect to edit the file if you can't rewrite it? That's what editing is, after all.


Edit2: Ok, I see now that you are trying to pipe the output of getmac straight into plink.exe, whatever that is. But does plink.exe even work that way? And even if it does, does it then also execute the commands stored in commands.txt and apply them to the input that it's getting from that pipe?

Somehow I really don't think it's supposed to work that way.

Last edited by David the H.; 03-15-2011 at 01:48 PM. Reason: as stated
 
Old 03-15-2011, 02:39 PM   #12
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Original Poster
Rep: Reputation: 3
Quote:
1. You want to collect the mac address from the output of getmac. -- but how? Where and how do you actually store this output? And how are you running this anyway--from a cygwin terminal, for example?
I run getmac under the windows command prompt. I dont store the info. I just run the command and then get all of the MAC of the local computer.

Quote:
2. You then send it to the unix box. -- how? Your commands.txt doesn't contain any of the output from above. So what is this file anyway? Is it a script that gets executed on the unix box? What exactly is it supposed to do?
After I get the output from "getmac", I send it to plink which it is part of putty. The commands.txt is under c:/. The commands.txt has the commands that should be ran after plinks logs in to the unix box.

Quote:
If I'm following it, it's supposed to contain or retrieve the mac address somehow, then once transferred to the other box it's supposed to edit the mac.db file somehow. Am I correct?
There is another program that run on the unix box that will add the MAC of a xp box to the mac.db file. What I need is for the xp box is to remove it's local MAC out of the mac.db file.


Quote:
a) Retrieve the mac address from the Win box.
getmac
Quote:
b) Transfer it to the Unix box.
using plink.exe
Quote:
c) Use it to edit the db file on the Unix box.
using tr, cut, and sed to rewrite the mac to the right format and then to remove it from the mac.db file.


Quote:
Edit: One more thing. Your last line says you can't rewrite the file? How can you expect to edit the file if you can't rewrite it? That's what editing is, after all.
I cant do something like this something "tr '-' ':' > mac.db" because I will have more then one computer running this code on the local xp using plink.exe. I think that if lets say that 4 xp computers run this script at the same time that the mac.db will get errors.

Quote:
Edit2: Ok, I see now that you are trying to pipe the output of getmac straight into plink.exe, whatever that is. But does plink.exe even work that way? And even if it does, does it then also execute the commands stored in commands.txt and apply them to the input that it's getting from that pipe?
I have the code working all the way up to the point when the MAC need to remove from the mac.db file. After the MAC goes " thur tr '-' ':' | tr -d '[:blank:]' | cut -c1-17 " get the just the MAC address.

What I need help with is how do I remove the MAC out of the mac.db file.
 
  


Reply

Tags
bash scripting, tr


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
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
help with bash scripting bourne Programming 3 10-21-2007 12:25 PM
new to bash scripting peok Programming 2 07-15-2006 02:46 AM
Bash Scripting Help Aioth Linux - Newbie 8 09-21-2004 11:21 AM
BASH scripting help Chucklez Linux - Newbie 4 12-12-2002 12:07 PM


All times are GMT -5. The time now is 08:40 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration