jonhadfield / python-hosts

a hosts file manager library written in python
MIT License
125 stars 33 forks source link

ipv4 host added to bottom of /etc/hosts and not in the ipv4 section #22

Closed Knowledge-Wisdom-Understanding closed 4 years ago

Knowledge-Wisdom-Understanding commented 5 years ago
from python_hosts.hosts import Hosts, HostsEntry

fz = "friendzone.red"
target = "10.10.10.123"

hosts = Hosts(path="hosts")
new_entry = HostsEntry(entry_type="ipv4", address=target, names=[fz])
hosts.add([new_entry])
hosts.write()

output:

127.0.0.1       localhost
127.0.1.1       kali.kali kali

#10.10.10.145   player.htb

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
10.10.10.123    friendzone.red

It's not a huge deal but for organizational purposes it would be nice if 10.10.10.123 friendzone.red test.htb was added either above or below #10.10.10.145 player.htb in the "ipv4" section. Also. I mentioned this in a closed issue, but if I run hosts.write() with a different host for the same IPV4 address, it doesn't append the new host to the existing line or anywhere for that matter. Being a library solely dedicated to working with the /etc/hosts file , it would be optimal to have the feature to append new hosts to an existing line. The CLI pyhostman doesn't work either for this purpose. You can use --force but that will remove the other hosts which is annoying as I'm writing a python script and in multiple functions it appends hosts to the /etc/hosts file for the same ipv4 address. I've already written this program in bash and it works great but i have to do a lot of if grep -q $htbdomain /etc/hosts; then sed -i $"/$rhost/ s/$/\t$htbdomain/" /etc/hosts etc.... Cool project by the way, but it could really use the appending feature

jonhadfield commented 5 years ago

Thanks for the feedback.
For the first item, I think the only practical solution would be to provide an option to add entries after a specific entry. I'll take a look to see if that's feasible without adding too much complexity or breaking it for others. For the second item, it sounds like you want a merge_names (for want of a better title) option, i.e. if adding a new item with an existing address, then merge/append specified names with all matching entries. Does that sound about right?

Knowledge-Wisdom-Understanding commented 5 years ago

Yes that sounds about right. Thanks for responding. The first item isn't as important as the second option ( to me anyways ) , yes the first item would be nice but I'm most interested in the ability to merge/append additional host names to the same ipv4 address line as they're discovered. In my recon project, I've tried to bundle as much host discovery as possible into domainFinder.py since i can only call hosts.add([new_entry]) once. It would be nice to be able to call a method such as hosts.append([existing_entry], [new_entry]) or hosts.merge([existing_entry], [new_entry]) for instance. Once I have some time, i might take a deep dive into this library and see if I can create this feature and submit a pull request however I'm still a novice and quite busy lately so I probably won't get around to doing so any time soon. Thanks again for getting back to me @jonhadfield . All the best,

jonhadfield commented 4 years ago

I've added a merge_names parameter to hosts' add method. It duplicates the functionality of 'force' (replace any existing items with same address) but merges the new names with those of the last matching (by address) entry that already exists. This is part of release 0.4.6 that's been uploaded to pypi.

Knowledge-Wisdom-Understanding commented 4 years ago

Awesome! @jonhadfield . Going to test it out now. Worked Like a Charm :) Thanks a million man. I really appreciate that you added this feature. Cheers 🍻