enable ssl

This commit is contained in:
deflax 2024-02-07 15:38:49 +02:00 committed by GitHub
parent 709326ff21
commit 2de33ceeb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,6 +12,7 @@ z_user = 'USER'
z_pass = 'PASS' z_pass = 'PASS'
server = 'irc.example.com' server = 'irc.example.com'
port = 9020
channel = '#example' channel = '#example'
nick = 'steinvord' nick = 'steinvord'
botnick = 'Steinvord' botnick = 'Steinvord'
@ -20,9 +21,9 @@ botuser = 'steinvord'
encoding = 'utf-8' encoding = 'utf-8'
calibrate = 0 calibrate = 0
### begin ### begin
import sys, socket, time, re, datetime import sys, time, re, datetime
import socket, ssl
from pyzabbix import ZabbixAPI from pyzabbix import ZabbixAPI
from difflib import Differ from difflib import Differ
@ -37,29 +38,35 @@ try:
except Exception as e: except Exception as e:
print(e) print(e)
exit(3) exit(3)
print ("Connected to Zabbix API Version %s" % zapi.api_version()) print("Connected to Zabbix API Version %s" % zapi.api_version())
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
irc_C = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
irc = ssl.wrap_socket(irc_C)
print("Establishing connection to [%s]" % (server)) print("Establishing connection to [%s]" % (server))
irc.connect((server, 6667)) #connects to the server irc.connect((server, port)) #connects to the server
irc.setblocking(False) irc.setblocking(False)
sock_auth = "USER "+ botuser +" "+ botuser +" "+ botuser +" :This is the Steinvord bot!\n" sock_auth = "USER "+ botuser +" "+ botuser +" "+ botuser +" :This is the Steinvord bot!\n"
sock_nick = "NICK "+ botnick +"\n" sock_nick = "NICK "+ botnick +"\n"
sock_nicksrv = "PRIVMSG nickserv :iNOOPE\r\n" sock_nicksrv = "PRIVMSG nickserv :iNOOPE\r\n"
sock_join = "JOIN "+ channel +"\n" sock_join = "JOIN "+ channel +"\n"
irc.send(sock_auth.encode(encoding)) irc.send(sock_auth.encode(encoding))
irc.send(sock_nick.encode(encoding)) irc.send(sock_nick.encode(encoding))
irc.send(sock_nicksrv.encode(encoding)) irc.send(sock_nicksrv.encode(encoding))
time.sleep(10)
irc.send(sock_join.encode(encoding)) irc.send(sock_join.encode(encoding))
def environment(): def environment():
currtemp = float(zapi.item.get(filter={'hostid': '10125', 'itemid': '26379'})[0]['lastvalue']) currtemp = float(zapi.item.get(filter={'hostid': '10137', 'itemid': '28145'})[0]['lastvalue'])
currhumid = calibrate + float(zapi.item.get(filter={'hostid': '10125', 'itemid': '26618'})[0]['lastvalue']) currhumid = calibrate + float(zapi.item.get(filter={'hostid': '10137', 'itemid': '28144'})[0]['lastvalue'])
t_result = 't=' + str(currtemp) + '°C' t_result = 't=' + str(currtemp) + '°C'
if currtemp > 24: if currtemp > 27:
t_result += ' !! WARNING !!' t_result += ' !! WARNING !!'
elif currtemp > 32:
t_result += ' !! CRITICAL !!'
t_result += ' h=' + str(currhumid) + '%' t_result += ' h=' + str(currhumid) + '%'
t_sock_result = 'PRIVMSG ' + channel + ' :' + t_result + '\n' t_sock_result = 'PRIVMSG ' + channel + ' :' + t_result + '\n'
irc.send(t_sock_result.encode(encoding)) irc.send(t_sock_result.encode(encoding))
@ -72,8 +79,8 @@ while True:
if counter == 60: if counter == 60:
counter = 0 counter = 0
ctime = datetime.datetime.now() ctime = datetime.datetime.now()
if int(ctime.minute) == 20: #if int(ctime.minute) == 20:
environment() # print env data every 20 minutes # environment() # print env data every 20 minutes
triggers = zapi.trigger.get(only_true=1, triggers = zapi.trigger.get(only_true=1,
skipDependent=1, skipDependent=1,
@ -129,8 +136,8 @@ while True:
sock_result = 'PRIVMSG ' + channel + " :" + result + '\n' sock_result = 'PRIVMSG ' + channel + " :" + result + '\n'
irc.send(sock_result.encode(encoding)) irc.send(sock_result.encode(encoding))
time.sleep(1) time.sleep(1)
if decoded_text.find('!env') != -1: #if decoded_text.find('!env') != -1:
environment() # environment()
# Prevent Timeout # Prevent Timeout
if decoded_text.find('PING') != -1: if decoded_text.find('PING') != -1:
@ -140,4 +147,3 @@ while True:
continue continue
exit(0) exit(0)