Esempio n. 1
0
 def test_set_timeout(self):
     session = Session()
     self.assertEqual(session.options_set(options.TIMEOUT, "1000"), 0)
     self.assertEqual(session.options_set(options.TIMEOUT_USEC, "1000"), 0)
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((self.host, self.port))
     session = Session()
     session.options_set(options.USER, self.user)
     session.options_set(options.HOST, self.host)
     session.options_set_port(self.port)
     self.assertEqual(session.set_socket(sock), 0)
     self.assertEqual(session.options_set(options.TIMEOUT, "1000"), 0)
     self.assertEqual(session.options_set(options.TIMEOUT_USEC, "1000"), 0)
Esempio n. 2
0
 def _init_session(self, retries=1):
     logger.debug("Starting new session for %s@%s:%s", self.user, self.host,
                  self.port)
     self.session = Session()
     self.session.options_set(options.USER, self.user)
     self.session.options_set(options.HOST, self.host)
     self.session.options_set_port(self.port)
     if self.gssapi_server_identity:
         self.session.options_set(options.GSSAPI_SERVER_IDENTITY,
                                  self.gssapi_server_identity)
     if self.gssapi_client_identity:
         self.session.options_set(options.GSSAPI_CLIENT_IDENTITY,
                                  self.gssapi_client_identity)
     if self.gssapi_client_identity or self.gssapi_server_identity:
         self.session.options_set_gssapi_delegate_credentials(
             self.gssapi_delegate_credentials)
     self.session.set_socket(self.sock)
     logger.debug("Session started, connecting with existing socket")
     try:
         self._session_connect()
     except Exception as ex:
         if retries < self.num_retries:
             return self._connect_init_session_retry(retries=retries + 1)
         msg = "Error connecting to host %s:%s - %s"
         logger.error(msg, self.host, self.port, ex)
         ex.host = self.host
         ex.port = self.port
         raise ex
Esempio n. 3
0
    def __init__(self, host, username, port, key_file=None, debug=False):
        """Initialize the connection manager.

        :param host: IP or Domain to connect to.
        :type host: String
        :param username: Username for the connection.
        :type username: String
        :param port: Port number used to connect to the remote server.
        :type port: Int
        :param key_file: SSH key file used to connect.
        :type key_file: String
        :param debug: Enable or disable debug mode
        :type debug: Boolean
        """

        self.log = logger.getLogger(name="directord-ssh", debug_logging=debug)
        self.key_file = key_file
        self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.sock.connect((host, port))

        self.session = Session()
        self.session.options_set(options.HOST, host)
        self.session.options_set(options.USER, username)
        self.session.options_set_port(port)
        self.session.set_socket(self.sock)
        self.session.connect()

        self.log.debug("Handshake with [ %s ] on port [ %s ] complete.", host,
                       port)

        self.channels = dict()
        self.host = host
        self.username = username
        self.key_file = key_file
Esempio n. 4
0
 def test_socket_connect(self):
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((self.host, self.port))
     session = Session()
     session.options_set(options.USER, self.user)
     session.options_set(options.HOST, self.host)
     session.options_set_port(self.port)
     self.assertEqual(session.set_socket(sock), 0)
     self.assertEqual(session.connect(), 0)
     self.assertRaises(RequestDenied, session.userauth_none)
     self.assertEqual(session.userauth_publickey(self.pkey), 0)
Esempio n. 5
0
def go():
    while True:
        try:
            s = Session()
            s.options_set(options.HOST, HOST)
            s.connect()
            sleep(2147483647)
        except Exception as e:
            global count
            print(str(e) + ': died #' + str(count))
            if 'Connection reset' in str(e):
                break
            count += 1
            pass
Esempio n. 6
0
 def setUp(self):
     self.host = '127.0.0.1'
     self.port = 2222
     self.cmd = 'echo me'
     self.resp = u'me'
     self.user_key = PKEY_FILENAME
     self.user_pub_key = PUB_FILE
     self.user = pwd.getpwuid(os.geteuid()).pw_name
     # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     # sock.connect((self.host, self.port))
     # self.sock = sock
     self.session = Session()
     self.session.options_set(options.HOST, self.host)
     self.session.options_set_port(self.port)
     self.session.options_set(options.USER, self.user)
     self.pkey = import_privkey_file(self.user_key)
Esempio n. 7
0
 def setUp(self):
     self.host = '127.0.0.1'
     self.port = 2222
     self.cmd = 'echo me'
     self.resp = u'me'
     self.user_key = PKEY_FILENAME
     self.user_pub_key = PUB_FILE
     self.user_ca_key = USER_CERT_PRIV_KEY
     self.user_cert_file = USER_CERT_FILE
     self.user = USER
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((self.host, self.port))
     self.sock = sock
     self.session = Session()
     self.session.options_set(options.HOST, self.host)
     self.session.options_set_port(self.port)
     self.session.options_set(options.USER, self.user)
     self.session.set_socket(sock)
     self.pkey = import_privkey_file(self.user_key)
Esempio n. 8
0
 def test_non_blocking_connect(self):
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.connect((self.host, self.port))
     session = Session()
     session.options_set(options.USER, self.user)
     session.options_set(options.HOST, self.host)
     session.options_set_port(self.port)
     self.assertEqual(session.set_socket(sock), 0)
     session.set_blocking(0)
     rc = session.connect()
     while rc == SSH_AGAIN:
         wait_socket(session, sock)
         rc = session.connect()
     self.assertEqual(rc, 0)
     rc = session.userauth_publickey(self.pkey)
     while rc == SSH_AUTH_AGAIN:
         wait_socket(session, sock)
         rc = session.userauth_publickey(self.pkey)
     self.assertEqual(rc, 0)
Esempio n. 9
0
 def _init(self, retries=1):
     logger.debug("Starting new session for %s@%s:%s", self.user, self.host,
                  self.port)
     self.session = Session()
     self.session.options_set(options.USER, self.user)
     self.session.options_set(options.HOST, self.host)
     self.session.options_set_port(self.port)
     if self.gssapi_server_identity:
         self.session.options_set(options.GSSAPI_SERVER_IDENTITY,
                                  self.gssapi_server_identity)
     if self.gssapi_client_identity:
         self.session.options_set(options.GSSAPI_CLIENT_IDENTITY,
                                  self.gssapi_client_identity)
     if self.gssapi_client_identity or self.gssapi_server_identity:
         self.session.options_set_gssapi_delegate_credentials(
             self.gssapi_delegate_credentials)
     self.session.set_socket(self.sock)
     logger.debug("Session started, connecting with existing socket")
     try:
         self.session.connect()
     except Exception as ex:
         while retries < self.num_retries:
             return self._connect_init_retry(retries)
         msg = "Error connecting to host %s:%s - %s"
         logger.error(msg, self.host, self.port, ex)
         ex.host = self.host
         ex.port = self.port
         raise ex
     try:
         self.auth()
     except Exception as ex:
         while retries < self.num_retries:
             return self._connect_init_retry(retries)
         msg = "Authentication error while connecting to %s:%s - %s"
         ex = AuthenticationException(msg, self.host, self.port, ex)
         ex.host = self.host
         ex.port = self.port
         raise ex
     logger.debug("Authentication completed successfully - "
                  "setting session to non-blocking mode")
     self.session.set_blocking(0)
Esempio n. 10
0
 def test_should_not_segfault(self):
     session = Session()
     self.assertEqual(session.get_error(), '')
     self.assertRaises(InvalidAPIUse, session.userauth_none)
     self.assertRaises(InvalidAPIUse, session.userauth_publickey, self.pkey)
     key = import_pubkey_file(self.user_pub_key)
     self.assertRaises(InvalidAPIUse, session.userauth_try_publickey, key)
     self.assertRaises(InvalidAPIUse, session.userauth_publickey_auto, '')
     self.assertRaises(InvalidAPIUse, session.channel_new)
     self.assertRaises(InvalidAPIUse, session.get_disconnect_message)
     self.assertRaises(InvalidAPIUse, session.get_issue_banner)
     self.assertRaises(InvalidAPIUse, session.get_openssh_version)
     self.assertIsNone(session.dump_knownhost())
     self.assertIsNone(session.get_clientbanner())
     self.assertIsNone(session.get_serverbanner())
     self.assertIsNone(session.get_kex_algo())
     self.assertIsNone(session.get_cipher_in())
     self.assertIsNone(session.get_cipher_out())
     self.assertIsNone(session.get_hmac_in())
     self.assertIsNone(session.get_hmac_out())
     self.assertIsNotNone(session.get_error_code())
     session.connector_new()
Esempio n. 11
0
import os
import pwd
import socket

from ssh.session import Session
from ssh import options

# Linux only
USERNAME = pwd.getpwuid(os.geteuid()).pw_name
HOST = 'localhost'
PORT = 22

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))

s = Session()
s.options_set(options.HOST, HOST)
s.options_set(options.USER, USERNAME)
s.options_set_port(PORT)
s.set_socket(sock)
s.connect()

# Authenticate with agent
s.userauth_agent(USERNAME)

chan = s.channel_new()
chan.open_session()
chan.request_exec('echo me')
size, data = chan.read()
while size > 0:
    print(data.strip())