Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ShExBy/smshex_r.../opt/sharedra...
File: envinfo.py
#! /opt/imh-python/bin/python3
[0] Fix | Delete
''' Gather information about system environment '''
[1] Fix | Delete
[2] Fix | Delete
import os
[3] Fix | Delete
import sys
[4] Fix | Delete
import json
[5] Fix | Delete
from argparse import ArgumentParser
[6] Fix | Delete
[7] Fix | Delete
__maintainer__ = "Daniel K"
[8] Fix | Delete
__email__ = "danielk@inmotionhosting.com"
[9] Fix | Delete
__version__ = "0.1.1"
[10] Fix | Delete
__date__ = "2016-09-22"
[11] Fix | Delete
[12] Fix | Delete
[13] Fix | Delete
# Globals
[14] Fix | Delete
DATA = {"status": 'uninitiated', "version": __version__}
[15] Fix | Delete
[16] Fix | Delete
[17] Fix | Delete
class EnvInfoError(Exception):
[18] Fix | Delete
'''Custom error class for module.'''
[19] Fix | Delete
[20] Fix | Delete
def __init__(self, message):
[21] Fix | Delete
'''Init with call to base constructor.'''
[22] Fix | Delete
super().__init__(message)
[23] Fix | Delete
[24] Fix | Delete
[25] Fix | Delete
def get_data(min_version=None, max_version=None):
[26] Fix | Delete
'''
[27] Fix | Delete
Main function for module, used to acquire environment information.
[28] Fix | Delete
[29] Fix | Delete
min_version - If set, require envinfo.py
[30] Fix | Delete
to have a version greater than min_version
[31] Fix | Delete
[32] Fix | Delete
max_version - If set, require envinfo.py
[33] Fix | Delete
to have a version less than max_version
[34] Fix | Delete
[35] Fix | Delete
Returns dictionary of environment information. Currently, the only
[36] Fix | Delete
guaranteed entries are 'type', 'platform', and 'users'. The following
[37] Fix | Delete
are the keys that which may be present:
[38] Fix | Delete
[39] Fix | Delete
type - General type of environment. Can be 'cpanel' or 'unknown'
[40] Fix | Delete
platform - Operating system information
[41] Fix | Delete
web_server - Type of webserver. Currently only 'apache'
[42] Fix | Delete
users - List of users.
[43] Fix | Delete
[44] Fix | Delete
Apache related keys:
[45] Fix | Delete
[46] Fix | Delete
easyapache - Version of EasyApache if present
[47] Fix | Delete
apache_conf - Location of httpd.conf for Apache
[48] Fix | Delete
apache_domlogs - Location of Apache domlogs
[49] Fix | Delete
[50] Fix | Delete
On error will throw EnvInfoError.
[51] Fix | Delete
'''
[52] Fix | Delete
[53] Fix | Delete
if min_version is not None and min_version > __version__:
[54] Fix | Delete
raise EnvInfoError(
[55] Fix | Delete
'Minimum version {} is greater than current version {}'.format(
[56] Fix | Delete
min_version, __version__
[57] Fix | Delete
)
[58] Fix | Delete
)
[59] Fix | Delete
[60] Fix | Delete
if max_version is not None and max_version < __version__:
[61] Fix | Delete
raise EnvInfoError(
[62] Fix | Delete
'Maximum version {} is less than current version {}'.format(
[63] Fix | Delete
max_version, __version__
[64] Fix | Delete
)
[65] Fix | Delete
)
[66] Fix | Delete
[67] Fix | Delete
if os.path.isdir('/var/cpanel'):
[68] Fix | Delete
DATA['type'] = 'cpanel'
[69] Fix | Delete
else:
[70] Fix | Delete
DATA['type'] = 'unknown'
[71] Fix | Delete
[72] Fix | Delete
if os.path.isfile('/etc/apache2/conf/httpd.conf'):
[73] Fix | Delete
DATA['apache_conf'] = '/etc/apache2/conf/httpd.conf'
[74] Fix | Delete
DATA['apache_domlogs'] = '/var/log/apache2/domlogs'
[75] Fix | Delete
DATA['web_server'] = 'apache'
[76] Fix | Delete
if DATA['type'] == 'cpanel':
[77] Fix | Delete
DATA['easyapache'] = '4'
[78] Fix | Delete
elif os.path.isfile('/usr/local/apache/conf/httpd.conf'):
[79] Fix | Delete
DATA['apache_conf'] = '/usr/local/apache/conf/httpd.conf'
[80] Fix | Delete
DATA['apache_domlogs'] = '/usr/local/apache/domlogs'
[81] Fix | Delete
DATA['web_server'] = 'apache'
[82] Fix | Delete
if DATA['type'] == 'cpanel':
[83] Fix | Delete
DATA['easyapache'] = '3'
[84] Fix | Delete
[85] Fix | Delete
user_list = []
[86] Fix | Delete
[87] Fix | Delete
if DATA['type'] == 'cpanel':
[88] Fix | Delete
user_list = os.listdir('/var/cpanel/users/')
[89] Fix | Delete
elif os.path.isfile('/etc/passwd'):
[90] Fix | Delete
# If we don't know how else to find users, look to /etc/passwd
[91] Fix | Delete
if os.path.exists('/etc/passwd'):
[92] Fix | Delete
with open('/etc/passwd') as file_handle:
[93] Fix | Delete
try:
[94] Fix | Delete
for line in file_handle:
[95] Fix | Delete
uid = int(line.split(':')[2])
[96] Fix | Delete
if 500 <= uid < 32000:
[97] Fix | Delete
user_list.append(line.split(':')[0])
[98] Fix | Delete
except OSError as e:
[99] Fix | Delete
# Catch, and throw back IOErrors.
[100] Fix | Delete
raise EnvInfoError('Could not read from /etc/passwd') from e
[101] Fix | Delete
else:
[102] Fix | Delete
# No known way to find users
[103] Fix | Delete
raise EnvInfoError("Unable to make user list for server type.")
[104] Fix | Delete
[105] Fix | Delete
DATA['users'] = user_list
[106] Fix | Delete
[107] Fix | Delete
DATA['status'] = 'initiated'
[108] Fix | Delete
[109] Fix | Delete
return DATA
[110] Fix | Delete
[111] Fix | Delete
[112] Fix | Delete
def parse_args():
[113] Fix | Delete
'''
[114] Fix | Delete
Parse command line arguments
[115] Fix | Delete
'''
[116] Fix | Delete
[117] Fix | Delete
parser = ArgumentParser(description=__doc__)
[118] Fix | Delete
[119] Fix | Delete
output_group = parser.add_argument_group("Output options")
[120] Fix | Delete
[121] Fix | Delete
output_type_group = output_group.add_mutually_exclusive_group()
[122] Fix | Delete
[123] Fix | Delete
output_type_group.add_argument(
[124] Fix | Delete
"-e",
[125] Fix | Delete
"--environment",
[126] Fix | Delete
action='store_const',
[127] Fix | Delete
dest='output_type',
[128] Fix | Delete
const='env',
[129] Fix | Delete
help=(
[130] Fix | Delete
"Print general system environment information."
[131] Fix | Delete
"This is the defualt."
[132] Fix | Delete
),
[133] Fix | Delete
)
[134] Fix | Delete
[135] Fix | Delete
output_type_group.add_argument(
[136] Fix | Delete
"-u",
[137] Fix | Delete
"--users",
[138] Fix | Delete
action='store_const',
[139] Fix | Delete
dest='output_type',
[140] Fix | Delete
const='users',
[141] Fix | Delete
help="Print list of users rather than general environment.",
[142] Fix | Delete
)
[143] Fix | Delete
[144] Fix | Delete
output_type_group.add_argument(
[145] Fix | Delete
"--apache-conf",
[146] Fix | Delete
action='store_const',
[147] Fix | Delete
dest='output_type',
[148] Fix | Delete
const='apache_conf',
[149] Fix | Delete
help="Print location of Apache's httpd.conf.",
[150] Fix | Delete
)
[151] Fix | Delete
[152] Fix | Delete
output_group.add_argument(
[153] Fix | Delete
"-j", "--json", action='store_true', help="Print results in JSON"
[154] Fix | Delete
)
[155] Fix | Delete
[156] Fix | Delete
parser.add_argument(
[157] Fix | Delete
"-v",
[158] Fix | Delete
"-V",
[159] Fix | Delete
"--version",
[160] Fix | Delete
action='store_true',
[161] Fix | Delete
help="Print version information and exit.",
[162] Fix | Delete
)
[163] Fix | Delete
[164] Fix | Delete
args = parser.parse_args()
[165] Fix | Delete
[166] Fix | Delete
if args.version:
[167] Fix | Delete
print(f"Environment Information tool version {__version__}")
[168] Fix | Delete
print(f"Last modified on {__date__}.")
[169] Fix | Delete
sys.exit(0)
[170] Fix | Delete
[171] Fix | Delete
return (args.output_type, args.json)
[172] Fix | Delete
[173] Fix | Delete
[174] Fix | Delete
def main():
[175] Fix | Delete
'''Function to be called when run from the command line'''
[176] Fix | Delete
[177] Fix | Delete
(output_type, use_json) = parse_args()
[178] Fix | Delete
[179] Fix | Delete
try:
[180] Fix | Delete
data = get_data()
[181] Fix | Delete
except EnvInfoError as error:
[182] Fix | Delete
print("Error:")
[183] Fix | Delete
print(error)
[184] Fix | Delete
[185] Fix | Delete
if output_type == 'users':
[186] Fix | Delete
users = data['users']
[187] Fix | Delete
if use_json:
[188] Fix | Delete
[189] Fix | Delete
print(
[190] Fix | Delete
json.dumps(
[191] Fix | Delete
users, sort_keys=True, indent=4, separators=(',', ': ')
[192] Fix | Delete
)
[193] Fix | Delete
)
[194] Fix | Delete
[195] Fix | Delete
else:
[196] Fix | Delete
print("\n".join(users))
[197] Fix | Delete
elif output_type == 'apache_conf':
[198] Fix | Delete
if 'apache_conf' not in data:
[199] Fix | Delete
sys.stderr.write('No Apache configuration found\n')
[200] Fix | Delete
sys.exit(255)
[201] Fix | Delete
print(data['apache_conf'])
[202] Fix | Delete
else:
[203] Fix | Delete
if use_json:
[204] Fix | Delete
[205] Fix | Delete
print(
[206] Fix | Delete
json.dumps(
[207] Fix | Delete
data, sort_keys=True, indent=4, separators=(',', ': ')
[208] Fix | Delete
)
[209] Fix | Delete
)
[210] Fix | Delete
[211] Fix | Delete
else:
[212] Fix | Delete
print()
[213] Fix | Delete
print("Server Environment")
[214] Fix | Delete
print()
[215] Fix | Delete
print("Server Type: {}".format(data['type']))
[216] Fix | Delete
print(
[217] Fix | Delete
"OS: {}".format(" ".join(data['platform']))
[218] Fix | Delete
)
[219] Fix | Delete
print("Web Server: {}".format(data['web_server']))
[220] Fix | Delete
print("Apache configuration: {}".format(data['apache_conf']))
[221] Fix | Delete
print("Apache domain logs: {}".format(data['apache_domlogs']))
[222] Fix | Delete
print()
[223] Fix | Delete
print("Users:")
[224] Fix | Delete
print(" ".join(data['users']))
[225] Fix | Delete
print()
[226] Fix | Delete
[227] Fix | Delete
[228] Fix | Delete
if __name__ == "__main__":
[229] Fix | Delete
main()
[230] Fix | Delete
[231] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function