user1881957
user1881957

Reputation: 3378

How to find if the user have entered hostname or IP address?

The user will input either hostname or the IP address. If the user enters the IP address, I want to leave as it is but if the user enters the hostname I want to convert it into IP address using the following method:

def convert(hostname):
    command = subprocess.Popen(['host', hostname],
                           stdout=subprocess.PIPE).communicate()[0]

    progress1 = re.findall(r'\d+.', command)
    progress1 = ''.join(progress1)
    return progress1 

How do I do it?

Upvotes: 4

Views: 6841

Answers (4)

ankuj
ankuj

Reputation: 21

In my case, host name can only contain - as a separator. So you can uncomment and use it according to your requirement.

import re

regex = "^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$"
# string_check= re.compile('[@_!#$%^&*()<>?/\|}{~:.]')
string_check= re.compile('[-]')

ip_host_detail = {}
    
def is_valid_hostname_ip(IpHost):
    # pass regular expression and ip string into search() method
    
    if (re.search(regex, IpHost)):
        print("Valid Ip address")
        ip_host_detail['is_ip'] = 'True'
        ip_host_detail['is_hostname'] = 'False'
        return True
    elif(string_check.search(IpHost)):
        print("Contain hostname")
        ip_host_detail['is_hostname'] = 'True'
        ip_host_detail['is_ip'] = 'False'
            return True
    else:
        print("Invalid Ip address or hostname:- " + str(IpHost))
        ip_host_detail['is_hostname'] = 'False'
        ip_host_detail['is_ip'] = 'False'
        return False
    
    
IpHost = sys.argv[1]
# IpHost = 'RACDC1-VM123'

is_valid_hostname_ip(IpHost)
print(ip_host_detail)

Upvotes: 0

jfs
jfs

Reputation: 414205

To get ip whether input is ip or hostname:

ip4 = socket.gethostbyname(ip4_or_hostname)

Upvotes: 8

Matt Seymour
Matt Seymour

Reputation: 9395

There are a number of questions on stackoverflow already about validating an IP address.

  1. IP Address validation in python
  2. Validating IP Addresses in python

I would like to ask why you are communicating with a subprocess when you can do this within the standard python library.

I would recommend resolving a host name into a IP address by using some of pythons built in functionality.

You can do this by importing and using the python sockets library

For example using the code found in link 1:

import socket
import re
regex = re.compile(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$")
result = regex.match(address)
if not result:
    address = socket.gethostbyname(address)

Upvotes: 0

Samuele Mattiuzzo
Samuele Mattiuzzo

Reputation: 11038

you can use a regex to match your input and test if it is a ip address or not

test = re.compile('\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b')
result = test.match(hostname)
if not result:
    # no match -> must be an hostname #
    convert(hostname)

that regex allows invalid ip addresses (like 999.999.999.999) so you may want to tweak it a bit, it's just a quick example

Upvotes: 1

Related Questions