Commit f946a4bf authored by ibuler's avatar ibuler

finish example

parent db2d00f8
...@@ -6,10 +6,13 @@ ...@@ -6,10 +6,13 @@
import base64 import base64
from binascii import hexlify from binascii import hexlify
import os import os
import socket
import sys import sys
import threading import threading
import traceback import traceback
import tty
import termios
import struct, fcntl, signal, socket, select
import errno
import paramiko import paramiko
from paramiko.py3compat import b, u, decodebytes from paramiko.py3compat import b, u, decodebytes
...@@ -20,7 +23,7 @@ paramiko.util.log_to_file('demo_server.log') ...@@ -20,7 +23,7 @@ paramiko.util.log_to_file('demo_server.log')
host_key = paramiko.RSAKey(filename='test_rsa.key') host_key = paramiko.RSAKey(filename='test_rsa.key')
class Server(paramiko.ServerInterface): class SSHService(paramiko.ServerInterface):
# 'data' is the output of base64.encodestring(str(key)) # 'data' is the output of base64.encodestring(str(key))
# (using the "user_rsa_key" files) # (using the "user_rsa_key" files)
data = (b'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' data = (b'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp'
...@@ -68,11 +71,19 @@ class SSHServer: ...@@ -68,11 +71,19 @@ class SSHServer:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.bind((self.host, self.port)) self.sock.bind((self.host, self.port))
self.server_ssh = None
@staticmethod self.server_chan = None
def handle_ssh_request(client, addr):
def connect(self):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='127.0.0.1', port=22, username='root', password='redhat')
self.server_ssh = ssh
self.server_chan = channel = ssh.invoke_shell(term='xterm')
return channel
def handle_ssh_request(self, client, addr):
print('Got a connection!') print('Got a connection!')
try: try:
t = paramiko.Transport(client, gss_kex=False) t = paramiko.Transport(client, gss_kex=False)
t.set_gss_host(socket.getfqdn("")) t.set_gss_host(socket.getfqdn(""))
...@@ -82,35 +93,45 @@ class SSHServer: ...@@ -82,35 +93,45 @@ class SSHServer:
print('(Failed to load moduli -- gex will be unsupported.)') print('(Failed to load moduli -- gex will be unsupported.)')
raise raise
t.add_server_key(host_key) t.add_server_key(host_key)
server = Server() service = SSHService()
server.add_prompt(">>")
try: try:
t.start_server(server=server) t.start_server(server=service)
except paramiko.SSHException: except paramiko.SSHException:
print('*** SSH negotiation failed.') print('*** SSH negotiation failed.')
return return
while True:
# wait for auth
chan = t.accept(20) chan = t.accept(20)
if chan is None: if chan is None:
print('*** No channel.') print('*** No channel.')
return return
print('Authenticated!') print('Authenticated!')
server.event.wait(10) chan.settimeout(100)
if not server.event.is_set():
print('*** Client never asked for a shell.')
return
chan.send('\r\n\r\nWelcome to my dorky little BBS!\r\n\r\n') chan.send('\r\n\r\nWelcome to my dorky little BBS!\r\n\r\n')
chan.send('We are on fire all the time! Hooray! Candy corn for everyone!\r\n') chan.send('We are on fire all the time! Hooray! Candy corn for everyone!\r\n')
chan.send('Happy birthday to Robot Dave!\r\n\r\n') chan.send('Happy birthday to Robot Dave!\r\n\r\n')
chan.send('Username: ') server_chan = self.connect()
f = chan.makefile('rU') if not service.event.is_set():
username = f.readline().strip('\r\n') print('*** Client never asked for a shell.')
chan.send('\r\nI don\'t like you, ' + username + '.\r\n') return
chan.close() while True:
r, w, e = select.select([server_chan, chan], [], [])
if chan in r:
recv_data = chan.recv(1024).decode('utf8')
print("From client: " + repr(recv_data))
if len(recv_data) == 0:
break
server_chan.send(recv_data)
if server_chan in r:
recv_data = server_chan.recv(1024).decode('utf8')
print("From server: " + repr(recv_data))
if len(recv_data) == 0:
break
chan.send(recv_data)
except Exception as e: except Exception as e:
print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e)) print('*** Caught exception: ' + str(e.__class__) + ': ' + str(e))
...@@ -127,7 +148,7 @@ class SSHServer: ...@@ -127,7 +148,7 @@ class SSHServer:
try: try:
client, addr = self.sock.accept() client, addr = self.sock.accept()
print('Listening for connection ...') print('Listening for connection ...')
threading.Thread(target=self.handle_ssh_request, args=(client, addr)).start() threading.Thread(target=self.handle_ssh_request, args=( client, addr)).start()
except Exception as e: except Exception as e:
print('*** Bind failed: ' + str(e)) print('*** Bind failed: ' + str(e))
traceback.print_exc() traceback.print_exc()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment