public SshExecChannel OpenExecChannel(
            string command,
            LIBSSH2_CHANNEL_EXTENDED_DATA mode)
        {
            this.session.Handle.CheckCurrentThreadOwnsHandle();
            Utilities.ThrowIfNull(command, nameof(command));

            using (SshTraceSources.Default.TraceMethod().WithParameters(
                       command,
                       mode))
            {
                var channelHandle = OpenChannelInternal(mode);

                //
                // Launch the process.
                //
                var request = "exec";
                var result  = (LIBSSH2_ERROR)UnsafeNativeMethods.libssh2_channel_process_startup(
                    channelHandle,
                    request,
                    (uint)request.Length,
                    command,
                    command == null ? 0 : (uint)command.Length);

                if (result != LIBSSH2_ERROR.NONE)
                {
                    channelHandle.Dispose();
                    throw this.session.CreateException(result);
                }

                return(new SshExecChannel(this.session, channelHandle));
            }
        }
        //---------------------------------------------------------------------
        // Channel.
        //---------------------------------------------------------------------

        private SshChannelHandle OpenChannelInternal(
            LIBSSH2_CHANNEL_EXTENDED_DATA mode)
        {
            this.session.Handle.CheckCurrentThreadOwnsHandle();

            using (SshTraceSources.Default.TraceMethod().WithParameters(mode))
            {
                LIBSSH2_ERROR result;
                var           channelHandle = UnsafeNativeMethods.libssh2_channel_open_ex(
                    this.session.Handle,
                    SshSessionChannelBase.Type,
                    (uint)SshSessionChannelBase.Type.Length,
                    DefaultWindowSize,
                    DefaultPacketSize,
                    null,
                    0);

                if (channelHandle.IsInvalid)
                {
                    result = (LIBSSH2_ERROR)UnsafeNativeMethods.libssh2_session_last_errno(
                        this.session.Handle);
                }
                else
                {
                    result = LIBSSH2_ERROR.NONE;
                }

                if (result != LIBSSH2_ERROR.NONE)
                {
                    throw this.session.CreateException(result);
                }

                channelHandle.SessionHandle = this.session.Handle;

                //
                // Configure how extended data (stderr, in particular) should
                // be handled.
                //
                result = (LIBSSH2_ERROR)UnsafeNativeMethods.libssh2_channel_handle_extended_data2(
                    channelHandle,
                    mode);
                if (result != LIBSSH2_ERROR.NONE)
                {
                    channelHandle.Dispose();
                    throw this.session.CreateException(result);
                }

                return(channelHandle);
            }
        }
예제 #3
0
 public static extern int libssh2_channel_handle_extended_data2(
     SshChannelHandle channel,
     LIBSSH2_CHANNEL_EXTENDED_DATA mode);
        public SshShellChannel OpenShellChannel(
            LIBSSH2_CHANNEL_EXTENDED_DATA mode,
            string term,
            ushort widthInChars,
            ushort heightInChars,
            IEnumerable <EnvironmentVariable> environmentVariables = null)
        {
            this.session.Handle.CheckCurrentThreadOwnsHandle();
            Utilities.ThrowIfNull(term, nameof(term));

            using (SshTraceSources.Default.TraceMethod().WithParameters(
                       term,
                       widthInChars,
                       heightInChars))
            {
                var channelHandle = OpenChannelInternal(mode);

                if (environmentVariables != null)
                {
                    foreach (var environmentVariable in environmentVariables
                             .Where(i => !string.IsNullOrEmpty(i.Value)))
                    {
                        SetEnvironmentVariable(
                            channelHandle,
                            environmentVariable);
                    }
                }

                //
                // Request a pseudoterminal. This must be done before the shell
                // is launched.
                //
                var result = (LIBSSH2_ERROR)UnsafeNativeMethods.libssh2_channel_request_pty_ex(
                    channelHandle,
                    term,
                    (uint)term.Length,
                    null,   // TODO: pass modifiers?
                    0,
                    widthInChars,
                    heightInChars,
                    0,
                    0);

                if (result != LIBSSH2_ERROR.NONE)
                {
                    channelHandle.Dispose();
                    throw this.session.CreateException(result);
                }

                //
                // Launch the shell.
                //
                var request = "shell";
                result = (LIBSSH2_ERROR)UnsafeNativeMethods.libssh2_channel_process_startup(
                    channelHandle,
                    request,
                    (uint)request.Length,
                    null,
                    0);

                if (result != LIBSSH2_ERROR.NONE)
                {
                    channelHandle.Dispose();
                    throw this.session.CreateException(result);
                }

                return(new SshShellChannel(this.session, channelHandle));
            }
        }