LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   bash scripting and tr (http://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-and-tr-868299/)

zunder1990 03-13-2011 10:20 AM

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.

David the H. 03-13-2011 10:37 AM

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.

zunder1990 03-13-2011 10:53 AM

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.

David the H. 03-13-2011 11:02 AM

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.

zunder1990 03-13-2011 11:27 AM

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.

David the H. 03-13-2011 11:38 AM

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.

zunder1990 03-13-2011 01:12 PM

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.

zunder1990 03-13-2011 08:12 PM

anyone got any help?

David the H. 03-14-2011 09:33 AM

:banghead:

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.

zunder1990 03-14-2011 10:33 AM

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.

David the H. 03-15-2011 01:30 PM

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.

zunder1990 03-15-2011 02:39 PM

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.


All times are GMT -5. The time now is 12:17 PM.