fhost entry tool class.
Implements a command-subcommand pattern wich is used as a main entry point of this tool.
A very basic CRUD is provided specifically create, list and remove commands to manage hosts.
Example:
usage: fhost [-h] [-v] {import,add,export,list,delete} ...
optional arguments:
-h, --help show this help message and exit
-v, --version
subcommands:
valid subcommands
{import,add,export,list,delete}
additional help
add Add new host
delete Delete existing hostname
export Export hosts as a backup
import Import hosts from a backup
list List available hosts
The above message shows the usage message help that fhost prints if no command is executed.
Add new host command.
Each host is composed of three parts:
- IP name
- Main hostname
- An alias to the main hostname.
Both three arguments are requied.
Results: | None |
---|
Delete existing hostname.
A valid hostname must be passed, no IP or alias are valid arguments.
Results: | None |
---|
List available hosts command.
No argument is required for this too.
Results: | list - A list of triplets ( ip, host, alias) hostnames. |
---|
Base Exception
This is the base exception. All other exceptions from fhost module inherits from this one.
Example:
try:
...
except BaseException:
raise BaseException("base exception raised!")
sys.exit(1)
Invalid Hostname
This exception is raised when some part of fhost code validates a hostname format.
As /etc/hosts file is quite delicated beacuse it can affect the whole system, fhost performs aggressive validations on all input and output data.
A common string message is shown to the user if this exeption raises.
Example:
try:
...
except InvalidHostnameException:
raise InvalidHostnameException("invalid hostname exception raised")
sys.exit(1)
Invalid IP
This exception is raised when some part of fhost code validates an IP address format.
As /etc/hosts file is quite delicated beacuse it can affect the whole system, fhost performs aggressive validations on all input and output data.
A common string message is shown to the user if this exeption raises.
Example:
try:
...
except InvalidIPException:
raise InvalidIPException("invalid ip exception raised!")
sys.exit(1)
A collection of Hosts
This is a collection of Hosts implemented as a Python container. It is iterable and have CRUD methods helpers to maintain a registry of available Hosts during fhost process execution.
It also have hardcoded the common path where /etc/hosts file is located.
In the future discover code will be implemented, both to detect the OS and its version and /etc/hosts location.
Add new hostname to the container.
Each host is composed by:
- IP address
- Main hostname
- Aliases to the main hostname
At this moment multiple aliases are accepted and inserted but only one is used and saved back.
Results: | None |
---|
Delete existing hostname.
A valid hostname must be passed, no IP or alias are valid arguments.
Results: | None |
---|
Check if this is a valid hostname
:rtype : object :param hostname: Example:
hostname = "example.com"
if isValidHostName(hostname):
print "Valid Hostname!"
else:
print "Invalid Hostname"
Results: | Boolean value that indicates if the hostname is valid. |
---|
Check if this is a valid IP
Example:
hostname = "173.194.34.208"
if isValidIP(hostname):
print "Valid IP!"
else:
print "Invalid IP"
Results: | Boolean value that indicates if the IP address is valid. |
---|