I want to write a short script using python that read my ip address conversely , so when I write 127.0.0.1 I should find as a result 1.0.0.127.
any help please
Try this
ip = '127.0.0.1'
ip = ip.split('.')
ip.reverse()
print('.'.join(ip))
If you want to preserve the original ip. it's very easy since strings are immutable in python assign it to a new variable and just call reversed on it instead of calling the list's reverse() so you don't alter the list also (if you want that)
ip = '127.0.0.1'
new = ip.split('.')
new = reversed(new)
print('.'.join(new))
print(ip)
You can use this :
def reverseIP(ip):
ip = ip.split(".")
ip.reverse()
return '.'.join(ip)
Example :
print(reverseIP("127.0.0.1")) # Prints 1.0.0.127
Related
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/
I have managed to compile two lists of IP addresses. used and unused ips as such
unused_ips = ['172.16.100.0/32', '172.16.100.1/32', '172.16.100.2/32', '172.16.100.3/32', '172.16.100.4/32', '172.16.100.5/32', '172.16.100.6/32', '172.16.100.7/32', '172.16.100.8/32', '172.16.100.9/32'...]
used_ips = ['172.16.100.1/32','172.16.100.33/32']
what I want to be able to do now is compare these lists and return the next free IP. in the above example the next ip would be 172.16.100.2/32, until it handed out all of those from 1 to 32 then it would hand out 34.
im not sure where to begin with this, I can convert these to IPv4Network objects if there is something built in for this but I couldn't find anything in documentation
Thanks
I'd keep a set of ipaddress objects and manipulate them to allocate and de-allocate the addresses, like so:
import ipaddress
def massage_ip_lists():
global unused_ips, used_ips
unused_ips = set(ipaddress.ip_address(ip.replace('/32', ''))
for ip in unused_ips)
used_ips = set(ipaddress.ip_address(ip.replace('/32', ''))
for ip in used_ips)
def allocate_next_ip():
new_ip = min(unused_ips - used_ips)
used_ips.add(new_ip)
return new_ip
unused_ips = [
'172.16.100.0/32',
'172.16.100.1/32',
'172.16.100.2/32',
'172.16.100.3/32',
'172.16.100.4/32',
'172.16.100.5/32',
'172.16.100.6/32',
'172.16.100.7/32',
'172.16.100.8/32',
'172.16.100.9/32']
used_ips = ['172.16.100.1/32', '172.16.100.33/32']
massage_ip_lists()
print(allocate_next_ip())
print(allocate_next_ip())
Note:
/32 is a nomenclature for IP networks, not IP hosts.
ipaddress objects are comparable, so functions like min() work on them.
172.16.100.0 is a perfectly valid IP address, depending upon the netmask. If you don't want to allocate it, either keep it out of unused_ips, or make the program aware of the netmask in use.
You want ips that are in unused but not used:
available_ips = [ip for ip in unused_ips if ip not in used_ips]
You want to sort them to get the one that's closest to zero. Naive sorting will not work as you have strings; 172.16.xxx.xxx is sorted higher than 172.100.xxx.xxx for example. You can convert the IPs into lists of numbers to sort them correctly.
import re
available_ips = sorted(available_ips, key=lambda ip: (int(n) for n in re.split(r'[./]', ip)))
If you're just trying to iterate through a list of the available ips, you could do something like this:
# Filter unavailable ips from the list of all ips
available_ips = set(unused_ips) - set(used_ips)
# Iterate through list of available ips
for ip in available_ips:
print(ip) # Or whatever you want to do with the next available ip
I'm working on python, how can I receive a website address? I have tried but it doesn't work.
I have this:
import socket
socket.gethostbyname(name)
but nothing happens.
The gethostbyname(hostname) method translates a host name to an IPv4 address format. The IPv4 address is returned as a string.
So the argument for this method has to be a host name like "www.python.org". Assign a hostname for the 'name' variable in string format in your code.
Ex. name = "www.python.org"
Hope this helps!
Is there a function in Python that determines if a hostname is a domain name or an IP(v4) address?
Note, the domain name may look like: alex.foo.bar.com or even (I think this is valid): 1.2.3.com.
One of the visual differences between IP and domain address is when you delete dots in the IP address the result would be a number. So based on this difference we can check if the input string is an IP or a domain address. We remove dots in the input string and after that check if we can convert the result to an integer or we get an exception.
def is_ip(address):
return address.replace('.', '').isnumeric()
Although in some IP-Representations like dotted hexadecimal (e.g. 0xC0.0x00.0x02.0xEB) there can be both letters and numbers in the IP-Address. However the top-level-domain (.org, .com, ...) never includes numbers. Using the function below will work in even more cases than the function above.
def is_ip(address):
return not address.split('.')[-1].isalpha()
I'd use IPy to test if the string is an IP address, and if it isn't - assume it's a domain name. E.g.:
from IPy import IP
def isIP(str):
try:
IP(str)
except ValueError:
return False
return True
I want to ssh to another node on my network as part of a larger python script, I am using pexpect which works when I do something like this:
session=spawn('ssh root#172.16.210.254')
I want to replace the address with a variable so I can cycle through addresses in a list however when I try:
address = "172.16.210.253"
session=spawn('ssh root#'address)
It doesn't work as using address in this way is invalid syntax. What is the correct syntax for this?
session=spawn('ssh root#' + address) to concatenate the strings