LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2018, 02:55 AM   #1
ericnyamu
LQ Newbie
 
Registered: Mar 2015
Posts: 6

Rep: Reputation: Disabled
Convert ipaddress into hostname


hey guys and girls.i have this python code and i would like to know how to make it output hostname instead of ipaddress.



Code:
import json
import sys

def parse_results(json_file):
    with open(json_file) as file:
        data = json.load(file)

        ips = {}    # dict of ip addresses

        """ colecting ip addresses from json """
        for line in data:
            ips[line["ip"]] = []

        """ searching results for specific ip address """
        for ip in ips:
            for line in data:
                if line["ip"] == ip:
                    ports_line = line["ports"][0]
                    ips[ip].append([ports_line["port"], ports_line["proto"], ports_line["status"]]) # extracting info about port

        print("Results:")
        for ip in ips:
            print(f"\t{ip}")
            for ports in ips[ip]:
                print(f"\t\tport: {ports[0]}/{ports[1]} is {ports[2]}")


if __name__ in "__main__":
    print("Author: Ero")
    print("Source: https://github.com/owca/")
    print("Contact: il@gmail.com")
    
    if len(sys.argv) != 2:
        print("\nJson file path is missing!")
        exit(0)

parse_results(sys.argv[1])

Last edited by ericnyamu; 08-22-2018 at 02:58 AM.
 
Old 08-22-2018, 03:23 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
https://docs.python.org/2/library/so....gethostbyaddr
 
1 members found this post helpful.
  


Reply

Tags
code, ipaddress, python



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Permission denied using ssh hostname@IPaddress akkki Linux - Newbie 6 11-06-2017 08:37 PM
How to change the ipaddress,hostname,mac number in Fedora 6 from Terminal me4linux Linux - Networking 1 03-24-2007 06:34 AM
ipaddress and hostname groverrajiv Linux - Networking 1 04-24-2004 08:29 AM
Changing hostname and ipaddress hemanth_13 Solaris / OpenSolaris 6 07-24-2003 07:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration