Esempio n. 1
0
class TestTelnetExecutorExecute(unittest.TestCase):
    def setUp(self):
        self.executor = TelnetExecutor('127.0.0.1', 'admin', 'sIcretandsecYre')

    def test_ok(self):
        return_code, stdout, stderr = self.executor.execute('pwd')

        self.assertEqual(return_code, 0)
        self.assertEqual(stdout.strip(), '/home/admin')
        self.assertEqual(stderr.strip(), '')

    def test_command_not_found(self):
        return_code, stdout, stderr = self.executor.execute('unknowncommand')

        self.assertEqual(return_code, 127)
        self.assertEqual(stdout.strip(), '')
        self.assertIn('not found', stderr)

    def test_command_failed(self):
        return_code, stdout, stderr = self.executor.execute('which')

        self.assertEqual(return_code, 1)
        self.assertEqual(stdout.strip(), '')
        self.assertEqual(stderr.strip(), '')

    def test_command_with_params(self):
        return_code, stdout, stderr = self.executor.execute(
            'ls', ('-a', '/etc/testing'))

        self.assertEqual(return_code, 0)
        self.assertIn('..', stdout)
        self.assertIn('requirements.txt', stdout)
        self.assertEqual(stderr.strip(), '')
Esempio n. 2
0
    def test_ok(self):
        executor = None
        try:
            executor = TelnetExecutor('127.0.0.1', 'admin', 'sIcretandsecYre')
        except ValueError as err:
            self.fail(f"Conection failed:\n{err}")

        self.assertIsInstance(executor, TelnetExecutor)
        self.assertTrue(executor.tn.get_socket())
Esempio n. 3
0
def telnet(connection_string, command, password, command_args):
    """ Will execute COMMAND via telnet

    Please pass CONNECTION_STRING in the followin format: <username>@<host>

    COMMAND: command to execute

    COMMAND_ARGS: params, passed to COMMAND, please prepend them with "--"\n
    """
    user, _, host = parse_connection_string(connection_string)

    try:
        executor = TelnetExecutor(host, user, password)
    except ValueError as err:
        click.echo(err)
        return None

    res = json_repr(*executor.execute(command, parameters=command_args))
    click.echo(res)
Esempio n. 4
0
 def setUp(self):
     self.executor = TelnetExecutor('127.0.0.1', 'admin', 'sIcretandsecYre')
Esempio n. 5
0
 def test_empty_user(self):
     with self.assertRaises(ValueError):
         TelnetExecutor('127.0.0.1', '', 'IncorrectPassword')
Esempio n. 6
0
 def test_empty_password(self):
     with self.assertRaises(ValueError):
         TelnetExecutor('127.0.0.1', 'admin', '')
Esempio n. 7
0
 def test_wrong_password(self):
     with self.assertRaises(ValueError):
         TelnetExecutor('127.0.0.1', 'admin', 'IncorrectPassword')
Esempio n. 8
0
 def test_wrong_account(self):
     with self.assertRaises(ValueError):
         TelnetExecutor('127.0.0.1', 'nonexistent', 'sIcretandsecYre')
Esempio n. 9
0
 def test_connection_to_non_existent_domain(self):
     with self.assertRaises(ValueError):
         executor = TelnetExecutor('nonexistentdomain.ua', 'admin',
                                   'sIcretandsecYre')