0

I have a text file contains domains and IPs, I want to get the IP using the domain name but it returns null.

Here is my code:

import json
import os
import sys
import pathlib
import platform
HERE = pathlib.Path(__file__).parent
def clean_server_names(name):
    server_names_list = [x.lower().strip() for x in
                            open(HERE / "servers" / "myservers.txt", 'r').readlines()]
    for server in server_names_list:
        domain_name_and_code = server.split(' ')
        domain = domain_name_and_code[0].strip()
        ip = domain_name_and_code[-1].strip()
        if name ==  domain:
            print(ip)
clean_server_names("domain1.nordvpn.com ")

Here's what inside my text file.

domain1.nordvpn.com 192.168.1.1
6
  • pls check your if condition if name == domain: how is the name initialized ? name still contains domain1.nordvpn.com 192.168.1.1. so equal will not work. Commented May 6, 2021 at 15:29
  • what do you mean? Commented May 6, 2021 at 15:35
  • so equal operator will not work? Commented May 6, 2021 at 15:43
  • yes, pls. you have space in name clean_server_names("domain1.nordvpn.com ") see the last space after .com. so you can either remove space when you pass the parameter or strip the space ` if name.strip() == domain:` Commented May 6, 2021 at 16:07
  • I already put the strip, but still not working, Please help me Commented May 6, 2021 at 16:12

1 Answer 1

1

You have a lot of code in there that's not needed and is cluttering up what your intent is.

$ cat ./x.py                                                                                                                   
#! /usr/bin/env python3
# -*- coding: UTF8 -*-

def clean_server_names(name):
    with open("servers/myservers.txt", "r") as f:
        for line in f:
            domain_name, ip = line.lower().split()
            if name == domain_name:
                print(ip)

clean_server_names("domain1.nordvpn.com")

$ cat ./servers/myservers.txt                                                                                                  
domain1.nordvpn.com 192.168.1.1

$ ./x.py                                                                                                                       
192.168.1.1

Simpler:

$ awk '$1 == "domain1.nordvpn.com"{print $2}' servers/myservers.txt
192.168.1.1
Sign up to request clarification or add additional context in comments.

3 Comments

still not working, but thanks for the help
it retuns like this ['\x00c\x00h\x003\x001\x008\x00.\x00n\x00o\x00r\x00d\x00v\x00p\x00n\x00.\x00c\x00o\x00m\x00', '\x008\x009\x00.\x001\x008\x007\x00.\x001\x006\x005\x00.\x003\x009\x00'] ['\x00'] ['\x00']
updating to show exactly what's in there and the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.