Improve hostnames tool
This commit is contained in:
parent
f404ea1d84
commit
904f579b2f
44
hostname.py
44
hostname.py
|
@ -1,44 +0,0 @@
|
|||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Give me a random hostname!
|
||||
|
||||
import sys, os
|
||||
import random
|
||||
|
||||
|
||||
def read_file(filename) :
|
||||
ret = []
|
||||
with open(filename, 'r') as f_in :
|
||||
for line in f_in.readlines():
|
||||
line = line.strip()
|
||||
if len(line) == 0 or line[0] in "#:!$%" : continue
|
||||
ret.append(line)
|
||||
return ret
|
||||
|
||||
|
||||
if __name__ == "__main__" :
|
||||
# Print 10 lines, 4 entries each line
|
||||
n = 10
|
||||
c = 4
|
||||
|
||||
args = sys.argv[1:]
|
||||
hostnames = []
|
||||
if len(args) == 0 :
|
||||
# From all files
|
||||
with os.scandir(".") as dirs:
|
||||
for entry in dirs :
|
||||
filename = entry.name
|
||||
if filename.endswith(".txt") :
|
||||
hostnames += read_file(filename)
|
||||
else :
|
||||
# Only from given files
|
||||
for filename in args :
|
||||
hostnames += read_file(filename)
|
||||
|
||||
|
||||
for i in range(n) :
|
||||
for j in range(c) :
|
||||
hostname = hostnames[random.randint(0, len(hostnames)-1)]
|
||||
sys.stdout.write("%-20s" % (hostname))
|
||||
sys.stdout.write("\n")
|
86
hostnames.py
Executable file
86
hostnames.py
Executable file
|
@ -0,0 +1,86 @@
|
|||
#!/usr/bin/python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Give me a random hostname!
|
||||
|
||||
import sys, os
|
||||
import random
|
||||
|
||||
|
||||
def read_file(filename) :
|
||||
ret = []
|
||||
with open(filename, 'r') as f_in :
|
||||
for line in f_in.readlines():
|
||||
line = line.strip()
|
||||
# ignore empty lines or comment
|
||||
if len(line) == 0 or line[0] in "#:!$%" : continue
|
||||
ret.append(line)
|
||||
return ret
|
||||
|
||||
def file_exists(filename) :
|
||||
return os.path.exists(filename)
|
||||
|
||||
def scan_files(path, suffix = ".txt") :
|
||||
ret = []
|
||||
with os.scandir(path) as dirs:
|
||||
for entry in dirs :
|
||||
filename = entry.name
|
||||
if filename.endswith(".txt") :
|
||||
ret.append(filename)
|
||||
return ret
|
||||
|
||||
def usage() :
|
||||
print("print hostname suggestions")
|
||||
print("Usage: %s [OPTIONS] [FILES...]" % (sys.argv[0]))
|
||||
print("OPTIONS")
|
||||
print(" -h, --help Print this help message")
|
||||
print(" -l, --ls Print source files")
|
||||
|
||||
if __name__ == "__main__" :
|
||||
# By default, print 10 lines, 4 entries each line
|
||||
n = 10
|
||||
c = 4
|
||||
|
||||
hostnames = []
|
||||
filenames = []
|
||||
|
||||
args = sys.argv[1:]
|
||||
for arg in args :
|
||||
if arg == "" : continue
|
||||
if arg[0] == '-' :
|
||||
if arg == "-h" or arg == "--help" :
|
||||
usage()
|
||||
sys.exit(0)
|
||||
elif arg == "-l" or arg == "--ls" :
|
||||
for filename in scan_files(".") :
|
||||
print(filename)
|
||||
sys.exit(0)
|
||||
else :
|
||||
filenames.append(arg)
|
||||
|
||||
# If no files are given, take all files
|
||||
if len(filenames) == 0 :
|
||||
filenames = scan_files(".")
|
||||
|
||||
# Load hostnames from files
|
||||
for filename in filenames :
|
||||
# Add txt if not yet present
|
||||
if not file_exists(filename) and not filename.endswith(".txt") :
|
||||
if file_exists(filename + ".txt") : filename += ".txt"
|
||||
if not file_exists(filename) :
|
||||
sys.stderr.write("file not found: %s\n" % (filename))
|
||||
sys.exit(1)
|
||||
hostnames += read_file(filename)
|
||||
|
||||
if len(hostnames) == 0 :
|
||||
sys.stderr.write("no hostnames found\n")
|
||||
sys.exit(1)
|
||||
|
||||
for i in range(n) :
|
||||
if len(hostnames) == 0 : break
|
||||
for j in range(c) :
|
||||
if len(hostnames) == 0 : break
|
||||
hostname = hostnames[random.randint(0, len(hostnames)-1)]
|
||||
hostnames.remove(hostname)
|
||||
sys.stdout.write("%-20s" % (hostname))
|
||||
sys.stdout.write("\n")
|
Loading…
Reference in a new issue