From 132ce81d053293f052e1acccbc5d75adae1b8413 Mon Sep 17 00:00:00 2001 From: Johannes Findeisen Date: Wed, 5 Jun 2013 04:20:26 +0200 Subject: [PATCH] added some code to the service classes; no working code; mostly boilerplate... --- lib/service/htmlcontent.py | 43 +++++++++++++++++++++++++++++++++- lib/service/service.py | 4 +++- lib/service/shell.py | 3 +-- lib/service/ssh.py | 42 +++++++++++++++++++++++++++++++-- lib/service/tcpconnect.py | 48 ++++++++++++++++++++------------------ 5 files changed, 111 insertions(+), 29 deletions(-) diff --git a/lib/service/htmlcontent.py b/lib/service/htmlcontent.py index 6d9580c..37a7554 100644 --- a/lib/service/htmlcontent.py +++ b/lib/service/htmlcontent.py @@ -1,5 +1,8 @@ """ The htmlcontent service in pure Python. STUPID NAME!!! + +This should just return 0 on success and NOT 0 on error. Just to make internals generic to just report this code and +not use a parser. """ # http://pycurl.sourceforge.net/ --- seems old... @@ -7,4 +10,42 @@ The htmlcontent service in pure Python. STUPID NAME!!! # # maybe better: http://docs.python.org/2/library/urllib.html # -# or look at tcpconnect.py! could be useful to. \ No newline at end of file +# or look at tcpconnect.py! could be useful to. + +import socket +import sys +from service import Service + + +class HtmlcontentService(Service): + def __init__(self, parser, log, **kwargs): + super(Service, self).__init__(parser) + + if "string" in kwargs: + self.string = kwargs["string"] + else: + log.w("There is no string set to match") + + def execute(self): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + except socket.error, msg: + sys.stderr.write("[ERROR] %s\n" % msg[1]) + sys.exit(1) + + try: + sock.connect((self.host, self.port)) + except socket.error, msg: + sys.stderr.write("[ERROR] %s\n" % msg[1]) + sys.exit(2) + + sock.send("GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (self.path, self.host)) + + data = sock.recv(1024) + string = "" + while len(data): + string = string + data + data = sock.recv(1024) + sock.close() + #print string + # make substring in string compare here. regex or so... diff --git a/lib/service/service.py b/lib/service/service.py index 1664152..0ec3ea8 100644 --- a/lib/service/service.py +++ b/lib/service/service.py @@ -1,6 +1,8 @@ class Service: - def __init__(self, parser): + def __init__(self, host, parser): + self.host = host self.parser = parser + self.errorcode = 0 def _execute(self): self.pre_execute() diff --git a/lib/service/shell.py b/lib/service/shell.py index 7b7e470..097ac35 100644 --- a/lib/service/shell.py +++ b/lib/service/shell.py @@ -1,6 +1,5 @@ """ -The shell service. This is for executing services as shell commands -and don't use a builtin function. This is useful to be free to do what you want. +The shell service. This is for executing local shell commands and retrieve the output. """ from service import Service diff --git a/lib/service/ssh.py b/lib/service/ssh.py index 123c460..7b94c02 100644 --- a/lib/service/ssh.py +++ b/lib/service/ssh.py @@ -1,5 +1,43 @@ """ -The ssh service in pure Python. +The ssh service This is for executing remote shell commands and retrieve the output. + +This service is using paramiko (http://www.lag.net/paramiko/). """ -# http://www.lag.net/paramiko/ \ No newline at end of file +import paramiko +import pprint +import os +from service import Service + + +class SshService(Service): + def __init__(self, parser, log, **kwargs): + super(Service, self).__init__(parser) + if "command" in kwargs: + self.command = kwargs["command"] + else: + log.w("There is no command") + + def execute(self): + path = os.path.join(os.environ['HOME'], '.ssh', 'id_rsa') + key = paramiko.RSAKey.from_private_key_file(path) + + client = paramiko.SSHClient() + client.get_host_keys().add('hanez.org', 'ssh-rsa', key) + pprint.pprint(client._host_keys) + + client.connect('hanez.org', username='hanez') + + #self.command.call() ist dann das: + stdin, stdout, stderr = client.exec_command('ls') + for line in stdout: + print '... ' + line.strip('\n') + client.close() + + +# def main(): +# # service = SshService(parser, log, command='uptime') +# return +# +# if __name__ == "__main__": +# main() \ No newline at end of file diff --git a/lib/service/tcpconnect.py b/lib/service/tcpconnect.py index 18fe5e7..2306b4b 100644 --- a/lib/service/tcpconnect.py +++ b/lib/service/tcpconnect.py @@ -1,33 +1,35 @@ """ -The tcpconnect service in pure Python. +The tcpconnect service. This is to check if a service on a specific port is reachable. + +This should just return 0 on success and NOT 0 on error. Just to make internals generic to just report this code and +not use a parser. """ import socket -import sys +from service import Service -HOST = 'linspector.org' -GET = '/index.html' -PORT = 80 -try: - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -except socket.error, msg: - sys.stderr.write("[ERROR] %s\n" % msg[1]) - sys.exit(1) +class TcpconnectService(Service): + def __init__(self, parser, log, **kwargs): + super(Service, self).__init__(parser) -try: - sock.connect((HOST, PORT)) -except socket.error, msg: - sys.stderr.write("[ERROR] %s\n" % msg[1]) - sys.exit(2) + if "port" in kwargs: + self.port = kwargs["port"] + else: + log.w("There is no port set") -sock.send("GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (GET, HOST)) + def execute(self, log): + try: + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + except socket.error, msg: + log.w("%s\n" % msg[1]) + self.errorcode = 1 -data = sock.recv(1024) -string = "" -while len(data): - string = string + data - data = sock.recv(1024) -sock.close() + try: + sock.connect((self.host, self.port)) + except socket.error, msg: + log.w("%s\n" % msg[1]) + self.errorcode = 2 -print string + sock.close() + return \ No newline at end of file