2019-10-07 19:42:29 +02:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import time
|
2019-10-24 18:12:10 +02:00
|
|
|
import re
|
2019-12-09 15:36:26 -08:00
|
|
|
|
2019-09-19 15:15:46 +02:00
|
|
|
|
2020-04-03 19:01:40 +02:00
|
|
|
|
2020-01-17 18:10:33 +01:00
|
|
|
from pwnagotchi._version import __version__
|
2019-10-04 00:33:06 +02:00
|
|
|
|
2019-09-19 15:15:46 +02:00
|
|
|
_name = None
|
2020-04-01 08:17:22 +02:00
|
|
|
config = None
|
2023-05-19 10:58:20 -07:00
|
|
|
_cpu_stats = {}
|
2019-09-19 15:15:46 +02:00
|
|
|
|
2019-10-24 17:48:37 +02:00
|
|
|
def set_name(new_name):
|
|
|
|
if new_name is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
new_name = new_name.strip()
|
|
|
|
if new_name == '':
|
|
|
|
return
|
|
|
|
|
2019-10-24 18:12:10 +02:00
|
|
|
if not re.match(r'^[a-zA-Z0-9\-]{2,25}$', new_name):
|
|
|
|
logging.warning("name '%s' is invalid: min length is 2, max length 25, only a-zA-Z0-9- allowed", new_name)
|
|
|
|
return
|
|
|
|
|
2019-10-24 17:48:37 +02:00
|
|
|
current = name()
|
|
|
|
if new_name != current:
|
|
|
|
global _name
|
|
|
|
|
2019-12-09 15:36:26 -08:00
|
|
|
logging.info("setting unit hostname '%s' -> '%s'", current, new_name)
|
2019-10-24 17:48:37 +02:00
|
|
|
with open('/etc/hostname', 'wt') as fp:
|
|
|
|
fp.write(new_name)
|
|
|
|
|
|
|
|
with open('/etc/hosts', 'rt') as fp:
|
|
|
|
prev = fp.read()
|
2019-12-09 15:36:26 -08:00
|
|
|
logging.debug("old hosts:\n%s\n", prev)
|
2019-10-24 17:48:37 +02:00
|
|
|
|
|
|
|
with open('/etc/hosts', 'wt') as fp:
|
|
|
|
patched = prev.replace(current, new_name, -1)
|
2019-12-09 15:36:26 -08:00
|
|
|
logging.debug("new hosts:\n%s\n", patched)
|
2019-10-24 17:48:37 +02:00
|
|
|
fp.write(patched)
|
|
|
|
|
2019-10-24 17:52:43 +02:00
|
|
|
os.system("hostname '%s'" % new_name)
|
2019-10-25 16:37:45 +02:00
|
|
|
pwnagotchi.reboot()
|
2019-10-24 17:48:37 +02:00
|
|
|
|
|
|
|
|
2019-09-19 15:15:46 +02:00
|
|
|
def name():
|
|
|
|
global _name
|
|
|
|
if _name is None:
|
|
|
|
with open('/etc/hostname', 'rt') as fp:
|
|
|
|
_name = fp.read().strip()
|
|
|
|
return _name
|
|
|
|
|
|
|
|
|
2019-10-13 12:18:35 +02:00
|
|
|
def uptime():
|
|
|
|
with open('/proc/uptime') as fp:
|
|
|
|
return int(fp.read().split('.')[0])
|
|
|
|
|
2019-10-25 16:37:45 +02:00
|
|
|
|
2019-09-19 15:15:46 +02:00
|
|
|
def mem_usage():
|
2019-10-24 20:29:09 +01:00
|
|
|
with open('/proc/meminfo') as fp:
|
|
|
|
for line in fp:
|
|
|
|
line = line.strip()
|
|
|
|
if line.startswith("MemTotal:"):
|
|
|
|
kb_mem_total = int(line.split()[1])
|
|
|
|
if line.startswith("MemFree:"):
|
|
|
|
kb_mem_free = int(line.split()[1])
|
|
|
|
if line.startswith("Buffers:"):
|
|
|
|
kb_main_buffers = int(line.split()[1])
|
|
|
|
if line.startswith("Cached:"):
|
|
|
|
kb_main_cached = int(line.split()[1])
|
|
|
|
kb_mem_used = kb_mem_total - kb_mem_free - kb_main_cached - kb_main_buffers
|
2019-10-25 16:37:45 +02:00
|
|
|
return round(kb_mem_used / kb_mem_total, 1)
|
2019-09-19 15:15:46 +02:00
|
|
|
|
|
|
|
return 0
|
|
|
|
|
2019-10-25 16:37:45 +02:00
|
|
|
|
2020-03-30 18:14:04 +02:00
|
|
|
def _cpu_stat():
|
|
|
|
"""
|
|
|
|
Returns the splitted first line of the /proc/stat file
|
|
|
|
"""
|
2019-09-19 15:15:46 +02:00
|
|
|
with open('/proc/stat', 'rt') as fp:
|
2020-03-30 18:14:04 +02:00
|
|
|
return list(map(int,fp.readline().split()[1:]))
|
|
|
|
|
|
|
|
|
2023-05-19 10:58:20 -07:00
|
|
|
def cpu_load(tag=None):
|
2020-03-30 18:14:04 +02:00
|
|
|
"""
|
|
|
|
Returns the current cpuload
|
|
|
|
"""
|
2023-05-19 10:58:20 -07:00
|
|
|
if tag and tag in _cpu_stats.keys():
|
|
|
|
parts0 = _cpu_stats[tag]
|
|
|
|
else:
|
|
|
|
parts0 = _cpu_stat()
|
2023-06-18 20:06:00 -07:00
|
|
|
time.sleep(0.1)
|
2020-03-30 18:14:04 +02:00
|
|
|
parts1 = _cpu_stat()
|
2023-05-19 10:58:20 -07:00
|
|
|
if tag: _cpu_stats[tag] = parts1
|
|
|
|
|
2020-03-30 18:14:04 +02:00
|
|
|
parts_diff = [p1 - p0 for (p0, p1) in zip(parts0, parts1)]
|
|
|
|
user, nice, sys, idle, iowait, irq, softirq, steal, _guest, _guest_nice = parts_diff
|
|
|
|
idle_sum = idle + iowait
|
|
|
|
non_idle_sum = user + nice + sys + irq + softirq + steal
|
|
|
|
total = idle_sum + non_idle_sum
|
|
|
|
return non_idle_sum / total
|
2019-09-19 15:15:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
def temperature(celsius=True):
|
|
|
|
with open('/sys/class/thermal/thermal_zone0/temp', 'rt') as fp:
|
|
|
|
temp = int(fp.read().strip())
|
|
|
|
c = int(temp / 1000)
|
|
|
|
return c if celsius else ((c * (9 / 5)) + 32)
|
2019-10-07 19:42:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def shutdown():
|
|
|
|
logging.warning("shutting down ...")
|
2020-04-03 19:01:40 +02:00
|
|
|
|
|
|
|
from pwnagotchi.ui import view
|
2019-10-07 19:42:29 +02:00
|
|
|
if view.ROOT:
|
|
|
|
view.ROOT.on_shutdown()
|
|
|
|
# give it some time to refresh the ui
|
2019-10-31 15:39:59 +01:00
|
|
|
time.sleep(10)
|
2020-05-08 21:56:08 +12:00
|
|
|
|
|
|
|
logging.warning("syncing...")
|
|
|
|
|
|
|
|
from pwnagotchi import fs
|
|
|
|
for m in fs.mounts:
|
|
|
|
m.sync()
|
|
|
|
|
2019-10-07 19:42:29 +02:00
|
|
|
os.system("sync")
|
|
|
|
os.system("halt")
|
2019-10-17 13:42:05 +02:00
|
|
|
|
|
|
|
|
2019-11-04 17:35:06 +01:00
|
|
|
def restart(mode):
|
2019-12-09 15:36:26 -08:00
|
|
|
logging.warning("restarting in %s mode ...", mode)
|
2019-11-04 17:35:06 +01:00
|
|
|
|
|
|
|
if mode == 'AUTO':
|
|
|
|
os.system("touch /root/.pwnagotchi-auto")
|
|
|
|
else:
|
|
|
|
os.system("touch /root/.pwnagotchi-manual")
|
|
|
|
|
|
|
|
os.system("service bettercap restart")
|
|
|
|
os.system("service pwnagotchi restart")
|
|
|
|
|
|
|
|
|
2019-11-04 11:13:51 +01:00
|
|
|
def reboot(mode=None):
|
|
|
|
if mode is not None:
|
|
|
|
mode = mode.upper()
|
2019-12-09 15:36:26 -08:00
|
|
|
logging.warning("rebooting in %s mode ...", mode)
|
2019-11-04 11:13:51 +01:00
|
|
|
else:
|
|
|
|
logging.warning("rebooting ...")
|
2019-11-03 22:57:32 +01:00
|
|
|
|
2020-04-03 19:01:40 +02:00
|
|
|
from pwnagotchi.ui import view
|
2019-11-03 22:57:32 +01:00
|
|
|
if view.ROOT:
|
2019-11-05 19:07:16 +01:00
|
|
|
view.ROOT.on_rebooting()
|
2019-11-03 22:57:32 +01:00
|
|
|
# give it some time to refresh the ui
|
|
|
|
time.sleep(10)
|
2019-11-04 11:13:51 +01:00
|
|
|
|
|
|
|
if mode == 'AUTO':
|
|
|
|
os.system("touch /root/.pwnagotchi-auto")
|
2019-11-04 17:35:06 +01:00
|
|
|
elif mode == 'MANU':
|
|
|
|
os.system("touch /root/.pwnagotchi-manual")
|
2019-11-04 11:13:51 +01:00
|
|
|
|
2020-05-08 21:57:08 +12:00
|
|
|
logging.warning("syncing...")
|
|
|
|
|
|
|
|
from pwnagotchi import fs
|
|
|
|
for m in fs.mounts:
|
|
|
|
m.sync()
|
|
|
|
|
2019-11-03 22:57:32 +01:00
|
|
|
os.system("sync")
|
|
|
|
os.system("shutdown -r now")
|