Script getting IP's and MAC addresses

Hy over there,

Lets make it simple :slight_smile:

Using a bash script, how to grep only the ip address and the mac address and put them in a file:.

for example from the dhcp.conf file we got such things:

The script will end up with two columns IP's and MAC adresses as it is showed below:

192.168.0.254 aa:aa:aa:aa:aa:aa
192.168.0.253 xx:xx:xx:xx:xx:xx:xx

Thanks for helping :slight_smile:

Do you need that file for any other purpose than your other thread? If not, why not skip it and use the dhcp.conf as immediate input for that?

Oh yes i need it for the other thread , it will be the ipmacmap file if you remember :slight_smile:

Using the dhcp.conf file as input ! does it works?
I think the whole code should be changed, does it?

Thanks RudiC

OK, let's combine the two. Try

sed -n '/hardware ethernet/ {s///; h; d;}; /fixed-address / {s///; G; s/[;\n]//g; s/ / -m --mac-source /; s/^/iptables -I FORWARD -s /; s/$/ -j ACCEPT/; p;}' dhcp.conf

Actually, you don't need a script to execute this. In a recent bash , you can deploy "process substitution" for the source ing:

. <(sed -n '/hardware ethernet/ {s///; h; d;}; /fixed-address / {s///; G; s/[;\n]//g; s/ / -m --mac-source /; s/^/iptables -I FORWARD -s /; s/$/ -j ACCEPT/; p;}'  dhcp.conf)

And, if you need this frequently, add an alias definition to your .profile :

alias FWD=". <(sed -n '/hardware ethernet/ {s///; h; d;}; /fixed-address /  {s///; G; s/[;\n]//g; s/ / -m --mac-source /; s/^/iptables -I  FORWARD -s /; s/$/ -j ACCEPT/; p;}'  dhcp.conf)"

I am sorry, RudiC,

The following is what i typed (code):

sed -n '/hardware ethernet/ {s///; h; d;}; /fixed-address / {s///; G; s/[;\n]//g; s/ / -p tcp -m multiport --dports 110,143,25,465,585,993,995,80,443 -m mac --mac-source /; s/^/iptables -I FORWARD -s /; s/$/ -j ACCEPT/; p;}' dhcpd.conf

This is my result so far:

iptables -I FORWARD -s  -p tcp -m multiport --dports 110,143,25,465,585,993,995,80,443 -m mac --mac-source                192.168.0.208                 30:10:B3:42:40:46 -j ACCEPT

This is how it should be:

iptables -I FORWARD -s 192.168.0.208 -p tcp -m multiport --dports 110,143,25,465,585,993,995,80,443 -m mac --mac-source 30:10:B3:42:40:46 -j ACCEPT

And that's part of my dhcpd.conf file:

#ddns-update-style interim;
ddns-update-style none;
ignore client-updates;
deny client-updates;
authoritative;

#### By red for PXE Booting
allow booting;
allow bootp;
### End by red

log-facility local6;

subnet 192.168.0.0 netmask 255.255.255.0 {
        # --- default gateway

        ##### By red for PXE booting
        class "pxeclients" {    match if substring(option vendor-class-identifier, 0, 9) = "PXEClient";
        next-server 192.168.0.1;
        filename "linux-install/pxelinux.0"; }
        #### End by red

        option routers 192.168.0.1;
        option subnet-mask 255.255.255.0;
        # option nis-domain             "domain.org";
        option domain-name "ensm.intranet";
        option domain-name-servers 192.168.0.1;
        option time-offset -18000;
        range dynamic-bootp 192.168.0.5 192.168.0.239;
        default-lease-time 3600;
        max-lease-time 7200;
        # we want the nameserver to appear at a fixed address

## by red: ICI on desire reserver des adresses IP fixes a des machines ##
group {
        use-host-decl-names true;


host s6 {
                hardware ethernet E8:50:8B:F3:98:F6;
                fixed-address 192.168.0.254;
                }


        host yacine {
                hardware ethernet E8:50:8B:E1:E9:35;
                fixed-address 192.168.0.253;
                }

        host hanane {
                hardware ethernet 3C:FA:43:B1:13:3A;
                fixed-address 192.168.0.252;
                }

Thanks.

This is what I get when applying above sed script to the dhcp.conf given:

iptables -I FORWARD -s 192.168.0.254 -p tcp -m multiport --dports 110,143,25,465,585,993,995,80,443 -m mac --mac-source E8:50:8B:F3:98:F6 -j ACCEPT
iptables -I FORWARD -s 192.168.0.253 -p tcp -m multiport --dports 110,143,25,465,585,993,995,80,443 -m mac --mac-source E8:50:8B:E1:E9:35 -j ACCEPT
iptables -I FORWARD -s 192.168.0.252 -p tcp -m multiport --dports 110,143,25,465,585,993,995,80,443 -m mac --mac-source 3C:FA:43:B1:13:3A -j ACCEPT

Anything special with your dhcp.conf ? What be your sed version?

EDIT: OK, after correcting the code tags (changed from QUOTE tags) we can see the leading whitespace in the dhcp.conf file that need to be taken care of.
HERE YOU CAN SEE THE IMPORTANCE OF SHOWING COMMANDS AND INPUT CHARACTER BY CHARACTER, KEY STROKE BY KEY STROKE IN CODE TAGS!
Try

sed -n '/^ *hardware ethernet/ {s///; h; d;}; /^ *fixed-address / {s///; G; s/[;\n]//g; s/ / -p tcp -m multiport --dports 110,143,25,465,585,993,995,80,443 -m mac --mac-source /; s/^/iptables -I FORWARD -s /; s/$/ -j ACCEPT/; p;}' dhcp.conf

Yeah, you got it RudiC, thanks a lot -:slight_smile:

I am jealous, i want to got your skills in scripting .

can you suggest any book, tutorials, .... for me.

Thank you very much for your patience with me. My english is not so good that's why my questions are not very explicit. i am sorry for that.

Well, i will apply what i've learned from you and any suggestions in coding are welcomed.

Thanks.
red

I hope you accept suggestions from me too:

Dale Dougherty: sed & awk, O'Reilly
Barry Rosenberg: Hands-On Korn Shell Programming, Addison-Wesley

I hope this helps.

bakunin

Of course you are welcomed :slight_smile:
Thank you sir.

In fact, I could not recommend any reading on awk etc. as I never read one EXCEPT for the respective man pages.
What helped me is a decent history of (production!) programming, deep curiosity and vivid competitive spirit to find optimal solutions, and exercise, exercise, exercise in varying directions. That's one of the reasons to participate in these forums - a challenging wealth of problems from diverging science fields to be solved . . . PLUS the comparison of my solutions to those of other witty guys in here. I've learned a lot!