Beispiel #1
0
    def test_5_cleanup(self):
        """
        verify that when an SSHClient is collected, its transport (and the
        transport's packetizer) is closed.
        """
        host_key = ssh.RSAKey.from_private_key_file('tests/test_rsa.key')
        public_host_key = ssh.RSAKey(data=str(host_key))

        self.tc = ssh.SSHClient()
        self.tc.set_missing_host_key_policy(ssh.AutoAddPolicy())
        self.assertEquals(0, len(self.tc.get_host_keys()))
        self.tc.connect(self.addr,
                        self.port,
                        username='******',
                        password='******')

        self.event.wait(1.0)
        self.assert_(self.event.isSet())
        self.assert_(self.ts.is_active())

        p = weakref.ref(self.tc._transport.packetizer)
        self.assert_(p() is not None)
        del self.tc
        # hrm, sometimes p isn't cleared right away.  why is that?
        st = time.time()
        while (time.time() - st < 5.0) and (p() is not None):
            time.sleep(0.1)
        self.assert_(p() is None)
Beispiel #2
0
    def test_4_auto_add_policy(self):
        """
        verify that SSHClient's AutoAddPolicy works.
        """
        host_key = ssh.RSAKey.from_private_key_file('tests/test_rsa.key')
        public_host_key = ssh.RSAKey(data=str(host_key))

        self.tc = ssh.SSHClient()
        self.tc.set_missing_host_key_policy(ssh.AutoAddPolicy())
        self.assertEquals(0, len(self.tc.get_host_keys()))
        self.tc.connect(self.addr,
                        self.port,
                        username='******',
                        password='******')

        self.event.wait(1.0)
        self.assert_(self.event.isSet())
        self.assert_(self.ts.is_active())
        self.assertEquals('slowdive', self.ts.get_username())
        self.assertEquals(True, self.ts.is_authenticated())
        self.assertEquals(1, len(self.tc.get_host_keys()))
        self.assertEquals(
            public_host_key,
            self.tc.get_host_keys()['[%s]:%d' %
                                    (self.addr, self.port)]['ssh-rsa'])
Beispiel #3
0
 def load_remote_rsa_key(self, remote_filename):
     """
     Returns ssh.RSAKey object for an RSA key located on the remote machine
     """
     rfile = self.remote_file(remote_filename, 'r')
     key = ssh.RSAKey(file_obj=rfile)
     rfile.close()
     return key
Beispiel #4
0
 def init_transport(self):
     transport = ssh.Transport(self.request)
     transport.add_server_key(ssh.RSAKey(filename=SERVER_PRIVKEY))
     transport.set_subsystem_handler('sftp',
                                     ssh.SFTPServer,
                                     sftp_si=FakeSFTPServer)
     server = TestServer(passwords, home, pubkeys, files)
     transport.start_server(server=server)
     self.ssh_server = server
     self.transport = transport
Beispiel #5
0
 def test_2_add(self):
     hostdict = ssh.HostKeys('hostfile.temp')
     hh = '|1|BMsIC6cUIP2zBuXR3t2LRcJYjzM=|hpkJMysjTk/+zzUUzxQEa2ieq6c='
     key = ssh.RSAKey(data=base64.decodestring(keyblob))
     hostdict.add(hh, 'ssh-rsa', key)
     self.assertEquals(3, len(hostdict))
     x = hostdict['foo.example.com']
     fp = hexlify(x['ssh-rsa'].get_fingerprint()).upper()
     self.assertEquals('7EC91BB336CB6D810B124B1353C32396', fp)
     self.assert_(hostdict.check('foo.example.com', key))
Beispiel #6
0
    def test_4_dict_set(self):
        hostdict = ssh.HostKeys('hostfile.temp')
        key = ssh.RSAKey(data=base64.decodestring(keyblob))
        key_dss = ssh.DSSKey(data=base64.decodestring(keyblob_dss))
        hostdict['secure.example.com'] = {'ssh-rsa': key, 'ssh-dss': key_dss}
        hostdict['fake.example.com'] = {}
        hostdict['fake.example.com']['ssh-rsa'] = key

        self.assertEquals(3, len(hostdict))
        self.assertEquals(2, len(hostdict.values()[0]))
        self.assertEquals(1, len(hostdict.values()[1]))
        self.assertEquals(1, len(hostdict.values()[2]))
        fp = hexlify(hostdict['secure.example.com']
                     ['ssh-rsa'].get_fingerprint()).upper()
        self.assertEquals('7EC91BB336CB6D810B124B1353C32396', fp)
        fp = hexlify(hostdict['secure.example.com']
                     ['ssh-dss'].get_fingerprint()).upper()
        self.assertEquals('4478F0B9A23CC5182009FF755BC1D26C', fp)
Beispiel #7
0
    def test_3_multiple_key_files(self):
        """
        verify that SSHClient accepts and tries multiple key files.
        """
        host_key = ssh.RSAKey.from_private_key_file('tests/test_rsa.key')
        public_host_key = ssh.RSAKey(data=str(host_key))

        self.tc = ssh.SSHClient()
        self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port),
                                    'ssh-rsa', public_host_key)
        self.tc.connect(
            self.addr,
            self.port,
            username='******',
            key_filename=['tests/test_rsa.key', 'tests/test_dss.key'])

        self.event.wait(1.0)
        self.assert_(self.event.isSet())
        self.assert_(self.ts.is_active())
        self.assertEquals('slowdive', self.ts.get_username())
        self.assertEquals(True, self.ts.is_authenticated())
Beispiel #8
0
class Server (ssh.ServerInterface):
    # 'data' is the output of base64.encodestring(str(key))
    # (using the "user_rsa_key" files)
    data = 'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + \
           'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \
           'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \
           'UWT10hcuO4Ks8='
    good_pub_key = ssh.RSAKey(data=base64.decodestring(data))

    def __init__(self):
        self.event = threading.Event()

    def check_channel_request(self, kind, chanid):
        if kind == 'session':
            return ssh.OPEN_SUCCEEDED
        return ssh.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED

    def check_auth_password(self, username, password):
        if (username == 'robey') and (password == 'foo'):
            return ssh.AUTH_SUCCESSFUL
        return ssh.AUTH_FAILED

    def check_auth_publickey(self, username, key):
        print 'Auth attempt with key: ' + hexlify(key.get_fingerprint())
        if (username == 'robey') and (key == self.good_pub_key):
            return ssh.AUTH_SUCCESSFUL
        return ssh.AUTH_FAILED

    def get_allowed_auths(self, username):
        return 'password,publickey'

    def check_channel_shell_request(self, channel):
        self.event.set()
        return True

    def check_channel_pty_request(self, channel, term, width, height, pixelwidth,
                                  pixelheight, modes):
        return True
Beispiel #9
0
    def test_1_client(self):
        """
        verify that the SSHClient stuff works too.
        """
        host_key = ssh.RSAKey.from_private_key_file('tests/test_rsa.key')
        public_host_key = ssh.RSAKey(data=str(host_key))

        self.tc = ssh.SSHClient()
        self.tc.get_host_keys().add('[%s]:%d' % (self.addr, self.port),
                                    'ssh-rsa', public_host_key)
        self.tc.connect(self.addr,
                        self.port,
                        username='******',
                        password='******')

        self.event.wait(1.0)
        self.assert_(self.event.isSet())
        self.assert_(self.ts.is_active())
        self.assertEquals('slowdive', self.ts.get_username())
        self.assertEquals(True, self.ts.is_authenticated())

        stdin, stdout, stderr = self.tc.exec_command('yes')
        schan = self.ts.accept(1.0)

        schan.send('Hello there.\n')
        schan.send_stderr('This is on stderr.\n')
        schan.close()

        self.assertEquals('Hello there.\n', stdout.readline())
        self.assertEquals('', stdout.readline())
        self.assertEquals('This is on stderr.\n', stderr.readline())
        self.assertEquals('', stderr.readline())

        stdin.close()
        stdout.close()
        stderr.close()
Beispiel #10
0
import base64
from binascii import hexlify
import os
import socket
import sys
import threading
import traceback

import ssh


# setup logging
ssh.util.log_to_file('demo_server.log')

host_key = ssh.RSAKey(filename='test_rsa.key')
#host_key = ssh.DSSKey(filename='test_dss.key')

print 'Read key: ' + hexlify(host_key.get_fingerprint())


class Server (ssh.ServerInterface):
    # 'data' is the output of base64.encodestring(str(key))
    # (using the "user_rsa_key" files)
    data = 'AAAAB3NzaC1yc2EAAAABIwAAAIEAyO4it3fHlmGZWJaGrfeHOVY7RWO3P9M7hp' + \
           'fAu7jJ2d7eothvfeuoRFtJwhUmZDluRdFyhFY/hFAh76PJKGAusIqIQKlkJxMC' + \
           'KDqIexkgHAfID/6mqvmnSJf0b5W8v5h2pI/stOSwTQ+pxVhwJ9ctYDhRSlF0iT' + \
           'UWT10hcuO4Ks8='
    good_pub_key = ssh.RSAKey(data=base64.decodestring(data))

    def __init__(self):