Repository navigation
Expand file tree
/
Copy pathuls.py
More file actions
272 lines (230 loc) · 9.21 KB
/
Copy pathuls.py
File metadata and controls
272 lines (230 loc) · 9.21 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ----------------------
# About exit codes:
# 0: ULS exited normally
# 1001: "/usr/share/uls/device.json" not found.
# 1002: ULS script path is invalid.
# 1003: Internet is unavailable for updating.
# 1004: "uls --getinfo" must be run as ROOT.
# 1005: "uls --update" must be run as ROOT.
# Other: ULS will return what the converted shell script returns.
# ----------------------
import platform
import socket
import json
import uuid
import sys
import os
# Show help.
def ShowHelp():
print("Universal Linux Script v5 by CYRO4S")
print("Visit https://github.com/CYRO4S/Universal-Linux-Script for documents and more information.")
print("")
print("Usage:")
print("uls --help: Show this help information.")
print("uls --getinfo: Refresh system information.")
print("uls --update: Update ULS to latest version.")
print("uls [ULS Script Path]: Run ULS script.")
exit(0)
# Getting system information & save to file.
def GetInfo():
# Check for ROOT
# If "uls --getinfo" is not run as ROOT, notify user & exit.
print('Checking for ROOT...')
if os.geteuid() != 0:
print("ERR_1004: 'uls --getinfo' must be run as ROOT. Use 'sudo uls --getinfo' instead.")
exit(1004)
# Determine which Linux distro is running on the device.
print('Determining Linux distrubution...')
strDist = platform.linux_distribution()[0].lower()
pUpd, pIns, pUpg, pRem, strBase = '', '', '', '', ''
if strDist == 'debian' or strDist == 'ubuntu' or strDist == 'elementary' or strDist == 'kali' or strDist == 'raspbian':
pUpd = 'apt-get -y upgrade'
pIns = 'apt-get -y install'
pUpg = 'apt-get -y upgrade'
pRem = 'apt-get -y remove'
strBase = 'debian'
elif strDist == 'redhat' or strDist == 'centos' or strDist == 'fedora' or strDist == 'redhat linux' or strDist == 'centos linux' or strDist == 'fedora linux':
pUpd = 'yum -y upgrade'
pIns = 'yum -y install'
pUpg = 'yum -y upgrade'
pRem = 'yum -y remove'
strBase = 'redhat'
# Get virtualization technology
print('Determining virtualization technology...')
rVirt = os.popen('virt-what')
strVirt = rVirt.read().strip('\n')
rVirt.close()
# Get system information
print('Getting system version...')
strVersion = platform.linux_distribution()[1]
print('Getting device architecture...')
strArch = platform.uname().machine
print('Getting kernel version...')
strKernel = platform.uname().release
print('Getting hostname')
strHostname = platform.node()
print('Getting OS bit...')
strBit = platform.architecture()[0]
# Get hardware info
print('Getting CPU information...')
rCPU = os.popen("echo $( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )")
strCPU = rCPU.read().strip('\n')
rCPU.close()
rCores = os.popen("echo $( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )")
strCores = rCores.read().strip('\n')
rCores.close()
rFreq = os.popen("echo $( awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )")
strFreq = rFreq.read().strip('\n')
rFreq.close()
print('Getting RAM size...')
rRAM = os.popen("echo $( free -m | awk '/Mem/ {print $2}' )")
strRAM = rRAM.read().strip('\n')
rRAM.close()
print('Getting Swap size...')
rSwap = os.popen("echo $( free -m | awk '/Swap/ {print $2}' )")
strSwap = rSwap.read().strip('\n')
rSwap.close()
# Get network info
print('Getting your local IPv4 address...')
rLIP = os.popen("ifconfig -a | grep 'inet ' | cut -d ':' -f 2 |cut -d ' ' -f 1 | grep -v '^127'")
strLocalIP = rLIP.read().strip('\n')
rLIP.close()
print('Getting your MAC address...')
mac = uuid.UUID(int=uuid.getnode()).hex[-12:]
strMac = ":".join([mac[e:e+2] for e in range(0,11,2)])
print('Getting your public IPv4 address...')
rIP = os.popen('wget -qO- -t1 -T2 ipv4.icanhazip.com')
strIP = rIP.read().strip('\n')
rIP.close()
print('Getting your public IPv6 address...')
rIP6 = os.popen('wget -qO- -t1 -T2 ipv6.icanhazip.com')
strIP6 = rIP6.read().strip('\n')
rIP6.close()
# Finally write the JSON file.
print("Now generating 'device.json'...")
strJson = {
'pkg.update': pUpd,
'pkg.install': pIns,
'pkg.upgrade': pUpg,
'pkg.remove': pRem,
'sys.os': strDist,
'sys.osbase': strBase,
'sys.version': strVersion,
'sys.arch': strArch,
'sys.bit': strBit,
'sys.kernel': strKernel,
'sys.hostname': strHostname,
'dev.virt': strVirt,
'dev.cpu': strCPU,
'dev.cores': strCores,
'dev.freq': strFreq,
'dev.ram': strRAM,
'dev.swap': strSwap,
'net.ip': strIP,
'net.ipv6': strIP6,
'net.localip': strLocalIP,
'net.mac': strMac
}
with open('/usr/share/uls/device.json', 'w') as f:
json.dump(strJson, f, sort_keys=False, indent=4)
# Finalize
print('--------------')
print("All done. Now you can run ULS scripts by using 'uls [ULS Script Path]' command,")
print("or you can refresh system information by using 'uls --getinfo' command,")
print("or you can update ULS to the latest version by using 'uls --update command.'")
# Run the script.
def RunScript(strPath):
# Check for ROOT
strSudo = 'sudo '
strRoot = 'false'
if os.geteuid() == 0:
strSudo = ''
strRoot = 'true'
# Check if device.json exists
if not os.path.isfile('/usr/share/uls/device.json'):
print("ERR_1001: 'device.json' not found. Run 'uls --getinfo' to generate.")
exit(1001)
# Prepare device.json
j = json.loads(open('/usr/share/uls/device.json', 'r').read())
# Check if ULS script exists
if not os.path.isfile(strPath):
print("ERR_1002: ULS script path invalid. Ensure you entered the correct path.")
exit(1002)
# Read ULS script file
lines = open(strPath, 'r').readlines()
f = open('/tmp/script.sh', 'w')
# Start to replace
for s in lines:
f.write(
# Replace PKG.*
s.replace('pkg.update', strSudo + j.get('pkg.update')) \
.replace('pkg.install', strSudo + j.get('pkg.install')) \
.replace('pkg.upgrade', strSudo + j.get('pkg.upgrade')) \
.replace('pkg.remove', strSudo + j.get('pkg.remove')) \
# Replace SYS.*
.replace('sys.os', '\"' + j.get('sys.os') + '\"') \
.replace('sys.osbase', '\"' + j.get('sys.osbase') + '\"') \
.replace('sys.version', '\"' + j.get('sys.version') + '\"') \
.replace('sys.arch', '\"' + j.get('sys.arch') + '\"') \
.replace('sys.bit', '\"' + j.get('sys.bit') + '\"') \
.replace('sys.kernel', '\"' + j.get('sys.kernel') + '\"') \
.replace('sys.hostname', '\"' + j.get('sys.hostname') + '\"') \
.replace('sys.root', '\"' + strRoot + '\"') \
# Replace DEV.*
.replace('dev.virt', '\"' + j.get('dev.virt') + '\"') \
.replace('dev.cpu', '\"' + j.get('dev.cpu') + '\"') \
.replace('dev.cores', '\"' + j.get('dev.cores') + '\"') \
.replace('dev.freq', '\"' + j.get('dev.freq') + '\"') \
.replace('dev.ram', '\"' + j.get('dev.ram') + '\"') \
.replace('dev.swap', '\"' + j.get('dev.swap') + '\"') \
# Replace NET.*
.replace('net.ip', '\"' + j.get('net.ip') + '\"') \
.replace('net.ipv6', '\"' + j.get('net.ipv6') + '\"') \
.replace('net.localip', '\"' + j.get('net.localip') + '\"') \
.replace('net.mac', '\"' + j.get('net.mac') + '\"')
)
# Save script.sh
f.close()
# Then execute it
strReturn = ''
strReturn = os.system('bash /tmp/script.sh')
# Then remove 'script.sh'
os.remove('/tmp/script.sh')
# Finally, exit.
exit(strReturn)
# Update ULS.
def Update():
# Check for ROOT
# If "uls --update" is not run as ROOT, notify user & exit.
print('Checking for ROOT...')
if os.geteuid() != 0:
print("ERR_1005: 'uls --update' must be run as ROOT. Use 'sudo uls --update' instead.")
exit(1005)
# Check for Internet connection before update
print('Checking for Internet connection...')
rPing = os.popen("ping -c 3 raw.githubusercontent.com | grep '0 received' | wc -l")
strPing = rPing.read().strip('\n')
rPing.close()
# If Internet is unavailable, exit.
if strPing == '1':
print('ERR_1003: Internet is unavailable. Check your connection before running update.')
exit(1003)
# Now, do the update
os.system("wget -O /usr/share/uls/uls_update.sh https://raw.githubusercontent.com/CYRO4S/Universal-Linux-Script/master/uls_update.sh && bash /usr/share/uls/uls_update.sh")
exit(0)
# Main
if __name__ == '__main__':
if len(sys.argv) == 1:
ShowHelp()
else:
if sys.argv[1] == "--getinfo":
GetInfo()
elif sys.argv[1] == "--help":
ShowHelp()
elif sys.argv[1] == "--update":
Update()
else:
RunScript(sys.argv[1])
exit(0)