How do I get the MAC address of the interface I am sending packets with?
I am trying to create a custom ARP packet, and I need to include my own MAC in it. I can not seem to find a way to get it.
Take a look at the get_if_hwaddr() function.
Doc: https://scapy.readthedocs.io/en/latest/routing.html
This code may help you :
my_macs = [get_if_hwaddr(i) for i in get_if_list()]
Cheers,
K.
You can easily by:
from scapy.all import Ether
print(Ether().src)
This prints the MAC address of the default interface you're using.
The Netifaces Python Package provides a great amount of information about the interfaces you are working with.
>>> netifaces.ifaddresses('en0')
{18: [{'addr': '00:12:34:56:78:9a'}], 2: [{'broadcast':
'10.255.255.255', 'netmask': '255.0.0.0', 'addr': '10.16.1.4'}],
30: [{'netmask': 'ffff:ffff:ffff:ffff::', 'addr':
'fe80::123:4567:89ab:cdef%en0'}]}
http://alastairs-place.net/projects/netifaces/
Related
What I'm going to do is writing an script with python to take an excel file as an input and then read the number and description of interfaces of a switch which is written in there , and then ssh to a cisco switch and change the description with the values added before in excel .
could any body give me a hint?
Try checking netmiko module. I was able to do something close to what you require using netmiko but now I use ansible ios_command which is a lot more easier for a non programmer network engineer.
Start with Paramiko or Netmiko , Netmiko is a bit better version. I would also just rethink about the actual project where instead of thinking about one switch think about all of them and see if you have some universal thing which you need to do in all of your switches instead of one.
For this project you could do below.
1 . save date in CSV
2 . Open CSV file
3. Create a dictionary and Save interface name as key , and description as values
4. Create a list where you can save all your keys --> l = d.keys()
4. SSH to the sw via paramiko/Netmiko .
5. Run a loop in the list l
on each iteration send below commands
interface l[i]
description d[l[i]]
this will translate to below
interface eth1/1
description d['eth1/1'] ( d['eth1/1'] will be value/description of whatever you are gonna get from CSV)
If you really try to learn python then its a good start however if you are on a time crunch Ansible is easier option
I have been tald that i need to send the progrem that sent or got the packet (packet in scapy) and that I need to use netstat –nb so i guess netstat –nb does that' but i can't find anywhere how to use it on packet, most of the code i fount was this:
import os
output_command = os.popen("netstat -nb").readlines()
but i can't understen, it not chacking one packet.
can someone help me to find how i use "netstat -nb " one a packet for finding the progrem that sent or got the packet? (in python)
I found something on Github hope it helps you. netstat.py written here
this is how netstat works.
I am looking for a fast way to check if IP addresses are part of a list of CIDR notated IP ranges. I've seen examples before use netaddr like:
from netaddr import IPNetwork, IPAddress
for CIDR in CIDRLIST:
if IPAddress(row[0]) in IPNetwork(CIDR):
print('success')
However this solution is way too slow for my problem (800 IP ranges in CIDR and 500.000 IP adresses).
What could be a way to do this faster? I've read about using pytries, but I am not certain this is the solution.
Patricia/Radix tree/tries seem to be the answer. I found them by searching for algorithms for looking up routing tables.
There is a python implementation here.
A little later: I now have this working fine in Ruby:
require 'rpatricia'
require 'uoainfoblox'
ib = UoAIinfoblox.new ({'user' => 'xxxxx', 'password' => 'yyyy', 'host' => 'ipam.auckland.ac.nz'})
pt = Patricia.new
ib.get_networks('*roaming_network=true').each do |net, info |
pt.add(net)
end
puts "'130.216.66.65 #{ pt.include?('130.216.66.65')}"
puts "130.216.5.128 #{pt.include?('130.216.5.128') }"
Infoblox is an IP Management system and UoAInfoblox is a wrapper around their web api. So here I get a list of the roaming networks add them into a patricia tree and then check two IP addresses (that I know the status of).
Edit: I have just found out from a friend who uses python and who teaches networking in our CS department that he used the python radix module in his research scripts. I know he was processing very large amounts of data from a /8 darkenet for CAIDA.
I'd like to know what would be the best way to populate a Chef attribute in a cookbook with the last octet of the IP address.
Here is how I do it now. It seems to work; however,I'd like to know how I can improve it.
default['application']['host_ip'] = node['network']['interfaces']['eth0']['addresses'].keys[1]
default['application']['app_id'] = node['application']['host_ip'].split('.')[-1]
Thanks!
That looks fine. You might want some error handling since this will crash if there isn't an eth0, but that's up to you. You could also use node['ipaddress'] which is the IP on the default interface.
I have a third party dll, that is supposed to return machine name.
Sometimes it returns
\\John-PC
some other times it returns
\\192.168.1.120
and recently i discovered that it returns something like this
\\[ef80::32d6:2255:27dd:123c]
So what is the third option?
If it is not a bug or MAC orelse, could i convert it to \192.168.1.120 and get happy???
It is the IPv6 version of the IPv4 address 192.168.1.120
. It also amuses me how your DLL seems to be indecisive with what to return.
It's an IPv6 address. IPv6 allows you to leave out sections that are equal to 0, hence the ::.
The 3rd option is a IPv6 address