109 lines
3.8 KiB
Python
109 lines
3.8 KiB
Python
import irc.bot, subprocess
|
|
import irc.strings
|
|
import psutil, platform
|
|
from sys import argv
|
|
|
|
channel = argv[1]
|
|
nickname = argv[2]
|
|
server = argv[3]
|
|
if len(argv) !=4:
|
|
print('Usage: {} channel nickname server'.format(argv[0]))
|
|
|
|
def get_size(bytes, suffix="B"):
|
|
factor = 1024
|
|
for unit in ["", "K", "M", "G", "T", "P"]:
|
|
if bytes < factor:
|
|
return f"{bytes:.2f}{unit}{suffix}"
|
|
bytes /= factor
|
|
|
|
|
|
class TestBot(irc.bot.SingleServerIRCBot):
|
|
def __init__(self, channel, nickname, server, port=6667):
|
|
irc.bot.SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
|
|
self.channel = channel
|
|
self.connection.add_global_handler('disconnect', self.on_disconnect)
|
|
|
|
def on_disconnect(self, connection, event):
|
|
exit(1)
|
|
|
|
def on_welcome(self, c, e):
|
|
c.mode(self._nickname, '+B')
|
|
c.join(self.channel)
|
|
|
|
def on_privmsg(self, c, e):
|
|
self.do_command(e, e.arguments[0])
|
|
|
|
def on_pubmsg(self, c, e):
|
|
a = e.arguments[0].split(":", 1)
|
|
if len(a) > 1 and irc.strings.lower(a[0]) == irc.strings.lower(
|
|
self.connection.get_nickname()
|
|
):
|
|
self.do_command(e, a[1].strip())
|
|
return
|
|
|
|
def do_command(self, e, cmd):
|
|
#nick = e.source.nick ## Uncomment when need to send to user not channel
|
|
c = self.connection
|
|
if cmd.lower() == "uptime":
|
|
result = subprocess.run(["uptime"], stderr=subprocess.PIPE, stdout=subprocess.PIPE)
|
|
output = result.stdout.decode().strip()
|
|
c.privmsg(channel, output)
|
|
|
|
elif cmd.lower() == "uname":
|
|
uname = platform.uname()
|
|
end = 'System name:' + uname.version
|
|
c.privmsg(channel, end)
|
|
|
|
elif cmd.lower() == "ram":
|
|
svmem = psutil.virtual_memory()
|
|
total = 'Total RAM:' + get_size(svmem.total)
|
|
used = 'Used Ram: ' + get_size(svmem.used)
|
|
percentage = 'Percent:' + str(svmem.percent) + '%'
|
|
avail = 'Available:' + get_size(svmem.available)
|
|
c.privmsg(channel, total)
|
|
c.privmsg(channel, avail)
|
|
c.privmsg(channel, used)
|
|
c.privmsg(channel, percentage)
|
|
|
|
elif cmd.lower() == "cpu":
|
|
percent = psutil.cpu_percent()
|
|
end = 'CPU load: {}%'.format(percent)
|
|
c.privmsg(channel, end)
|
|
|
|
elif cmd.lower() == "network":
|
|
if_addrs = psutil.net_if_addrs()
|
|
for interface_name, interface_addresses in if_addrs.items():
|
|
header = 'Interface: ' + interface_name
|
|
c.privmsg(channel, header)
|
|
for address in interface_addresses:
|
|
if str(address.family) == 'AddressFamily.AF_INET':
|
|
ip = 'IP Address: ' + address.address
|
|
mask = 'Netmask: ' + address.netmask
|
|
c.privmsg(channel, ' {} / {}'.format(ip, mask))
|
|
|
|
elif cmd.lower() == 'temp':
|
|
temperatures = psutil.sensors_temperatures()
|
|
for name, device_temp in temperatures.items():
|
|
header = 'Device: ' + name
|
|
c.privmsg(channel, header)
|
|
for sensor in device_temp:
|
|
sensor_name = sensor.label
|
|
if not sensor_name: sensor_name = '<no name>'
|
|
c.privmsg(channel, ' Sensor name: {}, current: {}'.format(sensor_name, sensor.current))
|
|
|
|
elif cmd.lower() == "help":
|
|
end = 'Available commands are: help, uptime, ram, cpu, uname, network, temp'
|
|
c.privmsg(channel, end)
|
|
|
|
elif cmd.lower() == "stop":
|
|
exit(1)
|
|
else:
|
|
end = 'Unknow command. Sorry'
|
|
c.privmsg(channel, end)
|
|
def main():
|
|
bot = TestBot(channel, nickname, server)
|
|
bot.start()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|