Bash script who maps IP with MAC address


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script who maps IP with MAC address
# 1  
Old 11-01-2017
Bash script who maps IP with MAC address

Hy every body,

Unfortunately and without success, i want to write a bash script who maps a known IP addess to a known MAC address using iptables and for the FORWARD chain.

Within the DHCP server, i have assigned a fixed IP address to all clients based on their MAC addresses of their network interface cards,

I have a list of the used IP addresses.
I have a list of their MAC addresses.
I dropped the FORWARD chain.

The output of the script will be such as the following:

Quote:
iptables -A FORWARD -s 192.168.0.10 -m mac --mac-source xx:xx:xx:xx:xx:xx -j ACCEPT
So far this is what i found and did, but it's very generic (192.168.0.0/24).
I want to be very specific Smilie

Code:
for MAC in `cat macacceptfile`;  
do
  iptables -A FORWARD -s 192.168.0.0/24 -p tcp -m mac --mac-source $MAC -j ACCEPT
done



Then i dropped some IP's with a second script. These IP's that i'm droping are not allowed within the DHCP server.


Code:
#!/bin/bash
BLOCKDB=/etc/squid/ipblocked
IPS=$(grep -Ev "^#" $BLOCKDB)
for i in $IPS
do
  iptables -I FORWARD -s $i -j DROP

Is there a solution in order to match these two scripts (just one script who do the work)


Thanks a lot in advance for your help SmilieSmilie

Red


---------- Post updated at 01:37 PM ---------- Previous update was at 04:32 AM ----------

Well make it simple:

if i have these 6 ip addresses:
192.168.0.10-15

Each ip address belong to nic card which has a MAC address
xx:xx:xx:xx:xx:xx:xx
aa:aa:aa:aa:aa:aa:aa
........


Now, is it possible to match (map) for example 192.168.0.10 TO xx:xx:xx:xx:xx:xx using a bash script !!

Thanks for helping

Last edited by hermouche; 11-01-2017 at 03:38 PM.. Reason: Replaced quote tags with code tags
# 2  
Old 11-01-2017
You can try something like this:

Code:
while read ipaddress && read macaddress <&3
do
  echo "do something with ip $ipaddress that has mac $macaddress"
done < ipaddress.file 3<macaddress.file


Where:
Code:
$ cat ipaddress.file 
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15
$ cat macaddress.file 
xx:xx:xx:xx:xx:xx:xx
aa:aa:aa:aa:aa:aa:aa
...


Last edited by Scrutinizer; 11-01-2017 at 04:53 PM..
# 3  
Old 11-02-2017
Without understanding what the request be, I'd be surprised if you could assign IPs amd MACs randomly reading from two independent files.
You should either read and use the DHCP config file, or the actual DHCP server's tables to find relations between the two.
# 4  
Old 11-02-2017
RedHat Bash script who maps IP with MAC address

Quote:
Originally Posted by RudiC
Without understanding what the request be, I'd be surprised if you could assign IPs amd MACs randomly reading from two independent files.
You should either read and use the DHCP config file, or the actual DHCP server's tables to find relations between the two.
Thanks a lot RudiC & Scruticizer for your reply,

Well,
1- I've got all the IP addresses with their respective MAC addresses in the DHCP server

2- I can also edit a file where i got two columns, one for the IP and the other for the MAC:

Code:
192.168.0.10    xx:xx:xx:xx:xx:xx
       192.168.0.11    aa:aa:aa:aa:aa:aa
        ................................................

Now either with the dhcp server or with the two columns file, is it possible to have at the end of the day, using a bash script something like:

Quote:
iptables -I FORWARD -s 192.168.0.10 -m --mac-source xx:xx:xx:xx:xx:xx -j ACCEPT
iptables -I FORWARD -s 192.168.0.11 -m --mac-source aa:aa:aa:aa:aa:aa -j ACCEPT
Sorry for my very basic english Smilie

Thanks again for your interest and reply SmilieSmilie

red

Last edited by rbatte1; 11-03-2017 at 08:33 AM..
# 5  
Old 11-02-2017
Given a file as shown under item 2-, try
Code:
sed 's/ / -m --mac-source /; s/^/iptables -I FORWARD -s /; s/$/ -j ACCEPT/' file
iptables -I FORWARD -s 192.168.0.10 -m --mac-source xx:xx:xx:xx:xx:xx -j ACCEPT
iptables -I FORWARD -s 192.168.0.11 -m --mac-source aa:aa:aa:aa:aa:aa -j ACCEPT

Should this NOT satisfy your needs, please become way more precise & detailed with your specification.
# 6  
Old 11-02-2017
Quote:
Originally Posted by RudiC
Given a file as shown under item 2-, try
Code:
sed 's/ / -m --mac-source /; s/^/iptables -I FORWARD -s /; s/$/ -j ACCEPT/' file
iptables -I FORWARD -s 192.168.0.10 -m --mac-source xx:xx:xx:xx:xx:xx -j ACCEPT
iptables -I FORWARD -s 192.168.0.11 -m --mac-source aa:aa:aa:aa:aa:aa -j ACCEPT

Should this NOT satisfy your needs, please become way more precise & detailed with your specification.
OK, thank you very much RudiC,

It shows the match between the IP and the MAC Smilie

Now i want the result of your code to be part of the firewall. I mean with the following command:

Quote:
# iptables -L FORWARD
i should see:

Quote:
iptables -I FORWARD -s 192.168.0.10 -m --mac-source xx:xx:xx:xx:xx:xx -j ACCEPT
iptables -I FORWARD -s 192.168.0.11 -m --mac-source aa:aa:aa:aa:aa:aa -j ACCEPT
Thanks again for your help RudiC Smilie
# 7  
Old 11-02-2017
Not sure I understand...?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to write a value to a physical memory address in bash script?

How would I write a value to a physical memory address? I was able to read a physical memory address (for example, 0x400) using this line: dd if=/dev/mem count=4 bs=1 skip=$(( 0x400 )) But I get an error: dd: 'standard input': cannot skip to specified offset when I try to write using... (1 Reply)
Discussion started by: rabrandt
1 Replies

2. IP Networking

MAC Address - Four Interfaces with the same MAC Address

four interfaces with ifconfig all interfaces have the same mac. If is not set for unique. but it still works. what difference does it make to have all macs the same or different? (4 Replies)
Discussion started by: rrodgers
4 Replies

3. Shell Programming and Scripting

Script extracting ip address from MAC wifi card

Hi Everybody, Goal: From my backup box on my local network, knowing the Wifi MAC address of my laptop, I would like to dynamically identify which ip address is attributed to my laptop. The aim is to store this ip address in a local variable and that this information is retrieved by another... (11 Replies)
Discussion started by: freddie50
11 Replies

4. Shell Programming and Scripting

IP Address LookUp Bash Script

I am new to bash scripting. I want write a script that reads from the first argument file and run nslookup, then prints out each nslookup. Something like below: File name = ip 8.8.8.8 8.8.4.4 Bash shell script: nslookup.sh #!/bin/bash for i in $1 do nslookup $i done I... (7 Replies)
Discussion started by: boldnbeautiful
7 Replies

5. OS X (Apple)

OpenStreetMap client (with offline maps) for Mac OS X

Hi I am searching for an OpenStreetMap client (with offline maps) for Mac OS X. Like Google Earth client, but with offline maps. (2 Replies)
Discussion started by: slashdotweenie
2 Replies

6. IP Networking

Tracing a MAC address to IP address: Solaris

Hi there I lost connectivity to one of our remote systems and when I checked the messages log I found the following: Aug 10 23:42:34 host xntpd: time reset (step) 1.681729 s Aug 16 13:20:51 host ip: WARNING: node "mac address" is using our IP address x.x.x.x on aggr1 Aug 16 13:20:51 host... (9 Replies)
Discussion started by: notreallyhere
9 Replies

7. Shell Programming and Scripting

bash: convert mac address to 16 character format

Hi there Im not quite sure how i can do this, but i am retrieving the mac address from boxes, which in some instances is arriving in its shortened format (i.e. dropping the leading zeros)... for example 0:3:BA:1:E:84 Im trying to figure out a way of converting the single character... (3 Replies)
Discussion started by: rethink
3 Replies

8. Shell Programming and Scripting

trim last octate of ip address using bash script

Hi, i need to replace the last octate in ipaddress with 0 using bash shell for one of my applicatiom. googling i found the below link where they do the same thing but use long2 ip which i dont see in linux. trim ip address octet - Stack Overflow Plz can soemone guide how do i do this... (5 Replies)
Discussion started by: akshatha
5 Replies

9. Shell Programming and Scripting

script to compare two files of mac address

Hi I need to write a bash shell script. I have two separate text files. One file contains a list of MAC addresses taken from a network scan, the other contains a list of MAC addresses for our currently-managed devices. How can I compare these two files, and output a list of addresses that have... (6 Replies)
Discussion started by: borderblaster
6 Replies

10. IP Networking

How to Achive IP address through MAC(Ethernet) address

Hi sir, i want to make such programe which takes MAC(Ethernet) address of any host & give me its IP address....... but i'm nt getting that how i can pass the MAC address to Frame........ Please give me an idea for making such program... Thanks & regards Krishna (3 Replies)
Discussion started by: krishnacins
3 Replies
Login or Register to Ask a Question