def create_command_channel(self, cmd): """ Create an SSH channel over the existing connection and issue a command. `cmd` should already be encoded as a byte string. Return the `CommandProtocol` instance. """ log = self.log if not self.connected_to_target: raise Exception( "Tried to open channel, but was not connected to the target host." ) ssh_conn = self.ssh_conn endpoint = SSHCommandClientEndpoint.existingConnection(ssh_conn, cmd) proto = CommandProtocol() proto.log = self.log d = connectProtocol(endpoint, proto) def _on_timeout(result, timeout): self.disconnect_from_target() raise Exception( "Timed out while attempting to establish command channel.") d.addTimeout(self.cmd_timeout, self.reactor, onTimeoutCancel=_on_timeout) log.debug("Establishing channel ...") proto = yield d log.debug("Channel established.") returnValue(proto)
def create(self): """ Create and return a new L{SSHCommandClientEndpoint} using the C{existingConnection} constructor. """ factory = Factory() factory.protocol = Protocol connected = self.endpoint.connect(factory) # Please, let me in. This kinda sucks. channelLookup = self.realm.channelLookup.copy() try: self.realm.channelLookup[b'session'] = WorkingExecSession server, client, pump = self.connectedServerAndClient( self.factory, self.reactor.tcpClients[0][2]) finally: self.realm.channelLookup.clear() self.realm.channelLookup.update(channelLookup) self._server = server self._client = client self._pump = pump protocol = self.successResultOf(connected) connection = protocol.transport.conn return SSHCommandClientEndpoint.existingConnection( connection, b"/bin/ls -l")
def create(self): """ Create and return a new L{SSHCommandClientEndpoint} using the C{existingConnection} constructor. """ factory = Factory() factory.protocol = Protocol connected = self.endpoint.connect(factory) # Please, let me in. This kinda sucks. channelLookup = self.realm.channelLookup.copy() try: self.realm.channelLookup[b'session'] = WorkingExecSession server, client, pump = self.connectedServerAndClient( self.factory, self.reactor.tcpClients[0][2]) finally: self.realm.channelLookup.clear() self.realm.channelLookup.update(channelLookup) self._server = server self._client = client self._pump = pump protocol = self.successResultOf(connected) connection = protocol.transport.conn return SSHCommandClientEndpoint.existingConnection( connection, b"/bin/ls -l")
def spawnProcess(self, protocol, command): factory = Factory() factory.protocol = lambda: _CommandProtocol(protocol) e = SSHCommandClientEndpoint.existingConnection( self.master_proto.transport.conn, command) d = e.connect(factory) return d.addCallback(self._commandStarted)
def executeNewCommand(self, line): factory = Factory() factory.protocol = MyProtocol e = SSHCommandClientEndpoint.existingConnection(self.ssh_conn, line.strip()) d = e.connect(factory) d.addCallback(self.protoStarted)
def exec_command(self, command): conn = self.transport.conn factory = protocol.Factory() factory.protocol = SingleCommandProtocol e = SSHCommandClientEndpoint.existingConnection(conn, command) d = e.connect(factory) d.addCallback(lambda p: p.finished) return d
def perform_run(dispatcher, intent): context.bind( message_type="flocker.provision.ssh:run", command=intent.log_command_filter(intent.command), ).write() endpoint = SSHCommandClientEndpoint.existingConnection( connection, intent.command) d = Deferred() connectProtocol(endpoint, CommandProtocol(deferred=d, context=context)) return d
def _spawnProcess(self, protocol, command): vvvv('ACTUALLY SPAWN: %r' % (command,)) factory = Factory() factory.protocol = lambda: _CommandProtocol(protocol) e = SSHCommandClientEndpoint.existingConnection( self.master_proto.transport.conn, command) d = e.connect(factory) vvvv('STARTING') return d.addCallbacks(self._commandStarted, self._commandFailedToStart)
def perform_run(dispatcher, intent): context.bind( message_type="flocker.provision.ssh:run", command=intent.log_command_filter(intent.command), ).write() endpoint = SSHCommandClientEndpoint.existingConnection( connection, intent.command) d = Deferred() connectProtocol(endpoint, CommandProtocol( deferred=d, context=context)) return d
def gotConnection(proto): conn = proto.transport.conn for i in range(50): factory = Factory() factory.protocol = PrinterProtocol factory.done = Deferred() done.append(factory.done) e = SSHCommandClientEndpoint.existingConnection( conn, b"/bin/echo %d" % (i, )) yield e.connect(factory)
def gotConnection(proto): conn = proto.transport.conn for i in range(50): factory = Factory() factory.protocol = PrinterProtocol factory.done = Deferred() done.append(factory.done) e = SSHCommandClientEndpoint.existingConnection( conn, b"/bin/echo %d" % (i,)) yield e.connect(factory)
def fork(self, command, args=(), env={}, path=None, _timeout=3600): """Execute a remote command on the SSH server """ if not self.connection: return defer.maybeDeferred(lambda: (None, "SSH not ready", 255)) if path: env['PATH'] = path if env: env = ' '.join('%s=%s' % (key, val) for key, val in env.items()) + ' ' else: env = '' if args: args = ' ' + ' '.join(args) else: args = '' existing = SSHCommandClientEndpoint.existingConnection( self.connection, (env + command + args).encode()) factory = protocol.Factory() factory.protocol = SSHCommandProtocol factory.done = defer.Deferred() def finished(result): """Command finished """ stdout, stderr, code = result return (stdout.read(), stderr.read(), code) factory.done.addCallback(finished) def connected(connection): """Connection established """ # Be nice if Conch exposed this better... connection.transport.extReceived = connection.extReceived return factory.done return existing.connect(factory).addCallback(connected)
def fork(self, command, args=(), env={}, path=None, timeout=3600): if not self.connection: log.msg("Connection to %s not yet ready" % ( self.hostname,)) return defer.maybeDeferred(lambda: (None, "SSH not ready", 255)) if env: env = ' '.join('%s=%s' % (k, v) for k, v in env.items()) + ' ' else: env = '' if args: args = ' ' + ' '.join(args) else: args = '' e = SSHCommandClientEndpoint.existingConnection(self.connection, (env + command + args).encode()) factory = protocol.Factory() factory.protocol = SSHCommandProtocol factory.done = defer.Deferred() def finished(result): stdout, stderr, code = result return (stdout.read(), stderr.read(), code) factory.done.addCallback(finished) def connected(connection): # Be nice if Conch exposed this better... connection.transport.extReceived = connection.extReceived return factory.done return e.connect(factory).addCallback(connected)