Python Forum
save the output from scapy to txt file
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
save the output from scapy to txt file
#1
hello all ...
some one is deleted my membership and my topic i don't know why ??
so ia'm new in python i want to modify this code after run and get the result on terminal save them in a .txt file

this is my code ...

lena = int(raw_input("Enter Number : "))
print(lena)

logging.basicConfig(format='%(asctime)s %(levelname)-5s %(message)s', datefmt='%Y-%m-%d %H:%M:%S', level=logging.DEBUG)
logger = logging.getLogger(__name__)

def long2net(arg):
    if (arg <= 0 or arg >= 0xFFFFFFFF):
        raise ValueError("illegal netmask value", hex(arg))
    return 32 - int(round(math.log(0xFFFFFFFF - arg, 2)))

def to_CIDR_notation(bytes_network, bytes_netmask):
    network = scapy.utils.ltoa(bytes_network)
    netmask = long2net(bytes_netmask)
    net = "%s/%s" % (network, netmask)
    if netmask < 16:
        logger.warn("%s is too big. skipping" % net)
        return None

    return net

def scan_and_print_neighbors(net, interface, timeout=1):
    logger.info("arping %s on %s" % (net, interface))
    try:
        ans, unans = scapy.layers.l2.arping(net, iface=interface, timeout=timeout, verbose=True)
        for s, r in ans.res:
            line = r.sprintf("%Ether.src%  %ARP.psrc%")
            try:
                hostname = socket.gethostbyaddr(r.psrc)
                line += " " + hostname[0]
            except socket.herror:
                # failed to resolve
                pass
            logger.info(line)
    except socket.error as e:
        if e.errno == errno.EPERM:     # Operation not permitted
            logger.error("%s. Did you run as root?", e.strerror)
        else:
            raise
if __name__ == "__main__":
    if lena == 1:
        for network, netmask, _, interface, address in scapy.config.conf.route.routes:
            # skip loopback network and default gw
            if network == 0 or interface == 'lo' or address == '127.0.0.1' or address == '0.0.0.0':
                continue

            if netmask <= 0 or netmask == 0xFFFFFFFF:
                continue

            net = to_CIDR_notation(network, netmask)
            if interface != scapy.config.conf.iface:
                # see http://trac.secdev.org/scapy/ticket/537
                logger.warn("skipping %s because scapy currently doesn't support arping on non-primary network interfaces", net)
                #continue
            if net:
                scan_and_print_neighbors(net, interface)
                repr(network)
                text_file = open("Output.txt", "w")
                text_file.write(repr(network))

            elif lena == 3 :
                print("Bye Bye  ")
Reply


Messages In This Thread
save the output from scapy to txt file - by evilcode1 - Oct-09-2017, 05:22 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Open/save file on Android frohr 0 437 Jan-24-2024, 06:28 PM
Last Post: frohr
  how to save to multiple locations during save cubangt 1 648 Oct-23-2023, 10:16 PM
Last Post: deanhystad
  save values permanently in python (perhaps not in a text file)? flash77 8 1,430 Jul-07-2023, 05:44 PM
Last Post: flash77
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,240 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Save and Close Excel File avd88 0 3,426 Feb-20-2023, 07:19 PM
Last Post: avd88
  Save multiple Parts of Bytearray to File ? lastyle 1 1,028 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  Permission issue when using scapy jao 3 10,543 Feb-05-2022, 06:14 PM
Last Post: snippsat
  Deploy Python to Cloud and save output to Google Drive chandrabr80 2 1,688 Jan-25-2022, 06:56 AM
Last Post: ndc85430
Sad Want to Save Print output in csv file Rasedul 5 11,480 Jan-11-2022, 07:04 PM
Last Post: snippsat
  How to save Matplot chart to temp file? Morkus 2 4,688 Jun-12-2021, 10:52 AM
Last Post: Morkus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020