@@ -14,6 +14,8 @@ namespace Renci.SshNet
1414 /// </summary>
1515 public class Shell : IDisposable
1616 {
17+ private const int DefaultBufferSize = 1024 ;
18+
1719 private readonly ISession _session ;
1820 private readonly string _terminalName ;
1921 private readonly uint _columns ;
@@ -24,6 +26,7 @@ public class Shell : IDisposable
2426 private readonly Stream _outputStream ;
2527 private readonly Stream _extendedOutputStream ;
2628 private readonly int _bufferSize ;
29+ private readonly bool _noTerminal ;
2730 private ManualResetEvent _dataReaderTaskCompleted ;
2831 private IChannelSession _channel ;
2932 private AutoResetEvent _channelClosedWaitHandle ;
@@ -77,24 +80,66 @@ public class Shell : IDisposable
7780 /// <param name="terminalModes">The terminal modes.</param>
7881 /// <param name="bufferSize">Size of the buffer for output stream.</param>
7982 internal Shell ( ISession session , Stream input , Stream output , Stream extendedOutput , string terminalName , uint columns , uint rows , uint width , uint height , IDictionary < TerminalModes , uint > terminalModes , int bufferSize )
83+ : this ( session , input , output , extendedOutput , bufferSize , noTerminal : false )
8084 {
81- _session = session ;
82- _input = input ;
83- _outputStream = output ;
84- _extendedOutputStream = extendedOutput ;
8585 _terminalName = terminalName ;
8686 _columns = columns ;
8787 _rows = rows ;
8888 _width = width ;
8989 _height = height ;
9090 _terminalModes = terminalModes ;
91+ }
92+
93+ /// <summary>
94+ /// Initializes a new instance of the <see cref="Shell"/> class.
95+ /// </summary>
96+ /// <param name="session">The session.</param>
97+ /// <param name="input">The input.</param>
98+ /// <param name="output">The output.</param>
99+ /// <param name="extendedOutput">The extended output.</param>
100+ /// <param name="bufferSize">Size of the buffer for output stream.</param>
101+ internal Shell ( ISession session , Stream input , Stream output , Stream extendedOutput , int bufferSize )
102+ : this ( session , input , output , extendedOutput , bufferSize , noTerminal : true )
103+ {
104+ }
105+
106+ /// <summary>
107+ /// Initializes a new instance of the <see cref="Shell"/> class.
108+ /// </summary>
109+ /// <param name="session">The session.</param>
110+ /// <param name="input">The input.</param>
111+ /// <param name="output">The output.</param>
112+ /// <param name="extendedOutput">The extended output.</param>
113+ /// <param name="bufferSize">Size of the buffer for output stream.</param>
114+ /// <param name="noTerminal">Disables pseudo terminal allocation or not.</param>
115+ private Shell ( ISession session , Stream input , Stream output , Stream extendedOutput , int bufferSize , bool noTerminal )
116+ {
117+ if ( bufferSize == - 1 )
118+ {
119+ bufferSize = DefaultBufferSize ;
120+ }
121+ #if NET8_0_OR_GREATER
122+ ArgumentOutOfRangeException . ThrowIfNegativeOrZero ( bufferSize ) ;
123+ #else
124+ if ( bufferSize <= 0 )
125+ {
126+ throw new ArgumentOutOfRangeException ( nameof ( bufferSize ) ) ;
127+ }
128+ #endif
129+ _session = session ;
130+ _input = input ;
131+ _outputStream = output ;
132+ _extendedOutputStream = extendedOutput ;
91133 _bufferSize = bufferSize ;
134+ _noTerminal = noTerminal ;
92135 }
93136
94137 /// <summary>
95138 /// Starts this shell.
96139 /// </summary>
97140 /// <exception cref="SshException">Shell is started.</exception>
141+ /// <exception cref="SshException">The pseudo-terminal request was not accepted by the server.</exception>
142+ /// <exception cref="SshException">The request to start a shell was not accepted by the server.</exception>
98143 public void Start ( )
99144 {
100145 if ( IsStarted )
@@ -112,8 +157,18 @@ public void Start()
112157 _session . ErrorOccured += Session_ErrorOccured ;
113158
114159 _channel . Open ( ) ;
115- _ = _channel . SendPseudoTerminalRequest ( _terminalName , _columns , _rows , _width , _height , _terminalModes ) ;
116- _ = _channel . SendShellRequest ( ) ;
160+ if ( ! _noTerminal )
161+ {
162+ if ( ! _channel . SendPseudoTerminalRequest ( _terminalName , _columns , _rows , _width , _height , _terminalModes ) )
163+ {
164+ throw new SshException ( "The pseudo-terminal request was not accepted by the server. Consult the server log for more information." ) ;
165+ }
166+ }
167+
168+ if ( ! _channel . SendShellRequest ( ) )
169+ {
170+ throw new SshException ( "The request to start a shell was not accepted by the server. Consult the server log for more information." ) ;
171+ }
117172
118173 _channelClosedWaitHandle = new AutoResetEvent ( initialState : false ) ;
119174
0 commit comments