Code:
#!/usr/bin/python3
"""
NB-IoT Test Code with ArduinoBlue
Listens on a port for UDP, simple authentication, echo back with unix time
Version 0.15 29 January 2020
Neo wwww.unix.com
Use as you wish. If you improve it, please post back.
"""
import socket
import sys
import time
import subprocess
# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# sock.setblocking(False)
port = 1234 # your port number here
append = str(time.time())
file = "/root/debug/arduino/nbiot"+append+".log"
f = open(file, "w")
# Bind the socket to the port
server_address = ('your.server.com', port)
print >>sys.stderr, 'starting up on %s port %s' % server_address
f.write('starting up on %s port %s' % server_address)
sock.bind(server_address)
password = "my_simple_password\n"
authenticated = True
receivepw = True
lastsent = 0
while True:
print >>sys.stderr, '\nwaiting to receive message'
f.write('\nwaiting to receive message')
now = time.time()
data, address = sock.recvfrom(4096)
print >>sys.stderr, str(time.time())+': received %s bytes from %s' % (len(data), address)
f.write(str(time.time())+': received %s bytes from %s' % (len(data), address))
stuff = str(time.time())+ ": "+ data
print >>sys.stderr, stuff
f.write(stuff)
# override simple authentication for now
if data:
lastsent = time.time()
if authenticated == False and receivepw == False:
sent = sock.sendto("Password: ", address)
receivepw = True
elif receivepw == True and authenticated == False:
if data == password:
sent = sock.sendto("Authenticated\n", address)
authenticated = True
else:
sent = sock.sendto("Password:\n", address)
print >>sys.stderr, 'a: sent %s bytes back to %s' % (sent, address)
f.write('a: sent %s bytes back to %s' % (sent, address))
elif authenticated == True: #set to True all the time passing the auth code
if '86940503122XXXXXXX' not in data:
print >>sys.stderr, str(time.time())+': received %s' % (data)
f.write(str(time.time())+':received %s' % (data))
message = data
# received = "You sent: " + data
# sent = sock.sendto(received, address)
# data = time.time()
# data = "Unix Time: " + str(data)+"\n"
# sent = sock.sendto(data, address)
# sensor = sock.sendto("Sensor Data", address)
# print >>sys.stderr, str(time.time())+': sent %s bytes back to %s' % (sent, address)
# f.write(str(time.time())+': sent %s bytes back to %s' % (sent, address))
doit = False
if 'GetLoadAvgs' in message:
replydata = "Server Load Averages:"+subprocess.check_output(['cat', '/proc/loadavg'])
doit = True
elif 'GetDiskSpace' in message:
tmp = subprocess.check_output("df | grep md2", shell=True)
tmp = tmp.split()
replydata ="MD2 Disk Space:"+tmp[4]
doit = True
elif 'GetApache2Procs' in message:
tmp = subprocess.check_output("ps aux | grep apache2 | grep -v grep| wc -l", shell=True)
replydata ="Apache2 Processes:"+tmp
doit = True
elif 'GetLastBackup' in message:
tmp = subprocess.check_output("ls -l /data/dumps | grep main | tail -1", shell=True)
tmp = tmp.split(".")
tmp = tmp[0].split("_")
replydata ="Last Backup:"+tmp[2]
doit = True
elif 'GetSSHDStatus' in message:
tmp = subprocess.check_output("ps aux | grep sshd | grep sbin", shell=True)
if tmp > 0:
sshd = "UP"
else:
sshd = "DOWN"
replydata ="SSHD Status:"+sshd
doit = True
if doit == True:
load = sock.sendto(replydata, address)
print >>sys.stderr, str(time.time())+': sent %s bytes back to %s message %s' % (load,address, replydata)
f.write(str(time.time())+': sent %s bytes back to %s message %s' % (load, address,replydata))
else:
received = "Rejected: Unauthorized EMEI"
sent = sock.sendto(received, address)
else:
sent = sock.sendto("Error\n", address)
print >>sys.stderr, str(time.time())+' : sent %s bytes back to %s' % (sent, address)
f.write(str(time.time())+' : sent %s bytes back to %s' % (sent, address))