Python | Is there a way to get my IP address? - python

I'm trying to make a program that gets the IP address that the program is running on, is there a way of getting an IP address without using an API of some sort?

For your private ip:
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer IP Address is:" + IPAddr)
Python's socket module is a great module for "all those networking stuff", like getting IP address.
For public ip you'll need to use an external service. Read more about it: Getting a machine's external IP address with Python
For example you can use: https://pypi.org/project/publicip/ (didn't try it myself)

Related

How to use Python SMB connection when you don't know the server's IP?

I'm using PySMB right now:
https://pysmb.readthedocs.io/en/latest/api/smb_SMBConnection.html
and the SMBConnection.connect spec is problematic because it requires knowing the server's IP address.
What about a usage case where I don't know the IP address and looking up the IP address fails? I already went through the steps in https://apple.stackexchange.com/questions/10956/finding-the-remote-ip-address-used-by-a-mounted-smb-share although the server in question isn't a Bonjour service.
I tested using the smb address (that would normally be typed into the Finder's "Connect To Server" option in Mac OS) in the connect function and that didn't work.
Is there an alternative library that takes an SMB address (instead of IP address), or at least a canonical/proper way to translate that into an IP address for this? Either way, please post an example.
I was facing similar kind of issue, did u try making the following changes?
smb = SMBConnection(user_id, password, client, server_name, domain = domain, use_ntlm_v2=True, is_direct_tcp=True)
ip = socket.gethostbyname(server_name)
print(ip)
smb.connect(server_name, 445)

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.

How to get the public and private ip addresses using python flask when code is deployed?

I created a flask API. Whoever visits that API, I am able to track their IP address. In my situation, i am able to track the local ip from any system which is connected to my internet where I ran the code locally. I deployed the code when I ran the API. It is able to track the public address. I want both public and local IP addresses to be tracked after deployment.
Is there any way to track the local address along with the public address even after deployment using flask and python?
from netifaces import interfaces, ifaddresses, AF_INET
for ifaceName in interfaces():
addresses = [i['addr'] for i in
ifaddresses(ifaceName).setdefault(AF_INET, [{'addr':'No IP addr'}] )]
print '%s: %s' % (ifaceName, ', '.join(addresses))
import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
i tried all this but of no use not able to get my desired output
#app.route("/get_my_ip", methods=["GET"])
def get_my_ip():
return jsonify({'ip': request.environ['REMOTE_ADDR']}), 200
Result:10.0.0.1
After the code deployment I am able to get only the public IP. I need to track the local IP along with the public IP.
For example: if 10 different systems connected to one internet used the deployed api, I should get 10 different IP addresses but i am getting the public IP which is common to all. But I expect both public IP and local UP like 106.89.78.90(public IP) 10.0.0.1(private IP) from one system, then 106.89.78.90/10.0.0.2 from another system, and so on.
You need to set up your server (nginx/apache/caddy etc) so that it adds a special header X-Forwarded-For with actual IPs to every request.
Related:
Get IP address of visitors using Flask for Python

How to connect to a python socket server from a web browser using its ip address?

As part of the a class I wrote a code in python by importing the socket. Here is the code.
import socket
ms=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ms.bind(('',1234))
ms.listen(5)
while True:
con, addr=ms.accept()
if con:
print("Someone made a request!")
con.close()
ms.close()
It has no errors but to access the server from the browser when i type the ip address in the browser. What to do, am I doing something wrong?
I recommend two ways to do that, first by changing the port address in your python code to 80. ms.bind(('',80)) will do the trick. This works because the default port for the browser to communicate is 80.
The second way is to type in your ip address and the port address in your browser address bar. For example 127.0.0.1:1234 replace 127.0.0.1 with your server ip address.
I changed the ms.bind(('',80)) and it works when i type my ip address in the address bar. So foolish of me not to check out the official documentation.

Getting the ip address of an ftp server in python

I've connected to a database using ftplib as shown below.
import ftplib
sitename = 'ftp.ensembl.org'
connection = ftplib.FTP(sitename)
connection.login()
But because I want to use ssh with an argument of user#host, I believe I need the ip address of the server instead of the url of the ftp site. Is there any code to find and store the ip address of an ftp server within ftplib?
for ssh, you need to use paramiko : http://www.paramiko.org/
here :
http://docs.paramiko.org/en/1.15/api/client.html#paramiko.client.SSHClient.open_sftp
best regard
You can translate a host name to IP address easily in Python using the socket library:
>>> import socket
>>> socket.gethostbyname('www.microsoft.com')
'184.85.88.154'
>>> socket.gethostbyname('ftp.ensembl.org')
'193.62.203.85'
However, I suspect you don't need to do so, as ssh and other utilities will accept hostnames or IP addresses interchangeably. If they require an IP address only, it is usually documented as such.
The hostname ftp.ensembl.org is just a name for the IP address.
In absolute majority on TCP applications/libraries you can use hostname and IP address interchangeably (HTTP/web browser being notable difference).
So with any Python SSH library, you will be able to use the host name too, just as you can use it with ftplib.
That being said, to find an IP address, you can for example use the ping command:
C:\>ping ftp.ensembl.org
Pinging ftp-ensembl-ext.sanger.ac.uk [193.62.203.85] with 32 bytes of data:
Reply from 193.62.203.85: bytes=32 time=51ms TTL=48
Reply from 193.62.203.85: bytes=32 time=51ms TTL=48
So the IP address of your FTP server is 193.62.203.85.
Or google for "DNS lookup".
To address your actual problem: To use the SSH from Python, you do not need an IP address, but rather an SSH library.
Only of the most commonly used SSH libraries for Python is Paramiko.

Categories