I have been toying with a few ideas and would like to use Netbios to do some checks on the network. So after some research decided pysmb's nmb.Netbios was a good place to start.
I have constructed a simple queryName function that I was hoping would return an ip address. But it seems after checking some wireshark pcap dumps it isnt even broadcasting.
Ive found an example in the pysmb docs but that doesnt seem to broadcast either. Below is my test function, any pointers would be appreciated.
from nmb.NetBIOS import NetBIOS
def queryNam(name):
n = NetBIOS(broadcast=True, listen_port=0)
ip = n.queryName(name, timeout=30)
return ip
name = "Computer-Name"
ip = queryNam(name)
print ip
I worked out the issue myself. Initially I wasnt using the correct computername as NetBios Broadcasts seem to broadcast the name in uppercase. I was presenting a lowercase so the system was not responding.
So supplying the value in uppercase resolved the problem. (Even though a hostname check on the client showed an Uppercase followed by lowercase characters.
Related
I created a program, which sorts all connected Devices (Serials). I only want the List to get COMx Ports instead of their description.
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
List1 = []
for port in sorted(ports):
List1.append(port)
print(*List1)
It always shows the description too, and i don't know what to do?
Can anyone help me to solve this problem? Any Ideas?
I also read the pyserial documentation and tried to divide results into port, desc, hwid, didn't work...
If you are on Windows, you will be able to list only the COM port names with one of the following:
List1.append(port.name)
or
List1.append(port.device)
If you stick to the string named COMx, you can change comports() to:
ports = serial.tools.list_ports.grep("COM[1-9][0-9]*")
serial.tools.list_ports
classserial.tools.list_ports.ListPortInfo
device
Full device name/path, e.g. /dev/ttyUSB0. This is also the information returned as first element when accessed by index.
name
Short device name, e.g. ttyUSB0.
I've the following problem : I'm actually making a script for an ovirt server to automatically delete virtual machine which include unregister them from the DNS. But for some very specific virtual machine there is multiple FQDN for an IP address example:
myfirstfqdn.com IN A 10.10.10.10
mysecondfqdn.com IN A 10.10.10.10
I've tried to do it with socket in Python but it return only one answer, I've also tried python with dnspython but I failed.
the goal is to count the number of type A record on the dns server
Anyone have an idea to do stuff like this?
That's outright impossible. If I am in the right mood, I could add an entry to my DNS server pointing to your IP address. Generally, you cannot find it out (except for some hints in some protocols like http(s)).
Given a zone file in the above format, you could do something like...
from collections import defaultdict
zone_file = """myfirstfqdn.com IN A 10.10.10.10
mysecondfqdn.com IN A 10.10.10.10"""
# Build mapping for lookups
ip_fqdn_mapping = defaultdict(list)
for record in zone_file.split("\n"):
fqdn, record_class, record_type, ip_address = record.split()
ip_fqdn_mapping[ip_address].append(fqdn)
# Lookup
ip_address_to_lookup = "10.10.10.10"
fqdns = ip_fqdn_mapping[ip_address_to_lookup]
print(fqdns)
Note: Using socket can be done like so - Python lookup hostname from IP with 1 second timeout
However this does require that DNS server that you are querying has correctly configured PTR reverse records.
https://www.cloudns.net/wiki/article/40/
import netifaces as ni
ip = ni.ifaddresses("eth0")[ni.AF_INET]['addr']
error
ip = ni.ifaddresses("eth0")[ni.AF_INET]['addr']
ValueError: You must specify a valid interface name.
ip = ni.ifaddresses("en0")[ni.AF_INET]['addr']
error
ip = ni.ifaddresses("en0")[ni.AF_INET]['addr']
TypeError: list indices must be integers or slices, not str
Does anyone know why the mac is giving such errors?
The first error means that there is no interface named eth0. Indeed, this is a common interface name on Linux, but not on MacOS.
The second error means that you are trying to extract a field which doesn't exist. There is information about en0 but it is an array, not a dict. This is like saying "hello"["addr"], there is no way to access the "addr":th element of a sequence. You apparently mean something like
ip = ni.ifaddresses("en0")[ni.AF_INET][0]['addr']
though there is no way out of context to tell whether getting only one address is actually what you want. The array you get represents a number of bindings; perhaps you want all of them?
addrs = ni.ifaddresses('en0')
ips = [x['addr'] for x in addrs[ni.AF_INET]]
The netifaces documentation actually explains this in quite some detail.
I know that I can use email.utils.parseaddr to parse out an email address properly, even a tricksy one:
>>> parseaddr('Bad Horse <bad.horse#example(no one expects the #-ish inquisition!).com')
('Bad Horse (no one expects the #-ish inquisition!)', 'bad.horse#example.com')
(So good, Python! So good!)
However, that's just a string:
>>> type(parseaddr('Bad Horse <bad.horse#example(no one expects the #-ish inquisition!).com')[-1])
<class 'str'>
In the typical case I can just do .rsplit('#', maxsplit=1)[-1] to get the domain. But what if I'm just sending local mail without a domain?
>>> parseaddr('Wayne <wayne>')[-1].rsplit('#', maxsplit=1)[-1]
'wayne'
That's not quite what I want - I'd prefer maybe None or 'localhost'.
Does anything like that come in Python's included batteries?
I haven't been able to find anything yet, so my current approach is to make a slight adjustment:
try:
domain = parseaddr('Wayne <wayne>')[-1].rsplit('#', maxsplit=1)[1]
except IndexError:
# There was no '#' in the email address
domain = None # or 'localhost'
In the absence of a better way, this works and gets me what I need.
I am very new to system programming. I am trying to query some NIC information using Python with ioctl, I easily got the code but having some difficulty in understanding
Python code to get the ip address
nic = "eth1"
# Create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# **How did they conclude the formate to be 16sH14s and why 14 times \x00 ? Please advice**
ifreq = struct.pack('16sH14s', nic, socket.AF_INET, '\x00'*14)
result = fcntl.ioctl(fd, SIOCGIFADDR, ifreq)
# My wild guess **unpack format would be same as pack**. But I am wrong
ip = struct.unpack('16sH2x4s8x', result)[2]
print socket.inet_ntoa(ip)
Can somebody advice how to decide upon the format and why/how to decide upon number of null characters ?
This link seemed to be almost same question as mine but couldn't find my answer
http://www.unix.com/programming/148374-python-struct-pack.html
I found another way of creating ifreq.. ifreq = struct.pack('256s', self.iface). If possible please help me understand the difference.