Python Getting LAN IP Address of Machine on Ubuntu [duplicate] - python

This question already has answers here:
Python - Get localhost IP [duplicate]
(2 answers)
Closed 9 years ago.
So I'm trying to get the LAN IP Address of the machine the program is running on and compare it to IP Addresses passed to it via UDP.
However when I use:
print str(socket.gethostbyname(socket.gethostname()))
It returns 127.0.0.1 which should be 192.168.1.9.
I've looked through the linux machine and its getting the IP Address of the lo (loopBack) port? I don't know exactly what that is but it should be getting the IP Address of eth0.
I've found that I can subprocess the bash command "ifconfig eth0" but that returns a big block of a string. I can process it down to what I need, but this is going to be running around 3 times a second on a beaglebone so I'd like it to be a little more effecient.
Is there a more elegant way of doing this?
Can I just change the target of gethostname?
Why is it targeting the lo port?
Thanks for your help maners.

Try returning the fully qualified domain name of the machine:
print str(socket.gethostbyname(socket.getfqdn()))
/etc/hosts probably has an entry resolving hostname to 127.0.0.1, which is why socket.gethostbyname() doesn't return what you expect.
Original question asked and answered here, but the socket.getfqdn() solution didn't stick out at a quick glance. Here's the solution for parsing ifconfig output if you decide to go that route. Standard library seems more than sufficient for solving your problem.

netifaces seems like a pretty sweet python module which should do the trick for you.

Related

Sending a string between 2 computers on different networks with python

I have been trying to figure this out for a few days and I just cannot get it to work. I simply want to send a string from one computer to another one which are on 2 different networks using python. I have tried:
using python socket to connect to an ngrok address (not possible apparently)
pyngrok tcp server (requires me to pay for a reserved address)
and a few other methods but none of them work. If someone could please tell me the simplest, free way to go about this I would much appreciate it

Python - dispy Port 51347 seems to be used by another program

I am trying to run the program on the dispy page (http://dispy.sourceforge.net/) and I know this question is very similar to this one once asked (Port 51347 seems to be used by another program), but please do not file as duplicate because I have tried the answer given there and it doesn't fix the problem. I ran netstat and couldn't find any program running on the port 51347.
I tried running the program on several different ports, but every time i tried to run it using a localhost IP it kept giving me the same error 'Port xxxx seems to be used by another program'. I tried using it with a different IP and it didn't give me an error but then the code waits for an input from that IP rather than simply running locally.
Has anyone else had this problem that found a fix?

only accept certain ip/mac/ethtype packets in a socket

I;m a completely new in network programming and a starter in python.
I want so set a socket in Python to just accept certain packages.
I have the following values at my disposal:
destination address and port
source MAC address
ethtype (own)
how can I set a filter to my socket class to only accept packets for me (at dest address,port)
and/or the right ethtype, and/or send by the device with the known mac address ?
Any help would be greatly appreciated, I tried to look via Google, but the amount of hits is incredible, and I haven't been able to find an answer that solves my question so far.
Thanx,
Arthur
I would recommend you to use scapy. It is a great tool for crafting custom packets and to do lot of other stuff.
You can add filters in the scapy's sniff() to capture the packets you desire. You can also use scapy with your own python programm.
More over you'll find ton of tutorials on the internet on how to use scapy.

socket.gethostbyaddr() returns error on some computers and not for others

I've looked for any other threads related to this topic, but after an extensive search i was not able to find an answer that relates to my question. Using Python, I'm trying to use socket.gethostbyaddr("ip here") to determine the hostname of an ip address in a local network:
import socket
def gethostname(ip):
hostname = socket.gethostbyaddr(ip)
return hostname
For some computers (such as the server) this returns the triplet of hostname, alias and other IP's, but for others it does not. Instead, i get the following error:
socket.herror: [Errno 4] No address associated with name
What exactly does this error imply? What could it be that causes it? Is there any service or instane that should be running on the target computer in order for this to work? The computers i'm trying to get the hostname of run Debian.
If this question has already been asked then i am sorry, but i could not find it.
If it has something to do with reverse dns lookups, how would i solve this?
It means exactly what it says, there is no address associated. Not all IP addresses have a reverse-lookup address.

Availability of IP Address - Python

How to check the availability of an IP address in python?
For example, I wan't to change my system's IP address to 192.168.112.226 statically overriding the dhcp provided address. The default gateway is 192.168.112.1. But I wan't to check before if anyone is using 192.168.112.226 before assigning to myself.
Usually do this in command line from bash. I check with ping 192.168.112.226. If host is unreachable, I use 'ifconfig' and 'route' to assign it to myself.
How to automate this using python?
PS: I prefer python so that I can use python-notify to beautify the output whether success or failure.
This is so bad in so many ways I can't even explain how awfull this is.
Why do you want this? Could you please tell us that, and we could come up with a much better answer than this utterly uggly "sollution"?
If you have a Linux/Unix system, you can make your DHCP client to request the DHCP-server to give you a specific IP address if the DHCP server know it's free. How to do this depends on the distribution.
There are two problems I see that you will create with your "sollution".
As some other has written, you could check to see that the IP is "free" right now, but the machine that own that IP address might start right after your test. Using its IP address, wich you have kidnapped.
If the DHCP server don't know that you have kidnapped an IP address, it could give it out to someone else.
Whatever it will break the network for that computer and yours, generating lots of work, and possible anger for/to the network administrator. And you don't want that, do you?
Okay, if you want to use bash, you can import os module or subprocess module.
for example:
import os
command = os.system('pint 192.168.112.226')
if command == 0: #Sucess
#write os.system() and give it ifconfig and route commands as parameter.
else: print "This IP is used by another person in your network."
you can read more about os.system and subprocess in python, by importing them and writing help(subprocess) for example.
You can use socket.gethostbyaddr() to find if IP Address is being in use or not.
import sys, os, socket
# Stores the IP Address
ip_address = sys.argv[1]
try:
socket.gethostbyaddr(ip_address)
# If previous line doesn't throw exception, IP address is being used by someone
print "No"
except socket.herror:
# socket.gethostbyaddr() throws error, so IP is not being used at present
# You can write os.system() and give it ifconfig and route commands as parameter.
print "Yes"
The problem with this code is that method.gethostbyaddr() takes lot of time to throw socket.herror if IP address is not in use on the network.
If you name this script as isIPAvailable.py, then it can be called by:
python isIPAvailable.py 192.168.112.226

Categories