Added logging
This commit is contained in:
parent
b5298d7ada
commit
5f085c5eb1
1 changed files with 153 additions and 89 deletions
98
checkmyip.py
98
checkmyip.py
|
@ -1,14 +1,64 @@
|
|||
import os
|
||||
import sys
|
||||
import time
|
||||
import socket
|
||||
import jinja2
|
||||
import paramiko
|
||||
import threading
|
||||
|
||||
j2log = "Connection from: {{ ip }} ({{ port }})"
|
||||
j2send = "\n\n\nYour IP Address is {{ ip }} ({{ port }})\n\n\n\n"
|
||||
j2log = "Connection from: {{ ip }} ({{ port }}) ({{ proto }})"
|
||||
#j2send = "\n\n\nYour IP Address is {{ ip }} ({{ port }})\n\n\n\n"
|
||||
|
||||
|
||||
paramiko.util.log_to_file('demo_server.log')
|
||||
j2send = """{
|
||||
|
||||
|
||||
"comment": "## Your IP Address is {{ ip }} ({{ port }}) ##",
|
||||
|
||||
|
||||
"family": "{{ family }}",
|
||||
"ip": "{{ ip }}",
|
||||
"port": "{{ port }}"
|
||||
}"""
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
class log_management:
|
||||
def __init__(self):
|
||||
self.logpath = "/etc/checkmyip/"
|
||||
self.logfile = "/etc/checkmyip/checkmyip.log"
|
||||
self._publish_methods()
|
||||
self.can_log = True
|
||||
paramiko.util.log_to_file('/etc/checkmyip/ssh.log')
|
||||
def _logger(self, data):
|
||||
logdata = time.strftime("%Y-%m-%d %H:%M:%S") + ": " + data + "\n"
|
||||
if self.can_log:
|
||||
try:
|
||||
f = open(self.logfile, 'a')
|
||||
f.write(logdata)
|
||||
f.close()
|
||||
except IOError:
|
||||
self._console("Unable to log to logfile %s. Creating log directory" % self.logfile)
|
||||
self.can_log = False
|
||||
self._create_log_dir()
|
||||
self._console(logdata)
|
||||
def _console(self, data, timestamp=False):
|
||||
if timestamp:
|
||||
logdata = time.strftime("%Y-%m-%d %H:%M:%S") + ": " + data + "\n"
|
||||
else:
|
||||
logdata = data
|
||||
print(logdata)
|
||||
def _publish_methods(self):
|
||||
global log
|
||||
global console
|
||||
log = self._logger
|
||||
console = self._console
|
||||
def _create_log_dir(self):
|
||||
os.system('mkdir -p ' + self.logpath)
|
||||
self._console("Logpath (%s) created" % self.logpath)
|
||||
self.can_log = True
|
||||
|
||||
|
||||
class rsa_key:
|
||||
|
@ -62,11 +112,15 @@ def j2format(j2tmp, valdict):
|
|||
return template.render(valdict)
|
||||
|
||||
|
||||
def cleanip(ip):
|
||||
def cleanip(addr):
|
||||
ip = addr[0]
|
||||
port = addr[1]
|
||||
family = "ipv6"
|
||||
if len(ip) > 6: # If this IP is not a super short v6 address
|
||||
if ip[:7] == "::ffff:": # If this is a prefixed IPv4 address
|
||||
return ip.replace("::ffff:", "") # Return the cleaned IP
|
||||
return ip # Return the uncleaned IP if not matched
|
||||
ip = ip.replace("::ffff:", "") # Return the cleaned IP
|
||||
family = "ipv4"
|
||||
return (ip, port, family) # Return the uncleaned IP if not matched
|
||||
|
||||
|
||||
def listener(port, talker):
|
||||
|
@ -79,18 +133,22 @@ def listener(port, talker):
|
|||
sock.bind((listen_ip, listen_port))
|
||||
sock.listen(buffer_size)
|
||||
client, addr = sock.accept()
|
||||
thread = threading.Thread(target=talker, args=(client, addr))
|
||||
ip, port, family = cleanip(addr)
|
||||
valdict = {"ip": ip, "port": port, "family": family}
|
||||
thread = threading.Thread(target=talker, args=(client, valdict))
|
||||
thread.start()
|
||||
|
||||
|
||||
def telnet_talker(client, addr):
|
||||
valdict = {"ip": cleanip(addr[0]), "port": addr[1]}
|
||||
print(j2format(j2log, valdict))
|
||||
def telnet_talker(client, valdict):
|
||||
valdict.update({"proto": "telnet"})
|
||||
log(j2format(j2log, valdict))
|
||||
client.send(j2format(j2send, valdict)) # echo
|
||||
client.close()
|
||||
#quit()
|
||||
|
||||
|
||||
def ssh_talker(client, addr):
|
||||
def ssh_talker(client, valdict):
|
||||
valdict.update({"proto": "ssh"})
|
||||
t = paramiko.Transport(client, gss_kex=True)
|
||||
t.set_gss_host(socket.getfqdn(""))
|
||||
t.load_server_moduli()
|
||||
|
@ -98,23 +156,29 @@ def ssh_talker(client, addr):
|
|||
server = ssh_server()
|
||||
t.start_server(server=server)
|
||||
chan = t.accept(20)
|
||||
if chan:
|
||||
server.event.wait(10)
|
||||
valdict = {"ip": cleanip(addr[0]), "port": addr[1]}
|
||||
#chan.send(j2format(j2log, valdict))
|
||||
log(j2format(j2log, valdict))
|
||||
chan.send('%s' % j2format(j2send, valdict))
|
||||
chan.makefile('rU').readline().strip('\r\n')
|
||||
chan.close()
|
||||
#quit()
|
||||
|
||||
|
||||
def start():
|
||||
talkers = {2200: ssh_talker, 23: telnet_talker}
|
||||
talkers = {22: ssh_talker, 23: telnet_talker}
|
||||
for talker in talkers:
|
||||
thread = threading.Thread(target=listener, args=(talker, talkers[talker]))
|
||||
thread.daemon = False
|
||||
thread.daemon = True
|
||||
thread.start()
|
||||
while True:
|
||||
try:
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
quit()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
logging = log_management()
|
||||
start()
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue