Created
May 13, 2024 07:12
-
-
Save vxcute/47ea2ab8ac31bfc426ecf53e213b8eca to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
a python script to set a static IPv4 address to a network interface but first checks if its unique | |
by doing an ARP probe. | |
''' | |
from scapy.all import * | |
import socket, fcntl | |
import os | |
import argparse | |
def set_if_addr(iface, ip): | |
SIOCSIFADDR = 0x8916 | |
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
bin_ip = socket.inet_aton(ip) | |
ifreq = struct.pack('16sH2s4s8s', bytes(iface, 'utf-8'), socket.AF_INET, b'\x00'*2, bin_ip, b'\x00'*8) | |
fcntl.ioctl(sock, SIOCSIFADDR, ifreq) | |
def is_ipaddr_used(ip): | |
ether = Ether() | |
probe = Ether(src=ether.src, dst='ff:ff:ff:ff:ff:ff')/ARP(op=1, hwsrc=ether.src, pdst=ip) | |
resp = srp(probe, timeout=2, verbose=False)[0] | |
return len(resp) != 0 | |
parser = argparse.ArgumentParser( | |
prog = 'ssetip', | |
description='sets a static ip address but first checks if its not used by doing an arp probe') | |
parser.add_argument('-ip', "--ip") | |
parser.add_argument('-if', "--iface") | |
args = parser.parse_args() | |
if is_ipaddr_used(args.ip): | |
print(f"[+] {args.ip} is used") | |
else: | |
print(f"[+] {args.ip} is unique ...\n[+] Setting {args.iface} address to {args.ip}") | |
set_if_addr(args.iface, args.ip) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment