SSH, the Secure SHell, is an essential tool for many developers and administrators. SSH provides a way to establish encrypted, authenticated connections. The most common use of an SSH connection is to get a remote shell, but it’s possible to do many other things through SSH as well, including transferring files and tunneling other connections.
The twisted.conch package adds SSH support to Twisted. This chapter shows how you can use the modules intwisted.conchto build SSH servers and clients.
Setting Up a Custom SSH Server
The command line is an incredibly efficient interface for certain tasks. System administrators love the ability to manage applications by typing commands without having to click through a graphical user interface. An SSH shell is even better, as it’s accessible from anywhere on the Internet.
You can usetwisted.conchto create an SSH server that provides access to a custom shell with commands you define. This shell will even support some extra features like command history, so that you can scroll through the commands you’ve already typed.
How Do I Do That?
Write a subclass of twisted.conch.recvline.HistoricRecvLinethat implements your shell protocol.HistoricRecvLineis similar totwisted.protocols.basic.LineReceiver, but with higher-level features for controlling the terminal.
Write a subclass of twisted.conch.recvline.HistoricRecvLine that implements your shell protocol. HistoricRecvLine is similar to twisted.protocols.basic.LineReceiver, but with higher-level features for controlling the terminal.
To make your shell available through SSH, you need to implement a few different classes thattwisted.conchneeds to build an SSH server. First, you need thetwisted.credauthentication classes: a portal, credentials checkers, and a realm that returns avatars. Usetwisted.conch.avatar.ConchUseras the base class for your avatar. Your avatar class should also implementtwisted.conch.interfaces.ISession, which includes anopenShell method in which you create aProtocolto manage the user’s interactive session. Finally, create atwisted.conch.ssh.factory.SSHFactoryobject and set itsportalattribute to an instance of your portal.
Example 10-1 demonstrates a custom SSH server that authenticates users by their username and password. It gives each user a shell that provides several commands.
Example 10-1. sshserver.py
from twisted.cred import portal, checkers, credentials from twisted.conch import error, avatar, recvline, interfaces as conchinterfaces from twisted.conch.ssh import factory, userauth, connection, keys, session, common from twisted.conch.insults import insults from twisted.application import service, internet from zope.interface import implements import os
class SSHDemoProtocol(recvline.HistoricRecvLine): def __init__(self, user): self.user = user
def connectionMade(self): recvline.HistoricRecvLine.connectionMade(self) self.terminal.write("Welcome to my test SSH server.") self.terminal.nextLine() self.do_help() self.showPrompt()
def lineReceived(self, line): line = line.strip() if line: cmdAndArgs = line.split() cmd = cmdAndArgs[0] args = cmdAndArgs[1:] func = self.getCommandFunc(cmd) if func: try: func(*args) except Exception, e: self.terminal.write("Error: %s" % e) self.terminal.nextLine() else: self.terminal.write("No such command.") self.terminal.nextLine() self.showPrompt()
def do_help(self, cmd=''): "Get help on a command. Usage: help command" if cmd: func = self.getCommandFunc(cmd) if func: self.terminal.write(func.__doc__) self.terminal.nextLine() return