コード例 #1
0
def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    if 'fake' in ip:
        return fakeprovider.FakeSSHClient()
    # HPcloud may return ECONNREFUSED or EHOSTUNREACH
    # for about 30 seconds after adding the IP
    for count in iterate_timeout(timeout, exceptions.SSHTimeoutException,
                                 "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except paramiko.SSHException as e:
            # NOTE(pabelanger): Currently paramiko only returns a string with
            # error code. If we want finer granularity we'll need to regex the
            # string.
            log.exception('Failed to negotiate SSH: %s' % (e))
        except paramiko.AuthenticationException as e:
            # This covers the case where the cloud user is created
            # after sshd is up (Fedora for example)
            log.info('Auth exception for %s@%s. Try number %i...' %
                     (username, ip, count))
        except socket.error as e:
            if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH, None]:
                log.exception('Exception while testing ssh access to %s:' % ip)

    out = client.ssh("test ssh access", "echo access okay", output=True)
    if "access okay" in out:
        return client
    return None
コード例 #2
0
ファイル: nodeutils.py プロジェクト: Tesora/tesora-nodepool
def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    if 'fake' in ip:
        return fakeprovider.FakeSSHClient()
    # HPcloud may return ECONNREFUSED or EHOSTUNREACH
    # for about 30 seconds after adding the IP
    for count in iterate_timeout(
            timeout, exceptions.SSHTimeoutException, "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except paramiko.SSHException as e:
            # NOTE(pabelanger): Currently paramiko only returns a string with
            # error code. If we want finer granularity we'll need to regex the
            # string.
            log.exception('Failed to negotiate SSH: %s' % (e))
        except paramiko.AuthenticationException as e:
            # This covers the case where the cloud user is created
            # after sshd is up (Fedora for example)
            log.info('Auth exception for %s@%s. Try number %i...' %
                     (username, ip, count))
        except socket.error as e:
            if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH, None]:
                log.exception(
                    'Exception while testing ssh access to %s:' % ip)

    out = client.ssh("test ssh access", "echo access okay", output=True)
    if "access okay" in out:
        return client
    return None
コード例 #3
0
ファイル: test_timeouts.py プロジェクト: cpopp/txsshclient
class IPV4FTPTimeoutTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SlowSSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            'hostname': self.hostname,
            'port': self.portnum,
            'user': self.user,
            'password': self.password,
            'buffersize': 32768
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults(
            [d, client.onConnectionLost, server.onConnectionLost])

    def test_lsdir_timeout_fail(self):
        d = self.client.ls('/tmp', timeout=1)
        return self.assertFailure(d, TimeoutError)

    @defer.inlineCallbacks
    def test_lsdir_timeout_pass(self):
        try:
            test_file = 'test_ls_dir'
            sandbox = tempfile.mkdtemp()
            testfile = '/'.join([sandbox, test_file])
            touch(testfile)
            d = yield self.client.ls(sandbox, timeout=60)

            self.assertEquals(d[0][0], test_file)
            defer.returnValue(d)
        finally:
            shutil.rmtree(sandbox)
class IPV4FunctionalNoServerTestCase(TestCase):
    def setUp(self):
        self.hostname = "127.0.0.1"
        self.user = getpass.getuser()
        self.password = "******"
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            "hostname": self.hostname,
            "port": self.portnum + 1,
            "user": self.user,
            "password": self.password,
            "buffersize": 32768,
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        # Wait for the deferred that tell us we disconnected.
        return defer.gatherResults([d])

    def test_run_command_connect_failure(self):
        "test what happens if the server isnt running"
        d = self.client.run("echo hi")
        return self.assertFailure(d, ConnectError)

    def test_ls_connect_failure(self):
        "test what happens if the server isnt running"

        sandbox = tempfile.mkdtemp()
        d = self.client.ls(sandbox)

        def sandbox_cleanup(data):
            shutil.rmtree(sandbox)
            return data

        d.addBoth(sandbox_cleanup)
        return self.assertFailure(d, ConnectError)
class IPV4FTPTimeoutTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SlowSSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.user,
                   'password': self.password,
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults([d,
                                    client.onConnectionLost,
                                    server.onConnectionLost])

    def test_lsdir_timeout_fail(self):
        d = self.client.ls('/tmp', timeout=1)
        return self.assertFailure(d, TimeoutError)

    @defer.inlineCallbacks
    def test_lsdir_timeout_pass(self):
        try:
            test_file = 'test_ls_dir'
            sandbox = tempfile.mkdtemp()
            testfile = '/'.join([sandbox, test_file])
            touch(testfile)
            d = yield self.client.ls(sandbox, timeout=60)

            self.assertEquals(d[0][0], test_file)
            defer.returnValue(d)
        finally:
            shutil.rmtree(sandbox)
コード例 #6
0
class IPV4FunctionalNoServerTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            'hostname': self.hostname,
            'port': self.portnum + 1,
            'user': self.user,
            'password': self.password,
            'buffersize': 32768
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        # Wait for the deferred that tell us we disconnected.
        return defer.gatherResults([d])

    def test_run_command_connect_failure(self):
        'test what happens if the server isnt running'
        d = self.client.run('echo hi')
        return self.assertFailure(d, ConnectError)

    def test_ls_connect_failure(self):
        'test what happens if the server isnt running'

        sandbox = tempfile.mkdtemp()
        d = self.client.ls(sandbox)

        def sandbox_cleanup(data):
            shutil.rmtree(sandbox)
            return data

        d.addBoth(sandbox_cleanup)
        return self.assertFailure(d, ConnectError)
コード例 #7
0
class IPV6FunctionalBaseTestCase(TestCase):
    def setUp(self):
        self.hostname = '::1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            'hostname': self.hostname,
            'port': self.portnum,
            'user': self.user,
            'password': self.password,
            'buffersize': 32768
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults(
            [d, client.onConnectionLost, server.onConnectionLost])

    def test_run_command(self):

        d = self.client.run('echo hi')

        def got_hi(data):
            log.debug('Got Data %s' % (data, ))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output, 'hi\n')
            return data

        d.addCallback(got_hi)
        #d = defer.Deferred()
        #d.callback('done')
        return d
class IPV6FunctionalBaseTestCase(TestCase):
    def setUp(self):
        self.hostname = '::1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.user,
                   'password': self.password,
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults([d,
                                    client.onConnectionLost,
                                    server.onConnectionLost])

    def test_run_command(self):

        d = self.client.run('echo hi')

        def got_hi(data):
            log.debug('Got Data %s' % (data,))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output,  'hi\n')
            return data

        d.addCallback(got_hi)
        #d = defer.Deferred()
        #d.callback('done')
        return d
コード例 #9
0
ファイル: utils.py プロジェクト: scurmmaster/config
def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    # HPcloud may return errno 111 for about 30 seconds after adding the IP
    for count in iterate_timeout(timeout, "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except socket.error as e:
            print "While testing ssh access:", e
            time.sleep(5)

    ret, out = client.ssh("echo access okay")
    if "access okay" in out:
        return client
    return None
コード例 #10
0
ファイル: utils.py プロジェクト: SlickNik/config
def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    # HPcloud may return errno 111 for about 30 seconds after adding the IP
    for count in iterate_timeout(timeout, "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except socket.error as e:
            print "While testing ssh access:", e
            time.sleep(5)

    ret, out = client.ssh("echo access okay")
    if "access okay" in out:
        return client
    return None
コード例 #11
0
    def test_password_identity_no_passphrase_success(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': '******',
                   'identities': [self.sandbox + '/id_rsa2'],
                   'buffersize': 32768}

        id_rsa_pub = testUsers['testUser2']['pub']
        id_rsa_priv = testUsers['testUser2']['priv']

        open(self.sandbox+'/id_rsa2.pub', 'w').write(id_rsa_pub)
        open(self.sandbox+'/id_rsa2', 'w').write(id_rsa_priv)

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        def got_hi(data):
            log.debug('Got Data %s' % (data,))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output,  'hi\n')
            return data

        d.addBoth(client_disconnect)
        d.addCallback(got_hi)

        return d
コード例 #12
0
    def test_password_identity_failure(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.sshServeruser,
                   'password': '******',  # sshkey phrase
                   'identities': [self.sandbox + '/id_rsa'],
                   'buffersize': 32768}

        id_rsa_pub = testUsers['testUser']['pub']
        id_rsa_priv = testUsers['testUser']['priv']

        open(self.sandbox+'/id_rsa.pub', 'w').write(id_rsa_pub)
        open(self.sandbox+'/id_rsa', 'w').write(id_rsa_priv)

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        d.addBoth(client_disconnect)

        return self.assertFailure(d, UnauthorizedLogin)
コード例 #13
0
    def test_password_auth_success(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.sshServeruser,
                   'password': self.sshServerPassword,
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        def got_hi(data):
            log.debug('Got Data %s' % (data,))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output,  'hi\n')
            return data

        d.addBoth(client_disconnect)
        d.addCallback(got_hi)

        return d
コード例 #14
0
def openConsole(ip, username, password):
    clientIp = cgi.escape(os.environ["REMOTE_ADDR"])
    bodyContent = ""
    bodyContent += 'Your machine IP is %s <br>' % (str(clientIp))
    try:
        cl = SSHClient(clientIp)
        cl.enableSSH(ip, username)
        return {'status': 'success', 'data': 'SSH Successfull'}
    except Exception as e:
        return {
            'status':
            'error',
            'data':
            'Could not connect to remote machine. Make sure the plugin is running in your machine, Cause : %s'
            % str(e)
        }
コード例 #15
0
def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    # HPcloud may return errno 111 for about 30 seconds after adding the IP
    for count in iterate_timeout(timeout, "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except socket.error, e:
            print "While testing ssh access:", e
コード例 #16
0
ファイル: nodeutils.py プロジェクト: yrobla/scalator
def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    for count in iterate_timeout(timeout, "ssh access"):
        try:
            log.debug('Connecting to ssh to %s with user %s' % (ip, username))
            client = SSHClient(ip, username, log=log, **connect_kwargs)
            break
        except paramiko.AuthenticationException as e:
            # This covers the case where the cloud user is created
            # after sshd is up (Fedora for example)
            log.info('Password auth exception. Try number %i...' % count)
            log.debug(str(e))
        except socket.error as e:
            if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH]:
                log.exception('Exception while testing ssh access:')

    out = client.ssh("test ssh access", "echo access okay", output=True)
    if "access okay" in out:
        return client
    return None
コード例 #17
0
ファイル: test_timeouts.py プロジェクト: cpopp/txsshclient
class IPV4CommandTimeoutTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            'hostname': self.hostname,
            'port': self.portnum,
            'user': self.user,
            'password': self.password,
            'buffersize': 32768
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults(
            [d, client.onConnectionLost, server.onConnectionLost])

    def test_run_command_timeout_failed(self):
        d = self.client.run('sleep 2 && ls', timeout=1)
        return self.assertFailure(d, TimeoutError)
class IPV4CommandTimeoutTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.user,
                   'password': self.password,
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults([d,
                                    client.onConnectionLost,
                                    server.onConnectionLost])

    def test_run_command_timeout_failed(self):
        d = self.client.run('sleep 2 && ls', timeout=1)
        return self.assertFailure(d, TimeoutError)
コード例 #19
0
def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    if 'fake' in ip:
        return fakeprovider.FakeSSHClient()
    # HPcloud may return ECONNREFUSED or EHOSTUNREACH
    # for about 30 seconds after adding the IP
    for count in iterate_timeout(timeout, "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except paramiko.AuthenticationException as e:
            # This covers the case where the cloud user is created
            # after sshd is up (Fedora for example)
            log.info('Password auth exception. Try number %i...' % count)
        except socket.error as e:
            if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH, None]:
                log.exception('Exception while testing ssh access:')

    out = client.ssh("test ssh access", "echo access okay", output=True)
    if "access okay" in out:
        return client
    return None
コード例 #20
0
ファイル: test_timeouts.py プロジェクト: cpopp/txsshclient
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            'hostname': self.hostname,
            'port': self.portnum,
            'user': self.user,
            'password': self.password,
            'buffersize': 32768
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()
コード例 #21
0
ファイル: nodeutils.py プロジェクト: dixudx/nodepool
def ssh_connect(ip, username, connect_kwargs={}, timeout=60):
    if ip == 'fake':
        return fakeprovider.FakeSSHClient()
    # HPcloud may return ECONNREFUSED or EHOSTUNREACH
    # for about 30 seconds after adding the IP
    for count in iterate_timeout(timeout, "ssh access"):
        try:
            client = SSHClient(ip, username, **connect_kwargs)
            break
        except paramiko.AuthenticationException as e:
            # This covers the case where the cloud user is created
            # after sshd is up (Fedora for example)
            log.info('Password auth exception. Try number %i...' % count)
        except socket.error as e:
            if e[0] not in [errno.ECONNREFUSED, errno.EHOSTUNREACH]:
                log.exception('Exception while testing ssh access:')

    out = client.ssh("test ssh access", "echo access okay", output=True)
    if "access okay" in out:
        return client
    return None
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.user,
                   'password': self.password,
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()
コード例 #23
0
    def test_password_auth_failure(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.sshServeruser,
                   'password': '******',
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        d.addBoth(client_disconnect)

        return self.assertFailure(d, UnauthorizedLogin)
コード例 #24
0
    def setUp(self):
        self.hostname = "127.0.0.1"
        self.user = getpass.getuser()
        self.password = "******"
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            "hostname": self.hostname,
            "port": self.portnum,
            "user": self.user,
            "password": self.password,
            "buffersize": 32768,
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()
コード例 #25
0
ファイル: dut.py プロジェクト: sniu0320/MyTool2020
    def __init__(self,
                 mng_ip,
                 username='******',
                 password='******',
                 enable='enable',
                 password_enable='zxr10',
                 telnet_port=23,
                 ssh_port=22):
        '''
        设备初始化
        '''
        self.mng_ip = mng_ip
        self.username = username
        self.password = password
        self.enable = enable
        self.password_enable = password_enable
        self.telnet = TelnetClient()
        self.telnet_port = telnet_port
        self.ssh = SSHClient()
        self.ssh_port = ssh_port

        self.dutType = None  # 设备类型 9900 or 5960 or 其他
        self.version = None  # 版本信息
        self.snmp_enable = False
コード例 #26
0
class IPV4FunctionalAuthTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.sshServeruser = '******'
        self.sshServerPassword = '******'
        self.server = SSHServer(self.sshServeruser, self.sshServerPassword)
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        # create ssh auth sandbox for keys.
        self.sandbox = tempfile.mkdtemp()

    def tearDown(self):

        # Shut down the server and client
        port, self.port = self.port, None
        #client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Remove the sandbox
        shutil.rmtree(self.sandbox)

        # Wait for the deferred that tell us we disconnected.
        return defer.gatherResults([d])

    def test_password_auth_success(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.sshServeruser,
                   'password': self.sshServerPassword,
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        def got_hi(data):
            log.debug('Got Data %s' % (data,))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output,  'hi\n')
            return data

        d.addBoth(client_disconnect)
        d.addCallback(got_hi)

        return d

    def test_password_auth_failure(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.sshServeruser,
                   'password': '******',
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        d.addBoth(client_disconnect)

        return self.assertFailure(d, UnauthorizedLogin)

    def test_password_identity_success(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.sshServeruser,
                   'password': '******',  # sshkey phrase
                   'identities': [self.sandbox+'/id_rsa'],
                   'buffersize': 32768}

        id_rsa_pub = testUsers['testUser']['pub']
        id_rsa_priv = testUsers['testUser']['priv']

        open(self.sandbox+'/id_rsa.pub', 'w').write(id_rsa_pub)
        open(self.sandbox+'/id_rsa', 'w').write(id_rsa_priv)
        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        def got_hi(data):
            log.debug('Got Data %s' % (data,))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output,  'hi\n')
            return data

        d.addBoth(client_disconnect)
        d.addCallback(got_hi)

        return d

    def test_password_identity_failure(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.sshServeruser,
                   'password': '******',  # sshkey phrase
                   'identities': [self.sandbox + '/id_rsa'],
                   'buffersize': 32768}

        id_rsa_pub = testUsers['testUser']['pub']
        id_rsa_priv = testUsers['testUser']['priv']

        open(self.sandbox+'/id_rsa.pub', 'w').write(id_rsa_pub)
        open(self.sandbox+'/id_rsa', 'w').write(id_rsa_priv)

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        d.addBoth(client_disconnect)

        return self.assertFailure(d, UnauthorizedLogin)

    def test_password_identity_no_passphrase_success(self):
        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': '******',
                   'identities': [self.sandbox + '/id_rsa2'],
                   'buffersize': 32768}

        id_rsa_pub = testUsers['testUser2']['pub']
        id_rsa_priv = testUsers['testUser2']['priv']

        open(self.sandbox+'/id_rsa2.pub', 'w').write(id_rsa_pub)
        open(self.sandbox+'/id_rsa2', 'w').write(id_rsa_priv)

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

        d = self.client.run('echo hi')

        def client_disconnect(data):
            log.debug('Disconnecting client')
            self.client.disconnect()
            return data

        def got_hi(data):
            log.debug('Got Data %s' % (data,))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output,  'hi\n')
            return data

        d.addBoth(client_disconnect)
        d.addCallback(got_hi)

        return d
コード例 #27
0
class IPV4FunctionalNoReconnectionTestCase(TestCase):
    def setUp(self):
        self.timeout = 10
        self.hostname = "127.0.0.1"
        self.user = getpass.getuser()
        self.password = "******"
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            "hostname": self.hostname,
            "port": self.portnum,
            "user": self.user,
            "password": self.password,
            "buffersize": 32768,
        }

        self.client = SSHClient(options)
        self.client.maxRetries = 0
        # self.client.maxDelay = 0
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug("tearing down")
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        # Wait for the server to stop.
        return defer.gatherResults([d])

    def test_run_command(self):
        def server_stop_listening(data):
            sld = self.port.stopListening()
            return sld

        def server_drop_connections(data):
            port, self.port = self.port, None
            server, self.server = self.server, None
            server.protocol.transport.loseConnection()
            log.debug("Dropping server connection")
            return self.client.onConnectionLost

        def run_command(data):

            log.debug("running command hi2")
            results = self.client.run("echo hi2")
            return results

        def test_failure_done(deferred):
            log.debug("Failure %s " % deferred)
            return self.assertEqual(deferred.type, ConnectionDone)

        def test_success(data):
            log.debug("Success %s" % (data,))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output, "hi\n")

        def bring_up_server(data):
            self.server = SSHServer()
            self.server.protocol = ServerProtocol
            self.port = reactor.listenTCP(self.portnum, self.server, interface=self.hostname)
            log.debug("server started")
            return data.factory.dConnected

        d = self.client.run("echo hi")
        d.addBoth(test_success)
        d.addCallback(server_stop_listening)
        d.addCallback(server_drop_connections)
        d.addCallback(bring_up_server)
        d.addCallback(run_command)
        d.addErrback(test_failure_done)

        return d

    def test_lsdir(self):
        test_file = "test_ls_dir"
        sandbox = tempfile.mkdtemp()
        testfile = "/".join([sandbox, test_file])
        touch(testfile)
        d = self.client.ls(sandbox)

        def cleanup_sandbox(data):
            log.debug("Cleaning up sandbox")
            shutil.rmtree(sandbox)

        def test_success(data):
            return self.assertEqual(data[0][0], test_file)

        def server_stop_listening(data):
            sld = self.port.stopListening()
            return sld

        def server_drop_connections(data):
            port, self.port = self.port, None
            server, self.server = self.server, None
            server.protocol.transport.loseConnection()
            log.debug("Dropping server connection")
            return self.client.onConnectionLost

        def bring_up_server(data):
            self.server = SSHServer()
            self.server.protocol = ServerProtocol
            self.port = reactor.listenTCP(self.portnum, self.server, interface=self.hostname)
            log.debug("server started")
            return data.factory.dConnected

        def run_lsdir(data):

            log.debug("running command ls again")
            results = self.client.ls(sandbox)
            return results

        def test_failure_done(deferred):
            log.debug("Failure %s " % deferred)
            return self.assertEqual(deferred.type, ConnectionDone)

        d.addCallback(test_success)
        d.addCallback(server_stop_listening)
        d.addCallback(server_drop_connections)
        d.addCallback(bring_up_server)
        d.addCallback(run_lsdir)
        d.addErrback(test_failure_done)
        d.addBoth(cleanup_sandbox)

        return d
コード例 #28
0
class IPV4FunctionalNoReconnectionTestCase(TestCase):
    def setUp(self):
        self.timeout = 10
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            'hostname': self.hostname,
            'port': self.portnum,
            'user': self.user,
            'password': self.password,
            'buffersize': 32768
        }

        self.client = SSHClient(options)
        self.client.maxRetries = 0
        #self.client.maxDelay = 0
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        # Wait for the server to stop.
        return defer.gatherResults([d])

    def test_run_command(self):
        def server_stop_listening(data):
            sld = self.port.stopListening()
            return sld

        def server_drop_connections(data):
            port, self.port = self.port, None
            server, self.server = self.server, None
            server.protocol.transport.loseConnection()
            log.debug('Dropping server connection')
            return self.client.onConnectionLost

        def run_command(data):

            log.debug('running command hi2')
            results = self.client.run('echo hi2')
            return results

        def test_failure_done(deferred):
            log.debug('Failure %s ' % deferred)
            return self.assertEqual(deferred.type, ConnectionDone)

        def test_success(data):
            log.debug('Success %s' % (data, ))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output, 'hi\n')

        def bring_up_server(data):
            self.server = SSHServer()
            self.server.protocol = ServerProtocol
            self.port = reactor.listenTCP(self.portnum,
                                          self.server,
                                          interface=self.hostname)
            log.debug('server started')
            return data.factory.dConnected

        d = self.client.run('echo hi')
        d.addBoth(test_success)
        d.addCallback(server_stop_listening)
        d.addCallback(server_drop_connections)
        d.addCallback(bring_up_server)
        d.addCallback(run_command)
        d.addErrback(test_failure_done)

        return d

    def test_lsdir(self):
        test_file = 'test_ls_dir'
        sandbox = tempfile.mkdtemp()
        testfile = '/'.join([sandbox, test_file])
        touch(testfile)
        d = self.client.ls(sandbox)

        def cleanup_sandbox(data):
            log.debug('Cleaning up sandbox')
            shutil.rmtree(sandbox)

        def test_success(data):
            return self.assertEqual(data[0][0], test_file)

        def server_stop_listening(data):
            sld = self.port.stopListening()
            return sld

        def server_drop_connections(data):
            port, self.port = self.port, None
            server, self.server = self.server, None
            server.protocol.transport.loseConnection()
            log.debug('Dropping server connection')
            return self.client.onConnectionLost

        def bring_up_server(data):
            self.server = SSHServer()
            self.server.protocol = ServerProtocol
            self.port = reactor.listenTCP(self.portnum,
                                          self.server,
                                          interface=self.hostname)
            log.debug('server started')
            return data.factory.dConnected

        def run_lsdir(data):

            log.debug('running command ls again')
            results = self.client.ls(sandbox)
            return results

        def test_failure_done(deferred):
            log.debug('Failure %s ' % deferred)
            return self.assertEqual(deferred.type, ConnectionDone)

        d.addCallback(test_success)
        d.addCallback(server_stop_listening)
        d.addCallback(server_drop_connections)
        d.addCallback(bring_up_server)
        d.addCallback(run_lsdir)
        d.addErrback(test_failure_done)
        d.addBoth(cleanup_sandbox)

        return d
コード例 #29
0
ファイル: pod.py プロジェクト: shreyasbs123/samplevnf
class Pod:
    """Class which represents test pods.
    For example with traffic gen, forward/swap applications, etc
    """
    k8s_CoreV1Api = None

    _log = None

    _name = "pod"
    _namespace = "default"
    _nodeSelector_hostname = None
    _last_status = None
    _id = None
    _admin_ip = None
    _dp_ip = None

    _ssh_client = None

    _sriov_vf = None
    _sriov_vf_mac = None

    def __init__(self, name, namespace="default", logger_name="k8srapid"):
        self._log = logging.getLogger(logger_name)

        self._name = name
        self._namespace = namespace
        self._ssh_client = SSHClient(logger_name=logger_name)

    def __del__(self):
        """Destroy POD. Do a cleanup.
        """
        if self._ssh_client is not None:
            self._ssh_client.disconnect()

    def create_from_yaml(self, file_name):
        """Load POD description from yaml file.
        """
        with open(path.join(path.dirname(__file__), file_name)) as yaml_file:
            self.body = yaml.safe_load(yaml_file)

            self.body["metadata"]["name"] = self._name

            if (self._nodeSelector_hostname is not None):
                if ("nodeSelector" not in self.body["spec"]):
                    self.body["spec"]["nodeSelector"] = {}
                self.body["spec"]["nodeSelector"][
                    "kubernetes.io/hostname"] = self._nodeSelector_hostname
            self._log.debug("Creating POD, body:\n%s" % self.body)

            try:
                self.k8s_CoreV1Api.create_namespaced_pod(
                    body=self.body, namespace=self._namespace)
            except client.rest.ApiException as e:
                self._log.error("Couldn't create POD %s!\n%s\n" %
                                (self._name, e))

    def terminate(self):
        """Terminate POD. Close SSH connection.
        """
        if self._ssh_client is not None:
            self._ssh_client.disconnect()

        try:
            self.k8s_CoreV1Api.delete_namespaced_pod(name=self._name,
                                                     namespace=self._namespace)
        except client.rest.ApiException as e:
            if e.reason != "Not Found":
                self._log.error("Couldn't delete POD %s!\n%s\n" %
                                (self._name, e.reason))

    def update_admin_ip(self):
        """Check for admin IP address assigned by k8s.
        """
        try:
            pod = self.k8s_CoreV1Api.read_namespaced_pod_status(
                name=self._name, namespace=self._namespace)
            self._admin_ip = pod.status.pod_ip
        except client.rest.ApiException as e:
            self._log.error("Couldn't update POD %s admin IP!\n%s\n" %
                            (self._name, e))

    def wait_for_start(self):
        """Wait for POD to start.
        """
        self._log.info("Waiting for POD %s to start..." % self._name)
        while True:
            self.get_status()
            if (self._last_status == "Running" or self._last_status == "Failed"
                    or self._last_status == "Unknown"):
                break
            else:
                time.sleep(3)

        self.update_admin_ip()

        return self._last_status

    def ssh_run_cmd(self, cmd):
        """Execute command for POD via SSH connection.
        SSH credentials should be configured before use of this function.
        """
        self._ssh_client.run_cmd(cmd)

    def get_name(self):
        return self._name

    def get_admin_ip(self):
        return self._admin_ip

    def get_dp_ip(self):
        return self._dp_ip

    def get_dp_mac(self):
        return self._sriov_vf_mac

    def get_dp_pci_dev(self):
        return self._sriov_vf

    def get_id(self):
        return self._id

    def get_status(self):
        """Get current status fro the pod.
        """
        try:
            pod = self.k8s_CoreV1Api.read_namespaced_pod_status(
                name=self._name, namespace=self._namespace)
        except client.rest.ApiException as e:
            self._log.error("Couldn't read POD %s status!\n%s\n" %
                            (self._name, e))

        self._last_status = pod.status.phase
        return self._last_status

    def get_sriov_dev_mac(self):
        """Get assigned by k8s SRIOV network device plugin SRIOV VF devices.
        Return 0 in case of sucessfull configuration.
        Otherwise return -1.
        """
        self._log.info("Checking assigned SRIOV VF for POD %s" % self._name)
        ret = self._ssh_client.run_cmd(
            "cat /opt/rapid/k8s_sriov_device_plugin_envs")
        if ret != 0:
            self._log.error("Failed to check assigned SRIOV VF!"
                            "Error %s" % self._ssh_client.get_error())
            return -1

        cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
        self._log.debug("Environment variable %s" % cmd_output)

        # Parse environment variable
        cmd_output = cmd_output.split("=")[1]
        self._sriov_vf = cmd_output.split(",")[0]
        self._log.debug("Using first SRIOV VF %s" % self._sriov_vf)

        self._log.info("Getting MAC address for assigned SRIOV VF %s" %
                       self._sriov_vf)
        self._ssh_client.run_cmd("sudo /opt/rapid/port_info_app -n 4 -w %s" %
                                 self._sriov_vf)
        if ret != 0:
            self._log.error("Failed to get MAC address!"
                            "Error %s" % self._ssh_client.get_error())
            return -1

        # Parse MAC address
        cmd_output = self._ssh_client.get_output().decode("utf-8").rstrip()
        self._log.debug(cmd_output)
        cmd_output = cmd_output.splitlines()
        for line in cmd_output:
            if line.startswith("Port 0 MAC: "):
                self._sriov_vf_mac = line[12:]

        self._log.debug("MAC %s" % self._sriov_vf_mac)

    def set_dp_ip(self, dp_ip):
        self._dp_ip = dp_ip

    def set_id(self, pod_id):
        self._id = pod_id

    def set_nodeselector(self, hostname):
        """Set hostname on which POD will be executed.
        """
        self._nodeSelector_hostname = hostname

    def set_ssh_credentials(self, user, rsa_private_key):
        """Set SSH credentials for the SSH connection to the POD.
        """
        self.update_admin_ip()
        self._ssh_client.set_credentials(ip=self._admin_ip,
                                         user=user,
                                         rsa_private_key=rsa_private_key)
コード例 #30
0
ファイル: client.py プロジェクト: cpopp/txsshclient
logging.basicConfig(level=logging.DEBUG)
#logging.basicConfig(level=logging.INFO)
from twisted.python import log as twistedlog
observer = twistedlog.PythonLoggingObserver()
observer.start()
log = logging.getLogger('txsshclient.client')

options = {
    'hostname': '127.0.0.1',
    'port': 2222,
    'user': '******',
    'password': '******',
    'buffersize': 32768
}

client = SSHClient(options)
client.connect()


def retry():
    log.debug('retrying')
    d = client.run('sleep 5 && ls')

    def failed_or_success(result):
        if isinstance(result, failure.Failure):
            log.info("Failed %s" % (result, ))
        else:
            log.info("Success %s" % (result, ))

    d.addBoth(failed_or_success)
class IPV4FunctionalBaseTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {'hostname': self.hostname,
                   'port': self.portnum,
                   'user': self.user,
                   'password': self.password,
                   'buffersize': 32768}

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults([d,
                                    client.onConnectionLost,
                                    server.onConnectionLost])

    def test_run_command(self):

        d = self.client.run('echo hi')

        def got_hi(data):
            log.debug('Got Data %s' % (data,))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output,  'hi\n')
            return data

        d.addCallback(got_hi)
        return d

    @defer.inlineCallbacks
    def test_lsdir(self):
        try:
            test_file = 'test_ls_dir'
            sandbox = tempfile.mkdtemp()
            testfile = '/'.join([sandbox, test_file])
            touch(testfile)
            d = yield self.client.ls(sandbox)

            self.assertEquals(d[0][0], test_file)
            defer.returnValue(d)
        finally:
            shutil.rmtree(sandbox)

    def test_lsdir_no_dir(self):
        d = self.client.ls('/_not_real')
        return self.assertFailure(d, SFTPError)

    @defer.inlineCallbacks
    def test_mkdir(self):
        try:
            sandbox = tempfile.mkdtemp()
            test_dir = 'tmpMkdir'
            directory = '/'.join([sandbox, test_dir])
            result = yield self.client.mkdir(directory)
            self.assertEquals(result[0], 'mkdir succeeded')
            self.assertTrue(os.path.isdir(directory))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_rmdir(self):
        try:
            sandbox = tempfile.mkdtemp()
            test_dir = 'tmpRmdir'
            directory = '/'.join([sandbox, test_dir])
            os.mkdir(directory)
            result = yield self.client.rmdir(directory)
            self.assertEquals(result[0], 'rmdir succeeded')
            self.assertFalse(os.path.exists(directory))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_rename(self):
        try:
            original_filename = 'test_rename'
            destination_filename = 'test_rename_changed'
            sandbox = tempfile.mkdtemp()
            original_path = '/'.join([sandbox, original_filename])
            destination_path = '/'.join([sandbox, destination_filename])
            touch(original_path)

            result = yield self.client.rename(original_path,
                                              destination_path)
            self.assertEquals(result[0], 'rename succeeded')
            self.assertFalse(os.path.exists(original_path))
            self.assertTrue(os.path.exists(destination_path))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_ln(self):
        try:
            original_filename = 'test_ln'
            destination_filename = 'test_ln_destination'
            sandbox = tempfile.mkdtemp()
            original_path = '/'.join([sandbox, original_filename])
            destination_path = '/'.join([sandbox, destination_filename])
            touch(original_path)
            result = yield self.client.ln(destination_path, original_path)

            self.assertEquals(result[0], 'symlink succeeded')
            self.assertTrue(os.path.isfile(original_path))
            self.assertTrue(os.path.islink(destination_path))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_rm(self):
        try:
            original_filename = 'test_rm_file'
            sandbox = tempfile.mkdtemp()
            original_path = '/'.join([sandbox, original_filename])
            touch(original_path)

            result = yield self.client.rm(original_path)

            self.assertEquals(result[0], 'remove succeeded')
            self.assertFalse(os.path.exists(original_path))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_put(self):
        try:
            source_data = 'This was my sourcefile...'
            source_filename = 'test_source_file'
            destination_filename = 'test_destination_file'

            source_sandbox = tempfile.mkdtemp()
            destination_sandbox = tempfile.mkdtemp()

            source_path = '/'.join([source_sandbox, source_filename])
            destination_path = '/'.join([destination_sandbox,
                                         destination_filename])
            open(source_path, 'w').write(source_data)

            result = yield self.client.put(source_path, destination_path)
            self.assertTrue(os.path.isfile(source_path))
            self.assertTrue(os.path.isfile(destination_path))
            self.assertEqual(source_data,
                             open(destination_path, 'r').read())
            defer.returnValue(result)
        finally:
            shutil.rmtree(source_sandbox)
            shutil.rmtree(destination_sandbox)

    @defer.inlineCallbacks
    def test_get(self):
        try:
            source_data = 'This was my sourcefile...'
            source_filename = 'test_source_file'
            destination_filename = 'test_destination_file'

            source_sandbox = tempfile.mkdtemp()
            destination_sandbox = tempfile.mkdtemp()

            source_path = '/'.join([source_sandbox, source_filename])
            destination_path = '/'.join([destination_sandbox,
                                         destination_filename])
            open(source_path, 'w').write(source_data)

            result = yield self.client.get(source_path, destination_path)
            self.assertTrue(os.path.isfile(source_path))
            self.assertTrue(os.path.isfile(destination_path))
            self.assertEqual(source_data,
                             open(destination_path, 'r').read())
            defer.returnValue(result)
        finally:
            shutil.rmtree(source_sandbox)
            shutil.rmtree(destination_sandbox)

    @defer.inlineCallbacks
    def test_chown(self):
        try:
            chown_filename = 'test_chown_file'
            sandbox = tempfile.mkdtemp()
            chown_path = '/'.join([sandbox, chown_filename])
            touch(chown_path)

            import os
            import pwd
            uid = pwd.getpwuid(os.getuid()).pw_uid
            result = yield self.client.chown(chown_path, uid)

            self.assertEquals(result[0], 'setstat succeeded')
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_chgrp(self):
        try:
            chgrp_filename = 'test_chgrp_file'
            sandbox = tempfile.mkdtemp()
            chgrp_path = '/'.join([sandbox, chgrp_filename])
            touch(chgrp_path)

            import os
            import pwd
            gid = pwd.getpwuid(os.getuid()).pw_gid
            result = yield self.client.chgrp(chgrp_path, gid)

            self.assertEquals(result[0], 'setstat succeeded')
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_chmod(self):
        try:
            chmod_filename = 'test_chmod_file'
            sandbox = tempfile.mkdtemp()
            chmod_path = '/'.join([sandbox, chmod_filename])
            touch(chmod_path)

            result = yield self.client.chmod(chmod_path, '1000')

            self.assertEquals(result[0], 'setstat succeeded')
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)
コード例 #32
0
class IPV4FunctionalBaseTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            'hostname': self.hostname,
            'port': self.portnum,
            'user': self.user,
            'password': self.password,
            'buffersize': 32768
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults(
            [d, client.onConnectionLost, server.onConnectionLost])

    def test_run_command(self):

        d = self.client.run('echo hi')

        def got_hi(data):
            log.debug('Got Data %s' % (data, ))
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output, 'hi\n')
            return data

        d.addCallback(got_hi)
        return d

    @defer.inlineCallbacks
    def test_lsdir(self):
        try:
            test_file = 'test_ls_dir'
            sandbox = tempfile.mkdtemp()
            testfile = '/'.join([sandbox, test_file])
            touch(testfile)
            d = yield self.client.ls(sandbox)

            self.assertEquals(d[0][0], test_file)
            defer.returnValue(d)
        finally:
            shutil.rmtree(sandbox)

    def test_lsdir_no_dir(self):
        d = self.client.ls('/_not_real')
        return self.assertFailure(d, SFTPError)

    @defer.inlineCallbacks
    def test_mkdir(self):
        try:
            sandbox = tempfile.mkdtemp()
            test_dir = 'tmpMkdir'
            directory = '/'.join([sandbox, test_dir])
            result = yield self.client.mkdir(directory)
            self.assertEquals(result[0], 'mkdir succeeded')
            self.assertTrue(os.path.isdir(directory))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_rmdir(self):
        try:
            sandbox = tempfile.mkdtemp()
            test_dir = 'tmpRmdir'
            directory = '/'.join([sandbox, test_dir])
            os.mkdir(directory)
            result = yield self.client.rmdir(directory)
            self.assertEquals(result[0], 'rmdir succeeded')
            self.assertFalse(os.path.exists(directory))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_rename(self):
        try:
            original_filename = 'test_rename'
            destination_filename = 'test_rename_changed'
            sandbox = tempfile.mkdtemp()
            original_path = '/'.join([sandbox, original_filename])
            destination_path = '/'.join([sandbox, destination_filename])
            touch(original_path)

            result = yield self.client.rename(original_path, destination_path)
            self.assertEquals(result[0], 'rename succeeded')
            self.assertFalse(os.path.exists(original_path))
            self.assertTrue(os.path.exists(destination_path))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_ln(self):
        try:
            original_filename = 'test_ln'
            destination_filename = 'test_ln_destination'
            sandbox = tempfile.mkdtemp()
            original_path = '/'.join([sandbox, original_filename])
            destination_path = '/'.join([sandbox, destination_filename])
            touch(original_path)
            result = yield self.client.ln(destination_path, original_path)

            self.assertEquals(result[0], 'symlink succeeded')
            self.assertTrue(os.path.isfile(original_path))
            self.assertTrue(os.path.islink(destination_path))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_rm(self):
        try:
            original_filename = 'test_rm_file'
            sandbox = tempfile.mkdtemp()
            original_path = '/'.join([sandbox, original_filename])
            touch(original_path)

            result = yield self.client.rm(original_path)

            self.assertEquals(result[0], 'remove succeeded')
            self.assertFalse(os.path.exists(original_path))
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_put(self):
        try:
            source_data = 'This was my sourcefile...'
            source_filename = 'test_source_file'
            destination_filename = 'test_destination_file'

            source_sandbox = tempfile.mkdtemp()
            destination_sandbox = tempfile.mkdtemp()

            source_path = '/'.join([source_sandbox, source_filename])
            destination_path = '/'.join(
                [destination_sandbox, destination_filename])
            open(source_path, 'w').write(source_data)

            result = yield self.client.put(source_path, destination_path)
            self.assertTrue(os.path.isfile(source_path))
            self.assertTrue(os.path.isfile(destination_path))
            self.assertEqual(source_data, open(destination_path, 'r').read())
            defer.returnValue(result)
        finally:
            shutil.rmtree(source_sandbox)
            shutil.rmtree(destination_sandbox)

    @defer.inlineCallbacks
    def test_get(self):
        try:
            source_data = 'This was my sourcefile...'
            source_filename = 'test_source_file'
            destination_filename = 'test_destination_file'

            source_sandbox = tempfile.mkdtemp()
            destination_sandbox = tempfile.mkdtemp()

            source_path = '/'.join([source_sandbox, source_filename])
            destination_path = '/'.join(
                [destination_sandbox, destination_filename])
            open(source_path, 'w').write(source_data)

            result = yield self.client.get(source_path, destination_path)
            self.assertTrue(os.path.isfile(source_path))
            self.assertTrue(os.path.isfile(destination_path))
            self.assertEqual(source_data, open(destination_path, 'r').read())
            defer.returnValue(result)
        finally:
            shutil.rmtree(source_sandbox)
            shutil.rmtree(destination_sandbox)

    @defer.inlineCallbacks
    def test_chown(self):
        try:
            chown_filename = 'test_chown_file'
            sandbox = tempfile.mkdtemp()
            chown_path = '/'.join([sandbox, chown_filename])
            touch(chown_path)

            import os
            import pwd
            uid = pwd.getpwuid(os.getuid()).pw_uid
            result = yield self.client.chown(chown_path, uid)

            self.assertEquals(result[0], 'setstat succeeded')
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_chgrp(self):
        try:
            chgrp_filename = 'test_chgrp_file'
            sandbox = tempfile.mkdtemp()
            chgrp_path = '/'.join([sandbox, chgrp_filename])
            touch(chgrp_path)

            import os
            import pwd
            gid = pwd.getpwuid(os.getuid()).pw_gid
            result = yield self.client.chgrp(chgrp_path, gid)

            self.assertEquals(result[0], 'setstat succeeded')
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)

    @defer.inlineCallbacks
    def test_chmod(self):
        try:
            chmod_filename = 'test_chmod_file'
            sandbox = tempfile.mkdtemp()
            chmod_path = '/'.join([sandbox, chmod_filename])
            touch(chmod_path)

            result = yield self.client.chmod(chmod_path, '1000')

            self.assertEquals(result[0], 'setstat succeeded')
            defer.returnValue(result)
        finally:
            shutil.rmtree(sandbox)
コード例 #33
0
def configure_server(server, branches):
    client = SSHClient(utils.get_public_ip(server), 'jenkins')
    client.ssh('make file cache directory', 'mkdir -p ~/cache/files')
    client.ssh('make pip cache directory', 'mkdir -p ~/cache/pip')
    client.ssh('install build-essential',
               'sudo DEBIAN_FRONTEND=noninteractive '
               'apt-get --option "Dpkg::Options::=--force-confold"'
               ' --assume-yes install build-essential python-dev '
               'linux-headers-virtual linux-headers-`uname -r`')

    for branch_data in branches:
        if branch_data['debs']:
            client.ssh('cache debs for branch %s' % branch_data['name'],
                       'sudo apt-get -y -d install %s' %
                       ' '.join(branch_data['debs']))

        if branch_data['pips']:
            venv = client.ssh('get temp dir for venv', 'mktemp -d').strip()
            client.ssh('create venv',
                       'virtualenv --no-site-packages %s' % venv)
            client.ssh('cache pips for branch %s' % branch_data['name'],
                       'source %s/bin/activate && '
                       'PIP_DOWNLOAD_CACHE=~/cache/pip pip install %s' %
                       (venv, ' '.join(branch_data['pips'])))
            client.ssh('remove venv', 'rm -fr %s' % venv)

        for url in branch_data['images']:
            fname = url.split('/')[-1]
            try:
                client.ssh('check for %s' % fname,
                           'ls ~/cache/files/%s' % fname)
            except:
                client.ssh('download image %s' % fname,
                    'wget -nv -c %s -O ~/cache/files/%s' % (url, fname))

    client.ssh('clear workspace', 'rm -rf ~/workspace-cache')
    client.ssh('make workspace', 'mkdir -p ~/workspace-cache')
    for project in PROJECTS:
        sp = project.split('/')[0]
        client.ssh('clone %s' % project,
            'cd ~/workspace-cache && '
            'git clone https://review.openstack.org/p/%s' % project)

    script = os.environ.get('DEVSTACK_GATE_CUSTOM_SCRIPT', '')
    if script and os.path.isfile(script):
        bn = os.path.basename(script)
        client.scp(script, '/tmp/%s' % bn)
        client.ssh('run custom script %s' % bn,
            'chmod +x /tmp/%s && sudo /tmp/%s' % (bn, bn))

    client.ssh('sync', 'sync && sleep 5')
コード例 #34
0
class IPV4FunctionalReconnectionTestCase(TestCase):
    def setUp(self):
        self.hostname = '127.0.0.1'
        self.user = getpass.getuser()
        self.password = '******'
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            'hostname': self.hostname,
            'port': self.portnum,
            'user': self.user,
            'password': self.password,
            'buffersize': 32768
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug('tearing down')
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults(
            [d, client.onConnectionLost, server.onConnectionLost])

    def test_run_command(self):
        def server_stop_listening(data):
            sld = self.port.stopListening()
            return sld

        def server_drop_connections(data):
            port, self.port = self.port, None
            server, self.server = self.server, None
            server.protocol.transport.loseConnection()
            return server.onConnectionLost

        def run_command(sld):
            results = self.client.run('echo hi')
            return results

        def test_failure(data):
            return self.assertFailure(data, ConnectionLost)

        def test_success(data):
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output, 'hi\n')
            return data

        def start_server(data):
            self.server = SSHServer()
            self.server.protocol = ServerProtocol
            self.port = reactor.listenTCP(self.portnum,
                                          self.server,
                                          interface=self.hostname)
            return self.port

        d = self.client.run('echo hi')
        d.addBoth(test_success)
        d.addCallback(server_stop_listening)
        d.addCallback(server_drop_connections)
        d.addCallback(run_command)
        d.addBoth(test_failure)
        d.addBoth(start_server)
        d.addCallback(run_command)
        d.addBoth(test_success)
        return d
コード例 #35
0
class IPV4FunctionalReconnectionTestCase(TestCase):
    def setUp(self):
        self.hostname = "127.0.0.1"
        self.user = getpass.getuser()
        self.password = "******"
        self.server = SSHServer()
        self.server.protocol = ServerProtocol

        self.port = reactor.listenTCP(0, self.server, interface=self.hostname)
        self.portnum = self.port.getHost().port

        options = {
            "hostname": self.hostname,
            "port": self.portnum,
            "user": self.user,
            "password": self.password,
            "buffersize": 32768,
        }

        self.client = SSHClient(options)
        self.client.protocol = ClientProtocol
        self.client.connect()

    def tearDown(self):
        # Shut down the server and client
        log.debug("tearing down")
        port, self.port = self.port, None
        client, self.client = self.client, None
        server, self.server = self.server, None

        # A Deferred for the server listening port
        d = port.stopListening()

        # Tell the client to disconnect and not retry.
        client.disconnect()

        return defer.gatherResults([d, client.onConnectionLost, server.onConnectionLost])

    def test_run_command(self):
        def server_stop_listening(data):
            sld = self.port.stopListening()
            return sld

        def server_drop_connections(data):
            port, self.port = self.port, None
            server, self.server = self.server, None
            server.protocol.transport.loseConnection()
            return server.onConnectionLost

        def run_command(sld):
            results = self.client.run("echo hi")
            return results

        def test_failure(data):
            return self.assertFailure(data, ConnectionLost)

        def test_success(data):
            self.assertEqual(data.exitCode, 0)
            self.assertEqual(data.output, "hi\n")
            return data

        def start_server(data):
            self.server = SSHServer()
            self.server.protocol = ServerProtocol
            self.port = reactor.listenTCP(self.portnum, self.server, interface=self.hostname)
            return self.port

        d = self.client.run("echo hi")
        d.addBoth(test_success)
        d.addCallback(server_stop_listening)
        d.addCallback(server_drop_connections)
        d.addCallback(run_command)
        d.addBoth(test_failure)
        d.addBoth(start_server)
        d.addCallback(run_command)
        d.addBoth(test_success)
        return d
コード例 #36
0
    def collect(self, device, log):
        manageIp = str(device.manageIp)

        log.info('Connecting to ssh://%s@%s:%d' % (
            device.zCommandUsername,
            manageIp,
            device.zCommandPort
            ))

        self.client = SSHClient({
            'hostname': manageIp,
            'port': device.zCommandPort,
            'user': device.zCommandUsername,
            'password': device.zCommandPassword,
            'buffersize': 32768})
        self.client.connect()
        self.timeout = device.zCommandCommandTimeout

        data = {}
        required_files = ['neutron.conf']
        optional_files = ['plugins/ml2/ml2_conf.ini']
        plugin_names = set()

        try:
            # Check if neutron-server runs on this machine
            d = yield self.client.run("pgrep neutron-server", timeout=self.timeout)
            if d.exitCode != 0:
                # neutron isn't running on this host, so its config
                # files are suspect, and should be ignored.
                log.info("neutron-server not running on host- not collecting ini files")
                defer.returnValue(data)
                return

            # Collect ini files
            for filename in required_files:
                if filename not in data:
                    data[filename] = yield self.read_ini(device, filename, required=True)
            for filename in optional_files:
                if filename not in data:
                    data[filename] = yield self.read_ini(device, filename)

            required_files = []
            optional_files = []

            if data['neutron.conf']:
                ini = data['neutron.conf']
                neutron_core_plugin = self.ini_get(device, filename, ini, 'DEFAULT', 'core_plugin', required=True)
                plugin_names.add(neutron_core_plugin)

            if 'plugins/ml2/ml2_conf.ini' in data:
                mechanism_drivers = split_list(self.ini_get(
                    device,
                    filename,
                    data['plugins/ml2/ml2_conf.ini'],
                    'ml2',
                    'mechanism_drivers',
                    required=True) or '')

                for mechanism_driver in mechanism_drivers:
                    plugin_names.add("ml2." + mechanism_driver)

            data['plugin_names'] = set()
            for plugin_name in plugin_names:
                plugin = zope.component.queryUtility(INeutronImplementationPlugin, plugin_name)
                if not plugin:
                    continue

                plugin_class = plugin.__class__.__name__
                log.info("Checking for additinal ini requirements in neutron implementation plugin '%s': %s" % (plugin_name, plugin_class))
                data['plugin_names'].add(plugin_name)

                for filename, section, option in plugin.ini_required():
                    required_files.append(filename)

                for filename, section, option in plugin.ini_optional():
                    optional_files.append(filename)

            for filename in required_files:
                if filename not in data:
                    data[filename] = yield self.read_ini(device, filename, required=True)
            for filename in optional_files:
                if filename not in data:
                    data[filename] = yield self.read_ini(device, filename)

        except Exception:
            raise
        finally:
            self.client.disconnect()

        defer.returnValue(data)
コード例 #37
0
def configure_server(server, branches):
    client = SSHClient(utils.get_public_ip(server), 'jenkins')
    client.ssh('make file cache directory', 'mkdir -p ~/cache/files')
    client.ssh('make pip cache directory', 'mkdir -p ~/cache/pip')
    client.ssh(
        'install build-essential', 'sudo DEBIAN_FRONTEND=noninteractive '
        'apt-get --option "Dpkg::Options::=--force-confold"'
        ' --assume-yes install build-essential python-dev '
        'linux-headers-virtual linux-headers-`uname -r`')

    for branch_data in branches:
        if branch_data['debs']:
            client.ssh(
                'cache debs for branch %s' % branch_data['name'],
                'sudo apt-get -y -d install %s' %
                ' '.join(branch_data['debs']))

        if branch_data['pips']:
            venv = client.ssh('get temp dir for venv', 'mktemp -d').strip()
            client.ssh('create venv',
                       'virtualenv --no-site-packages %s' % venv)
            client.ssh(
                'cache pips for branch %s' % branch_data['name'],
                'source %s/bin/activate && '
                'PIP_DOWNLOAD_CACHE=~/cache/pip pip install %s' %
                (venv, ' '.join(branch_data['pips'])))
            client.ssh('remove venv', 'rm -fr %s' % venv)

        for url in branch_data['images']:
            fname = url.split('/')[-1]
            try:
                client.ssh('check for %s' % fname,
                           'ls ~/cache/files/%s' % fname)
            except:
                client.ssh('download image %s' % fname,
                           'wget -c %s -O ~/cache/files/%s' % (url, fname))

    client.ssh('clear workspace', 'rm -rf ~/workspace-cache')
    client.ssh('make workspace', 'mkdir -p ~/workspace-cache')
    for project in PROJECTS:
        sp = project.split('/')[0]
        client.ssh(
            'clone %s' % project, 'cd ~/workspace-cache && '
            'git clone https://review.openstack.org/p/%s' % project)

    script = os.environ.get('DEVSTACK_GATE_CUSTOM_SCRIPT', '')
    if script and os.path.isfile(script):
        bn = os.path.basename(script)
        client.scp(script, '/tmp/%s' % bn)
        client.ssh('run custom script %s' % bn,
                   'chmod +x /tmp/%s && /tmp/%s' % (bn, bn))

    client.ssh('sync', 'sync && sleep 5')
コード例 #38
0
ファイル: pod.py プロジェクト: shreyasbs123/samplevnf
    def __init__(self, name, namespace="default", logger_name="k8srapid"):
        self._log = logging.getLogger(logger_name)

        self._name = name
        self._namespace = namespace
        self._ssh_client = SSHClient(logger_name=logger_name)
コード例 #39
0
ファイル: remotessh.py プロジェクト: pradeeppanayal/pat
#
#Author Pradeep CH
#
__author__ = 'Pradeep CH'
__version__ = '1.0.0'

# Create instance of FieldStorage
form = cgi.FieldStorage()
clientIp = cgi.escape(os.environ["REMOTE_ADDR"])

bodyContent = ''  #html.getBackButton('/pat/ssh/remotessh.htm')

# Get mode from fields
un = form.getvalue('username')
ip = form.getvalue('ip')

if not ip or not un:
    bodyContent += 'IP and Username required.'
else:
    bodyContent += 'Your machine IP is %s <br>' % (str(clientIp))
    try:
        cl = SSHClient(clientIp)
        cl.enableSSH(ip, un)
        bodyContent += 'SSH successfull'
    except Exception as e:
        bodyContent += 'Could not connect to remote machine. Make sure the PAT plugin is running in your machine, Cause : %s' % str(
            e)
#print html
html.printHeader('Authetication Validation')
html.printBodyContent(bodyContent)
コード例 #40
0
def configure_server(server, branches):
    client = SSHClient(utils.get_public_ip(server), "jenkins")
    client.ssh("make file cache directory", "mkdir -p ~/cache/files")
    client.ssh("make pip cache directory", "mkdir -p ~/cache/pip")
    client.ssh(
        "install build-essential",
        "sudo DEBIAN_FRONTEND=noninteractive "
        'apt-get --option "Dpkg::Options::=--force-confold"'
        " --assume-yes install build-essential python-dev "
        "linux-headers-virtual linux-headers-`uname -r`",
    )

    for branch_data in branches:
        if branch_data["debs"]:
            client.ssh(
                "cache debs for branch %s" % branch_data["name"],
                "sudo apt-get -y -d install %s" % " ".join(branch_data["debs"]),
            )

        if branch_data["pips"]:
            venv = client.ssh("get temp dir for venv", "mktemp -d").strip()
            client.ssh("create venv", "virtualenv --no-site-packages %s" % venv)
            client.ssh(
                "cache pips for branch %s" % branch_data["name"],
                "source %s/bin/activate && "
                "PIP_DOWNLOAD_CACHE=~/cache/pip pip install %s" % (venv, " ".join(branch_data["pips"])),
            )
            client.ssh("remove venv", "rm -fr %s" % venv)

        for url in branch_data["images"]:
            fname = url.split("/")[-1]
            try:
                client.ssh("check for %s" % fname, "ls ~/cache/files/%s" % fname)
            except:
                client.ssh("download image %s" % fname, "wget -nv -c %s -O ~/cache/files/%s" % (url, fname))

    client.ssh("clear workspace", "rm -rf ~/workspace-cache")
    client.ssh("make workspace", "mkdir -p ~/workspace-cache")
    for project in PROJECTS:
        client.ssh(
            "clone %s" % project, "cd ~/workspace-cache && " "git clone https://review.openstack.org/p/%s" % project
        )

    script = os.environ.get("DEVSTACK_GATE_CUSTOM_SCRIPT", "")
    if script and os.path.isfile(script):
        bn = os.path.basename(script)
        client.scp(script, "/tmp/%s" % bn)
        client.ssh("run custom script %s" % bn, "chmod +x /tmp/%s && sudo /tmp/%s" % (bn, bn))

    client.ssh("sync", "sync && sleep 5")
コード例 #41
0
    def collect(self, device, log):
        manageIp = str(device.manageIp)

        log.info('Connecting to ssh://%s@%s:%d' % (
            device.zCommandUsername,
            manageIp,
            device.zCommandPort
            ))

        client = SSHClient({
            'hostname': manageIp,
            'port': device.zCommandPort,
            'user': device.zCommandUsername,
            'password': device.zCommandPassword,
            'buffersize': 32768})
        client.connect()
        timeout = device.zCommandCommandTimeout
        data = {}

        try:
            for instanceId, instanceUUID in device.openstack_instanceList:
                cmd = "virsh --readonly -c 'qemu:///system' dumpxml '%s'" % instanceUUID
                log.info("Running %s" % cmd)
                d = yield client.run(cmd, timeout=timeout)

                if d.exitCode != 0 or d.stderr:
                    if 'Domain not found' in d.stderr:
                        log.debug("Domain not found while running virsh (rc=%s, stderr='%s')" % (d.exitCode, d.stderr))
                    else:
                        log.error("Error running virsh (rc=%s, stderr='%s')" % (d.exitCode, d.stderr))
                    continue

                try:
                    tree = etree.fromstring(d.output)

                    instanceName = str(tree.xpath("/domain/name/text()")[0])
                    zenossInstanceId = 'server-%s' % (instanceUUID)
                    data[instanceUUID] = {
                        'id': zenossInstanceId,
                        'serialNumber': str(tree.xpath("/domain/sysinfo/system/entry[@name='serial']/text()")[0]),
                        'biosUuid': str(tree.xpath("/domain/sysinfo/system/entry[@name='uuid']/text()")[0])
                    }

                except Exception:
                    log.error("Invalid XML Received from (%s):\n%s\n\n" % (cmd, d.output))
                    raise LibvirtXMLError('Incomplete or invalid XML returned from virsh command. Consult log for more details.')

                vnics = []
                for interface in tree.xpath("/domain/devices/interface"):
                    target = interface.find("target/[@dev]")
                    mac = interface.find("mac/[@address]")

                    if target is None or mac is None:
                        # unrecognized interface type
                        continue

                    # compute the resourceId in the same way that ceilometer's
                    # net pollster does.
                    vnicName = str(target.get('dev'))
                    zenossVnicId = 'vnic-%s-%s' % (instanceUUID, vnicName)
                    ceilometerResourceId = '%s-%s-%s' % (instanceName, instanceUUID, vnicName)

                    vnics.append({
                        'id': zenossVnicId,
                        'name': vnicName,
                        'macaddress': str(mac.get('address')),
                        'resourceId': ceilometerResourceId
                    })
                data[instanceUUID]['vnics'] = vnics

        finally:
            client.disconnect()

        returnValue(data)
コード例 #42
0
from sshclient import SSHClient

import logging

logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig(level=logging.INFO)
from twisted.python import log as twistedlog

observer = twistedlog.PythonLoggingObserver()
observer.start()
log = logging.getLogger("txsshclient.client")

options = {"hostname": "127.0.0.1", "port": 2222, "user": "******", "password": "******", "buffersize": 32768}

client = SSHClient(options)
client.connect()


def retry():
    log.debug("retrying")
    d = client.run("sleep 5 && ls")

    def failed_or_success(result):
        if isinstance(result, failure.Failure):
            log.info("Failed %s" % (result,))
        else:
            log.info("Success %s" % (result,))

    d.addBoth(failed_or_success)
コード例 #43
0
class inifiles(PythonPlugin):

    _yaml_config = None
    _eventService = None

    deviceProperties = PythonPlugin.deviceProperties \
        + ('zCommandUsername', 'zCommandPassword',
           'zCommandPort', 'zCommandCommandTimeout',
           'zOpenStackNeutronConfigDir')

    def sendEvent(self, evt):
        if not self._eventService:
            self._eventService = zope.component.queryUtility(IEventService)
        self._eventService.sendEvent(evt)

    def sendFileClearEvent(self, device, filename):
        evt = dict(
            device=device.id,
            component='',
            summary="File %s was loaded successfully" % filename,
            severity=Event.Clear,
            eventClassKey='openStackIniFileAccess',
            eventKey=filename
        )
        self.sendEvent(evt)

    def sendFileErrorEvent(self, device, filename, errmsg):
        evt = dict(
            device=device.id,
            component='',
            summary="File %s could not be accessed: %s" % (filename, errmsg),
            severity=Event.Error,
            eventClassKey='openStackIniFileAccess',
            eventKey=filename
        )
        self.sendEvent(evt)

    def sendOptionClearEvent(self, device, filename, section, option):
        evt = dict(
            device=device.id,
            component='',
            summary="%s: Required option [%s] %s was loaded successfully" % (filename, section, option),
            severity=Event.Clear,
            eventClassKey='openStackIniFileOptionParsing',
            eventKey="%s/%s/%s" % (filename, section, option)
        )
        self.sendEvent(evt)

    def sendOptionErrorEvent(self, device, filename, section, option):
        evt = dict(
            device=device.id,
            component='',
            summary="%s: Required option [%s] %s was not found" % (filename, section, option),
            severity=Event.Error,
            eventClassKey='openStackIniFileOptionParsing',
            eventKey="%s/%s/%s" % (filename, section, option)
        )
        self.sendEvent(evt)

    def ini_get(self, device, filename, ini, section, option, required=False):
        try:
            return ini.get(section, option)
            if required:
                self.sendOptionClearEvent(device, filename, section, option)
        except ConfigParser.NoOptionError:
            if required:
                self.sendOptionErrorEvent(device, filename, section, option)
            return None

    @defer.inlineCallbacks
    def read_ini(self, device, filename, required=False):
        filepath = os.path.join(device.zOpenStackNeutronConfigDir, filename)
        log.info("Retrieving %s", filepath)

        cmd = "cat %s" % filepath
        d = yield self.client.run(cmd, timeout=self.timeout)

        if d.exitCode != 0 or d.stderr:
            if required:
                log.error("Unable to access required file %s (%s)" % (filepath, d.stderr.strip()))
                self.sendFileErrorEvent(device, filepath, d.stderr)
            else:
                log.info("Unable to access optional file %s (%s)" % (filepath, d.stderr.strip()))

            defer.returnValue(None)
            return

        self.sendFileClearEvent(device, filepath)

        ini = ConfigParser.RawConfigParser(allow_no_value=True)
        ini.readfp(io.BytesIO(d.output))
        defer.returnValue(ini)

        return

    @defer.inlineCallbacks
    def collect(self, device, log):
        manageIp = str(device.manageIp)

        log.info('Connecting to ssh://%s@%s:%d' % (
            device.zCommandUsername,
            manageIp,
            device.zCommandPort
            ))

        self.client = SSHClient({
            'hostname': manageIp,
            'port': device.zCommandPort,
            'user': device.zCommandUsername,
            'password': device.zCommandPassword,
            'buffersize': 32768})
        self.client.connect()
        self.timeout = device.zCommandCommandTimeout

        data = {}
        required_files = ['neutron.conf']
        optional_files = ['plugins/ml2/ml2_conf.ini']
        plugin_names = set()

        try:
            # Check if neutron-server runs on this machine
            d = yield self.client.run("pgrep neutron-server", timeout=self.timeout)
            if d.exitCode != 0:
                # neutron isn't running on this host, so its config
                # files are suspect, and should be ignored.
                log.info("neutron-server not running on host- not collecting ini files")
                defer.returnValue(data)
                return

            # Collect ini files
            for filename in required_files:
                if filename not in data:
                    data[filename] = yield self.read_ini(device, filename, required=True)
            for filename in optional_files:
                if filename not in data:
                    data[filename] = yield self.read_ini(device, filename)

            required_files = []
            optional_files = []

            if data['neutron.conf']:
                ini = data['neutron.conf']
                neutron_core_plugin = self.ini_get(device, filename, ini, 'DEFAULT', 'core_plugin', required=True)
                plugin_names.add(neutron_core_plugin)

            if 'plugins/ml2/ml2_conf.ini' in data:
                mechanism_drivers = split_list(self.ini_get(
                    device,
                    filename,
                    data['plugins/ml2/ml2_conf.ini'],
                    'ml2',
                    'mechanism_drivers',
                    required=True) or '')

                for mechanism_driver in mechanism_drivers:
                    plugin_names.add("ml2." + mechanism_driver)

            data['plugin_names'] = set()
            for plugin_name in plugin_names:
                plugin = zope.component.queryUtility(INeutronImplementationPlugin, plugin_name)
                if not plugin:
                    continue

                plugin_class = plugin.__class__.__name__
                log.info("Checking for additinal ini requirements in neutron implementation plugin '%s': %s" % (plugin_name, plugin_class))
                data['plugin_names'].add(plugin_name)

                for filename, section, option in plugin.ini_required():
                    required_files.append(filename)

                for filename, section, option in plugin.ini_optional():
                    optional_files.append(filename)

            for filename in required_files:
                if filename not in data:
                    data[filename] = yield self.read_ini(device, filename, required=True)
            for filename in optional_files:
                if filename not in data:
                    data[filename] = yield self.read_ini(device, filename)

        except Exception:
            raise
        finally:
            self.client.disconnect()

        defer.returnValue(data)

    def process(self, device, results, log):
        log.info("Modeler %s processing data for device %s",
                 self.name(), device.id)

        if 'neutron.conf' not in results:
            log.info("No neutron ini files to process.")
            return

        data = {
            'neutron_core_plugin': None,
            'neutron_mechanism_drivers': [],
            'neutron_type_drivers': [],
            'set_neutron_ini': {}
        }

        if 'plugin_names' not in results:
            log.error("No neutron implementation plugins were identified, unable to continue.")
            return

        if results['neutron.conf']:
            filename = 'neutron.conf'
            ini = results[filename]
            data['neutron_core_plugin'] = self.ini_get(device, filename, ini, 'DEFAULT', 'core_plugin', required=True)

        if data['neutron_core_plugin']:
            if data['neutron_core_plugin'] in ('neutron.plugins.ml2.plugin.Ml2Plugin', 'ml2'):
                filename = 'plugins/ml2/ml2_conf.ini'
                ini = results[filename]
                if ini:
                    data['neutron_type_drivers'] = split_list(self.ini_get(device, filename, ini, 'ml2', 'type_drivers', required=True))
                    data['neutron_mechanism_drivers'] = split_list(self.ini_get(device, filename, ini, 'ml2', 'mechanism_drivers', required=True))

        for plugin_name in results['plugin_names']:
            # See if we have any plugins registered for the core module
            # (if not ML2) or mechanism type (if ML2)
            plugin = zope.component.queryUtility(INeutronImplementationPlugin, plugin_name)
            if not plugin:
                continue

            log.debug("(Process) Using plugin '%s'" % plugin_name)
            for filename, section, option in plugin.ini_required():
                ini = results.get(filename, None)
                if ini:
                    data['set_neutron_ini'][(filename, section, option)] = self.ini_get(device, filename, ini, section, option, required=True)

            for filename, section, option in plugin.ini_optional():
                ini = results.get(filename, None)
                if ini:
                    data['set_neutron_ini'][(filename, section, option)] = self.ini_get(device, filename, ini, section, option)

            return ObjectMap({'setApplyDataMapToOpenStackInfrastructureEndpoint': ObjectMap(data)})