Script getting IP's and MAC addresses


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script getting IP's and MAC addresses
# 1  
Old 11-04-2017
RedHat Script getting IP's and MAC addresses

Hy over there,

Lets make it simple Smilie

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:

Quote:
host s6 {
hardware ethernet aa:aa:aa:aa:aa;
fixed-address 192.168.0.254;
}

host s5 {
hardware ethernet xx:xx:xx:xx:xx:xx;
fixed-address 192.168.0.253;
}

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 Smilie
# 2  
Old 11-04-2017
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?
# 3  
Old 11-04-2017
RedHat

Quote:
Originally Posted by RudiC
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 Smilie

Quote:
sed '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/' ipmacmap >> forward.sh
Using the dhcp.conf file as input ! does it works?
I think the whole code should be changed, does it?

Thanks RudiC

Last edited by hermouche; 11-04-2017 at 12:16 PM..
# 4  
Old 11-04-2017
OK, let's combine the two. Try
Code:
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 sourceing:
Code:
. <(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:
Code:
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)"

# 5  
Old 11-04-2017
Quote:
Originally Posted by RudiC
OK, let's combine the two. Try

Code:
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 sourceing:
Code:
. <(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:
Code:
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):
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:
Code:
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:
Code:
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:

Code:
#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.


Moderator's Comments:
Mod Comment Please use CODE (not QUOTE) tags as required by forum rules!

Last edited by RudiC; 11-05-2017 at 06:07 AM.. Reason: Changed QUOTE to CODE tags.
# 6  
Old 11-05-2017
This is what I get when applying above sed script to the dhcp.conf given:
Code:
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
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;}' dhcp.conf


Last edited by RudiC; 11-05-2017 at 06:18 AM..
# 7  
Old 11-05-2017
Yeah, you got it RudiC, thanks a lot -Smilie

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Don't see my mac addresses of my devices

I have a problem with a script , i want to see my devices there are up in my network. I want as output the ip addresses of the devices and also the mac address but I only had the ip addresses #!/bin/bash while ] do -mac) Extension=5 shift mac=$1 shift ;; esac... (1 Reply)
Discussion started by: Roggy
1 Replies

2. UNIX for Dummies Questions & Answers

Script to ftp in to multiple ip addresses

Hello Is there an easy way to login to various ip's..one after the other. I need to login to about 30 aix boxes and put a file in each one... Cheers (1 Reply)
Discussion started by: Grueben
1 Replies

3. Windows & DOS: Issues & Discussions

Script to ftp in to multiple ip addresses

Hello Is there an easy way to login to various ip's..one after the other. I need to login to about 30 aix boxes and put a file in each one... Cheers (1 Reply)
Discussion started by: Grueben
1 Replies

4. UNIX for Dummies Questions & Answers

Difference between Mac and IP addresses

If a web search is done on this topic there are many links but no information included in the documents. So my questions are; Why does a NIC card have it's own permanent ID. Why can't it share the host name? An ID is an ID. Why does a computer need two ways to ID it. If the network... (3 Replies)
Discussion started by: theKbStockpiler
3 Replies

5. Shell Programming and Scripting

script to get all ip addresses of servers into a file

Hi all i need to create a script that pings every server in my range (0-254) adn then returns the values to a file? can anyone please help. i am working in the tcsh ( and yes i know how to ping ) but i dont know how to ping them all in one script without copying and pasting a 254 times? ... (1 Reply)
Discussion started by: brian112
1 Replies

6. Shell Programming and Scripting

Help with bash script to block IP addresses

I am using a bash script for CentOS 5.5, I found one and modified it, however I want to block the incoming IP addresses and ALLOW the IP addresses that are blocked to send out email. I will use an internal network range for an example, 10.10.10.0/24 (if a lot of spam is incoming) from this range... (0 Replies)
Discussion started by: grifs71
0 Replies

7. Shell Programming and Scripting

Need help with IP and MAC addresses

Hi, i am working on a project where i have to write a script to find out MAC addresses of the systems with given IP address. Can anybody tell me which command i can use to find MAC address if you know IP address of the machine. Thanks (5 Replies)
Discussion started by: manmeet
5 Replies

8. UNIX for Dummies Questions & Answers

Help extracting MAC addresses from List

Hello all. I have a large number of text files outputted from various Netstumbler Wireless Scans; from which I need to extract the MAC addresses of the various Access Points. The Text files look like this: # $Creator: Network Stumbler Version 0.4.0 # $Format: wi-scan summary with... (9 Replies)
Discussion started by: dhs23
9 Replies

9. Shell Programming and Scripting

Script to get IP addresses of LAN computers

I need a shell script for OS X, one that can find IP addresses of machines connected to my LAN, get the names of the computer associated with those addresses, then display them like so in a list: "Bob's L33T Boxx: #.#.#.#" Something like the network scanner in Apple Remote Desktop is what I'm... (1 Reply)
Discussion started by: sladuuch
1 Replies

10. UNIX for Dummies Questions & Answers

Mac addresses of NICs (why are they the same)

I am training to be support on our solaris based network and was wondering why solaris seems to assign the same mac address to all NICs placed in the machine regardless of how many NICs are in there, when i do a ifconfig -a all nics have same MAC. Presumably this is a feature and there is... (1 Reply)
Discussion started by: hcclnoodles
1 Replies
Login or Register to Ask a Question