Last active
August 29, 2015 14:21
-
-
Save fo0nikens/0eb29ffce0bdeb8c1506 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import sys | |
import socket | |
import argparse | |
class Connector: | |
def __init__(self,hostname,password,port=4094): | |
self.hostname = hostname | |
self.password = password | |
self.port = port | |
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
self.discussion = [] | |
# Network functions | |
def connect(self): | |
self.socket.connect((self.hostname,self.port)) | |
def read(self): | |
data = self.socket.recv(1024) | |
self.discussion.append(['READ',data]) | |
return data | |
def send(self,msg): | |
data = bytes(msg,'utf-8') | |
self.discussion.append(['SEND',data]) | |
self.socket.send(data) | |
def close(self): | |
self.socket.close() | |
# Protocol functions | |
def read_header(self): | |
data = self.read() | |
self.fail(data != bytes('sysrqd password: ','ascii'),'Invalid welcome message') | |
def send_password(self): | |
self.send(self.password) | |
def read_prompt(self): | |
data = self.read() | |
self.fail(data != bytes('sysrq> ','ascii'),'Invalid prompt') | |
# Misc | |
def fail(self,cond,message): | |
if cond: | |
print(message) | |
print('{}:{} pass:«{}»'.format(self.hostname,self.port,self.password)) | |
print('\n'.join(['{} {}'.format(a[0],a[1]) for a in self.discussion])) | |
print('Aborting') | |
sys.exit(0) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument('hostname') | |
parser.add_argument('--password',default='yolo') | |
args = parser.parse_args() | |
c = Connector(args.hostname,args.password) | |
c.connect() | |
c.read_header() | |
c.send_password() | |
c.read_prompt() | |
c.send('sub') | |
c.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment