Are hostnames permanent? Can I change it? - python

Sorry for the general questions but I am not deep into the subject and the info I am finding in the internet is very confusing to me. I am trying to retrieve data from a sensor that is connected to the wifi, and for it I need to call a function that has this sensor's IP Address as a parameter. Therefore, when the IP Address changes, I need to keep rewriting the IP Address on my script. I have found this way to get the IP Address of the sensor. Assuming my sensor's hostname is "mydevice".
hostname = "mydevice"
ip = socket.gethostbyname(hostname)
I found out the hostname of my sensor using an IP Scanner App on my phone to just get the info of the devices that are on my network. The problem is that if I take the sensor to another place and use another wifi, it doesn't show a hostname in this app anymore. I am very confused and therefore I have the following questions.
I know each device has a hostname. Is this hostname permanent and can it be used in different WiFi networks?
If the hostname is not permanent, can I somehow set it to be permanent?
If I cannot set the hostname permanently, can I somehow include code in my python script so that the ip address of my sensor is retrieved every time the script runs, in case there are any changes?
Thanks so much.

Related

Finding a device IP address within a different IP range from the machine IP address

We have couple of debugging tools that look for a device and they can find IP address of the device even if it's within a different IP range of current machine.
For instance, usr-vcom cannot find all usriot Lan to serial network which accessible.
I have three USR-TCP232-304 adaptor within IP range of 10.99.33.xxx and my laptop IP address is 120.34.76.347, but usr-vcom can find all the USR-TCP232-304. They have a configuration tool that even edit the config values and change parameters including the IP address of the adaptors if you wish.
I'd like to write a python code to do a same thing, not just for USR-TCP232-304 devices, I have other equipment with similar configuration tools capability.
I can connect to the instruments and control them if I know theirs IP addresses and ports but I don't know how to search for them especially outside of the IP range of the machine that runs the code. Appreciate any advice.
import library
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)
to obtain IP address of any range from an Hostname
2- You can find the IP address of any class and different network or IP range.
just install Advanced IP scanner Get IP scanner here.
3- You can also check from DHCP leases if you have router access if necessary.

Changing IP of python requests

How do I change the IP of HTTP requests in python?
My friend built an API for a website, and sometimes it blocks certain IP's and so we need to change the IP of the request... here is an example:
login_req = self.sess.post('https://www.XXX/YYY', params={...}
Now, each request that it sends, is through the computer's IP, and we need it basically to pass through an imaginary VPN.
Thanks for the help. If something isn't clear I will explain.
Short answer: you can't.
Long answer: it seems like you're misunderstanding how IP addresses work. Your IP address is the network address that corresponds to your computer - when you send a request to a server, you attach your IP as a "return address" of sorts, so that the server can send a response back to you.
However, just like a physical address, you don't get to choose what your IP address is – you live on a street, and that's your address, you don't get to change what the street is called or what your house number is. In general, when you send a request from your computer, the message passes through a chain of devices. For example:
Your computer --> Your router --> Your ISP --> The Server
In a lot of cases, each of these assigns a different IP address to whatever's below it. So, when your request passes through your router, your router records your IP address and then forwards the request through your ISP using its own IP address. Hence how several users on the same network can have the same IP address.
There are physical IP addresses, that correspond directly to devices, but there are a limited amount of these. Mostly, each Internet Service Provider has a few blocks of IP addresses that it can attach to things; an ISP can keep a specific IP address pointed to a specific computer all of the time, but they don't have to, and for many of their regular users, they don't.
Your computer has basically no power to determine what its own IP address is, basically. There's nothing python can do about that.
Your Question:
we need [the request] basically to pass through an imaginary VPN.
It'd be easier to actually requisition a real proxy or VPN from somewhere and push your request through it. You'd have to talk with your internet service provider to get them to set something like that up for you specifically, and unless you're representing a reasonably big company they're unlikely to want to put in that effort. Most python libraries that deal with HTTP can easily handle proxy servers, so once you figure it out it shouldn't be a problem.
You can use an IP address from https://www.sslproxies.org/
For example,
import requests
response=requests.get("yourURL", proxies={'https': 'https://219.121.1.93:80', 'http': http://219.121.1.93:80 "})
The IP addresses on that site are pretty crappy and sometimes don't work, so it would be best to find a way to constantly scrape IP addresses from the site so you have a couple to try. Check out this article: https://www.scrapehero.com/how-to-rotate-proxies-and-ip-addresses-using-python-3/
warning: These should not be used for sensitive information as they are not secure. Don't use those IP addresses unless you are ok with anyone in the world knowing what your're doing.

Exchange internal ip blindly using python

I would like to share my internal IP between two devices on a network (using python 3).
Let's say I have my phone, and my computer. Both connected to the same network. I need to run a client and server script to connect the two but in order to do that, my phone (client) needs the ip of the computer (server).
The IP of the computer changes all the time (school wifi, nothing I can do about it) and even so I would like this to work instantly when connected to a new router without having to manually enter the IP.
One more thing, due to the huge amounts of devices on the network, mapping every device and finding the computer name to get the IP will take too long for its purpose.
Please use DNS for the purpose, or assign static addresses to your devices, and use the defined static addresses in your scripts.
In case anyone was wondering. I take this question as unsolvable, but, in order to solve my issue, I have set my computer to upload its internal IP to a server each time it connects to a new network. My phone then reads the server.

Connecting to BACnet device giving single use socket address error

I've tried to look through posts to see if i could find anything but haven't managed to find it yet.
I'm running the SCADA BACnet device simulator on my localhost.I'm assuming the ip for the device is my localhost ip since the only IP i could find was in the deviceAddressBinding property which showed 192.168.x.xx
I am running through the bacpypes(python library for bacnet) tutorial for the SampleApplication and when I first tried running the tutorial, it stated that my .ini file ip is not a valid address in the context. So I tried to put in the simulator's settings to try and connect to the simulator and now it's giving me a Only one usage of each socket address (protocol/network address/port) is normally permitted error. Not quite sure where to go from here, am I perhaps missing something?
my .ini file:
[BACpypes]
objectName: Testing Device
address: 192.168.x.xx #tutorial came with 128.253.109.40/20
objectIdentifier: 123
maxApduLengthAccepted: 1024
segmentationSupported: segmentedBoth
vendorIdentifier: 123
foreignPort: 47808
foreignBBMD: 192.168.1.254
foreignTTL: 30
My simulator properties:
it seems to be a windows issue. Replaced :
this_application = WhoIsIAmApplication(this_device, args.ini.address)
with:
this_application = WhoIsIAmApplication(this_device, ('', 47808))
your sample application and BACnet simulators are opening a server socket. looks like both are using the same port number. you can change the port number in ini file as given below
address: 192.168.1.22:47809
by default the BACPypes sample applications run on the 47808 port number unless you explicitly mention in the configuration file.

Sending traffic from multiple source IPs Scapy

I am trying to send some traffic via python using scapy (on Ubuntu). I am using a range of source IPs (10.0.0.32/29). Everything seems to be working (at least I see the traffic in wireshark and it reaches my firewall) but I am having a problem completing the TCP handshake using the IP addresses that aren't the main IP of the eth0 adapter. Does anyone know if this is possible to do:
Source:
from scapy.all import *
import random
sp=random.randint(1024,65535)
ip=IP(src="10.0.0.234/29",dst="www.google.com")
SYN=TCP(sport=sp, dport=80,flags="S",seq=10)
SYNACK=sr1(ip/SYN)
my_ack=SYNACK.seq+1
ACK=TCP(sport=sp,dport=80,flags="A",seq=11,ack=my_ack)
send(ip/ACK)
payload="SEND TCP"
PUSH=TCP(sport=sp,dport=80,flags="PA",seq=11,ack=my_ack)
send(ip/PUSH/payload)
Because you are behind a NAT/router, you should check it allows you to use the full range of IPs. If it is running DHCP protocol, your eth0 will typically recieve a unique IP adress that will be the only routed in your private network.
Furthermore, you must ensure your kernel knows what IPs are attributed to it, else it will drop response packets. If you want to use the full range of IP, you have two choices :
Create virtual devices with virtual mac adresses, each requesting an IP through DHCP.
Configure your router so it statically routes the full IP table to your host, and alias each IP you intend to use
Once you have done that, there is no reason you wouldn't be able to syn/ack from your multiple source IPs. From distant server point of view, there wouldn't be any difference between what you are trying to do and several machines in a local network requesting a page at the same time.

Categories