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.
Related
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)
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)
Hy folks,
my problem: I want to start a (tftp) server for an non-existing IP-address.
the server is meant for USB/RNDIS where its IP-address by nature only is existing when there is actual network-traffic going on -- but I want to start the server 'early' (e.g. when Windows starts).
idea was to bind() the socket to 0.0.0.0 - and then to check each request for "valid" addresses.
problem with that approach: recfrom() only returns the source-address (client), but not the destination (server)!
how do I get the IP-address this client has talked to?
(I could of course check for the same subnet at least, but I need the real server-address this client was talking to!)
or, are there by chance any options to let bind() use a non-existing IP-address?
cheers.
p.s.
this is for the Python tftpy server...
-- at the moment I need to ping from client side when starting the server, which is quite meh...
There's no way to get the local address directly but there's a trick that will usually work for you.
Let's say that you just obtained a buffer and client address from recvfrom. Now you create a new auxiliary UDP socket, connect it to the client's address, and then use getsockname to obtain the local address on this new socket. (With UDP, connect doesn't actually send anything to the peer, it just does address resolution.)
So in this way, you can discover the IP address that the server system would use as source were it to send a datagram back to the client system. That's usually the same address that the client used to target the server.
>>> cbytes, caddr = server_sock.recvfrom(2048)
>>> print(caddr) # Client IP and port
('192.168.0.11', 38869)
>>> aux_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
>>> aux_socket.connect((caddr[0], 9999)) # Connect back to client (port doesn't matter)
>>> saddr = aux_socket.getsockname() # Get local IP and port (ignore port here too)
>>> print(saddr)
('192.168.0.12', 39753)
This works on linux. I'm not 100% sure it would work the same way on Windows but it seems reasonable to expect that it would.
So I'm using socket to connect clients to the server. For that, I need the computer's ip. Currently, the best way I found is this:
socket.gethostbyname(socket.gethostname())
I then use requests to tell my clients about my ip, and they connect. The issue here is that when my vpn is on, I get another host and that causes the clients to be unable to connect.
But when I open command prompt and type ipconfig, I get the correct ip regardless of the vpn status. So I need to get the same ip as would be shown under IPv4 in command prompt, is this possible in python?
I'm trying to get the server to work on any device regardless of exceptions such as this.
Thanks!
The way you retrieve the IP addresses of your system (most systems these days have multiple) uses the hostname of your system and therefore depends on i.e. DNS and your local hosts file. It will only give you one address, and can be quite unreliable, as you have seen with your VPN.
I'd recommend using the netifaces package. With it you can retrieve a list of all network interfaces and all their addresses.
An example from the manual:
>>> addrs = netifaces.ifaddresses('en0')
>>> addrs[netifaces.AF_INET]
[{'broadcast': '10.15.255.255', 'netmask': '255.240.0.0', 'addr': '10.0.1.4'}, {'broadcast': '192.168.0.255', 'addr': '192.168.0.47'}]
You should be able to install it with pip. The source repository is here: https://bitbucket.org/al45tair/netifaces
Yeah so I ran into this issue and it seems that ipconfig command does work, so I used the following. It calls ipconfig using subprocess and uses a regex pattern to match the ipv4 line. Since there's two ipv4 lines, and on my machine the VPN appeared as Unknown adapter WindscribeWireguard: before Wireless LAN adapter Wi-Fi: so I ended up matching the last one that appears. It's possible this isn't robust but I'm sure one of my users will let me know then.
from subprocess import check_output
import re
ipv4_pattern = re.compile(r'IPv4 Address.*:\s*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
def get_ipv4():
ipconfig_output = check_output(['ipconfig'], shell=True, text=True, encoding='iso8859-2')
return ipv4_pattern.findall(ipconfig_output)[-1]
I have a Python script that is running on a Linux server that has a dozen IP addresses associated with it. I implemented a TCPSServer from Python's socketserver library and had it listen on all network interfaces.
Several devices will be connecting to this server, and we need to be able to somehow capture the ip address of the destination (not the IP address of the client, but the IP address of the server that the client thinks it is connecting to). Right now, I can receive client connections, I can see the client IP, but I cannot figure out a method for obtaining the destination IP.
Does anyone know a method for capturing the destination IP on the socketserver class? It would seem if I can listen to multiple interfaces, there would be a way to tell which interface was hit.
This will be installed on several servers eventually, each with an unknown number of network interfaces. However, we do know that this will only exist on Linux bases systems. So if there was an OS specific way to do this, I would be fine with that as well.
If you have a socket object, you can use socket.getsockname() to obtain the IP address it's bound to. So something along the lines of:
# IPv4
client = listening_socket.accept()
(ipv4,port) = client.getsockname()
# IPv6
client = listening_socket.accept()
(address, port, flow_info, scope_id) = client.getsockname()
Never tested it on a multihomed server with a socket bound to all interfaces though - might return IPv4 0.0.0.0 or the IPv6 equivalent, for all I know, which wouldn't be all that useful.