-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathadd_2env.py
More file actions
172 lines (131 loc) · 5.77 KB
/
add_2env.py
File metadata and controls
172 lines (131 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/python -tt
# Project: creds_in_env
# Filename: add_2env
# claudia
# PyCharm
from __future__ import absolute_import, division, print_function
__author__ = "Claudia de Luna (claudia@indigowire.net)"
__version__ = ": 1.0 $"
__date__ = "8/6/20"
__copyright__ = "Copyright (c) 2020 Claudia"
__license__ = "Python"
## All modules in this script are part of Python
import argparse
import os
import getpass
import pprint
import json
# Setting environment variables via the Linux CLI
# export NETUSER=cisco
# export NETPASS=cisco
# In Bash (OS-X or Linux), use unset to remove an environment variable
# unset NETUSER
# unset NETPASS
def all_env_vars(verbose=True):
"""
:param verbose: Optional parameter to enable (True) or disable (False) printed output to STDOUT
:return:
"""
env_vars = os.environ
if verbose:
if "USER" in env_vars.keys():
sys_user = os.environ['USER']
elif "USERNAME" in env_vars.keys():
sys_user = os.environ['USERNAME']
else:
sys_user = "System Username cannot be determined"
print(f"\n======== ENVIRONMENT VARIABLES for USER {sys_user} ======== ")
pprint.pprint(dict(env_vars), width = 4)
# Return all the environment variables as a dictionary
return dict(env_vars)
def check_env(env_var):
"""
:param env_var: Name of environment variable to check for existence and validity
Checks for both the existence of an environment variable as well as existence and empty
Returns three variables
:return:
var_exists: Variable is set in the environment
var_exists_empty: Variable exists in the environment and is empty
var_valid: Variable is valid (exists and is not empty)
"""
var_info = {}
var_info.update({'NAME': env_var})
var_info.update({'EXISTS': False})
var_info.update({'EMPTY': False})
var_info.update({'VALID': False})
var_info.update({'VALUE': os.getenv(env_var)})
if os.getenv(env_var) and os.environ[env_var] != '':
var_info.update({'EXISTS': True})
var_info.update({'VALID': True})
if env_var in os.environ.keys():
if os.environ[env_var] == '':
var_info.update({'EXISTS': True})
var_info.update({'EMPTY': True})
var_info.update({'VALID': False})
return var_info
def set_env(desc="Username", always_upper=True, sensitive=False, debug=True):
"""
Brief function to set environment variables (name/value) using the Python built in os module
The function has 4 optional parameters:
desc: Cosmetic message for the Input text to remind the user what key/value pair is being requested.
Default: "Username"
always_upper: Boolean used to convert the name to Uppercase to adhere to convention Default: True
sensitive: Boolean used to
Returns:
os_var_valid; the variable is set and valid
"""
print(f"\n======== Creating Environment Variable for {desc} ========")
if always_upper: print(f"**** Variable NAME will be set to all uppercase per convention...")
env_var_name = input(f"\nPlease enter {desc} environment variable name: ")
if sensitive:
env_var_value = getpass.getpass(f"Please enter {desc} sensitive environment variable value "
f"(will not echo to screen): ")
else:
env_var_value = input(f"Please enter {desc} environment variable value: ")
# If the always_upper option is True make the environment variable name all uppercase
if always_upper:
env_var_name = env_var_name.upper()
# Set the environment variable in the Operating System
os.environ[env_var_name] = env_var_value
os_var_info_dict = check_env(env_var_name)
if debug:
if sensitive:
if os_var_info_dict['VALID']:
print(f"\n======== ENV SET Environment Variable {env_var_name} set and valid ========\n")
else:
print(f"\n======== ERROR! Environment Variable {env_var_name} set but EMPTY! ========\n")
else:
if os_var_info_dict['VALID']:
print(f"\n======== ENV SET Environment Variable {env_var_name} set with valid value "
f"{os.environ[env_var_name]} ========\n")
else:
print(f"\n======== ERROR! Environment Variable {env_var_name} set but EMPTY! ========\n")
return os_var_info_dict['VALID'], os_var_info_dict
def unset_env(env_var=''):
# Unsetting environment variables depends on OS feature support and may not have expected outcome
if not env_var:
env_var = input(f"\nPlease enter environment variable name to UNSET: ")
os.putenv(env_var, '')
return os.unsetenv(env_var)
def main():
print(f"\nCurrent Environment Variables:")
env_vars_dict = all_env_vars()
# evars_json = json.dumps(evars, indent=4, sort_keys=True)
# print(evars_json)
# Call the set_env function, by default the description of the environment variable is "Username".
# There is nothing special about this description value.
# Its just a reminder of what key/value pair you are creating.
set_env()
# Call the set_env function with a description indicating we are setting a password and set the
# sensitive option to true so that the password can be typed in securely without echo to the screen
set_env(desc="Password", sensitive=True)
env_vars_dict = all_env_vars(verbose=False)
print(f"\nUPDATED Environment Variables:")
env_vars_json = json.dumps(env_vars_dict, indent=4, sort_keys=True)
print(env_vars_json)
# Standard call to the main() function.
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Script Description",
epilog="Usage: ' python add_2env.py' ")
arguments = parser.parse_args()
main()