Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,30 @@ public override string KeyFilePath
set { base.KeyFilePath = value; }
}

/// <summary>
/// Gets and sets a value for the SSH subsystem to use for the remote connection.
/// </summary>
[Parameter(ParameterSetName = InvokeCommandCommand.SSHHostParameterSet)]
[Parameter(ParameterSetName = InvokeCommandCommand.FilePathSSHHostParameterSet)]
public override string Subsystem
{
get { return base.Subsystem; }

set { base.Subsystem = value; }
}

/// <summary>
/// Gets and sets a value in milliseconds that limits the time allowed for an SSH connection to be established.
/// </summary>
[Parameter(ParameterSetName = InvokeCommandCommand.SSHHostParameterSet)]
[Parameter(ParameterSetName = InvokeCommandCommand.FilePathSSHHostParameterSet)]
public override int ConnectingTimeout
{
get { return base.ConnectingTimeout; }

set { base.ConnectingTimeout = value; }
}

/// <summary>
/// This parameter specifies that SSH is used to establish the remote
/// connection and act as the remoting transport. By default WinRM is used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Management.Automation.Remoting;
using System.Management.Automation.Remoting.Client;
using System.Management.Automation.Runspaces;
using System.Threading;

using Dbg = System.Management.Automation.Diagnostics;

Expand Down Expand Up @@ -285,6 +286,7 @@ internal struct SSHConnection
public string KeyFilePath;
public int Port;
public string Subsystem;
public int ConnectingTimeout;
}

/// <summary>
Expand Down Expand Up @@ -761,6 +763,20 @@ public virtual string KeyFilePath
set;
}

/// <summary>
/// Gets or sets a value for the SSH subsystem to use for the remote connection.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true,
ParameterSetName = PSRemotingBaseCmdlet.SSHHostParameterSet)]
public virtual string Subsystem { get; set; }

/// <summary>
/// Gets or sets a value in milliseconds that limits the time allowed for an SSH connection to be established.
/// Default timeout value is infinite.
/// </summary>
[Parameter(ParameterSetName = PSRemotingBaseCmdlet.SSHHostParameterSet)]
public virtual int ConnectingTimeout { get; set; } = Timeout.Infinite;

/// <summary>
/// This parameter specifies that SSH is used to establish the remote
/// connection and act as the remoting transport. By default WinRM is used
Expand Down Expand Up @@ -789,13 +805,6 @@ public virtual Hashtable[] SSHConnection
set;
}

/// <summary>
/// This parameter specifies the SSH subsystem to use for the remote connection.
/// </summary>
[Parameter(ValueFromPipelineByPropertyName = true,
ParameterSetName = InvokeCommandCommand.SSHHostParameterSet)]
public virtual string Subsystem { get; set; }

#endregion

#endregion Properties
Expand Down Expand Up @@ -856,6 +865,7 @@ internal static void ValidateSpecifiedAuthentication(PSCredential credential, st
private const string IdentityFilePathAlias = "IdentityFilePath";
private const string PortParameter = "Port";
private const string SubsystemParameter = "Subsystem";
private const string ConnectingTimeoutParameter = "ConnectingTimeout";

#endregion

Expand Down Expand Up @@ -902,7 +912,7 @@ protected void ParseSshHostName(string hostname, out string host, out string use
/// <returns>Array of SSHConnection objects.</returns>
internal SSHConnection[] ParseSSHConnectionHashTable()
{
List<SSHConnection> connections = new List<SSHConnection>();
List<SSHConnection> connections = new();
foreach (var item in this.SSHConnection)
{
if (item.ContainsKey(ComputerNameParameter) && item.ContainsKey(HostNameAlias))
Expand All @@ -915,7 +925,7 @@ internal SSHConnection[] ParseSSHConnectionHashTable()
throw new PSArgumentException(RemotingErrorIdStrings.SSHConnectionDuplicateKeyPath);
}

SSHConnection connectionInfo = new SSHConnection();
SSHConnection connectionInfo = new();
foreach (var key in item.Keys)
{
string paramName = key as string;
Expand Down Expand Up @@ -955,6 +965,10 @@ internal SSHConnection[] ParseSSHConnectionHashTable()
{
connectionInfo.Subsystem = GetSSHConnectionStringParameter(item[paramName]);
}
else if (paramName.Equals(ConnectingTimeoutParameter, StringComparison.OrdinalIgnoreCase))
{
connectionInfo.ConnectingTimeout = GetSSHConnectionIntParameter(item[paramName]);
}
else
{
throw new PSArgumentException(
Expand Down Expand Up @@ -1448,9 +1462,9 @@ protected void CreateHelpersForSpecifiedSSHComputerNames()
{
ParseSshHostName(computerName, out string host, out string userName, out int port);

var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port, this.Subsystem);
var sshConnectionInfo = new SSHConnectionInfo(userName, host, KeyFilePath, port, Subsystem, ConnectingTimeout);
var typeTable = TypeTable.LoadDefaultTypeFiles();
var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, Host, typeTable) as RemoteRunspace;
var pipeline = CreatePipeline(remoteRunspace);

var operation = new ExecutionCmdletHelperComputerName(remoteRunspace, pipeline);
Expand All @@ -1471,7 +1485,8 @@ protected void CreateHelpersForSpecifiedSSHHashComputerNames()
sshConnection.ComputerName,
sshConnection.KeyFilePath,
sshConnection.Port,
sshConnection.Subsystem);
sshConnection.Subsystem,
sshConnection.ConnectingTimeout);
var typeTable = TypeTable.LoadDefaultTypeFiles();
var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
var pipeline = CreatePipeline(remoteRunspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ private RemoteRunspace GetRunspaceForContainerSession()
private RemoteRunspace GetRunspaceForSSHSession()
{
ParseSshHostName(HostName, out string host, out string userName, out int port);
var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port, this.Subsystem);
var sshConnectionInfo = new SSHConnectionInfo(userName, host, KeyFilePath, port, Subsystem, ConnectingTimeout);
var typeTable = TypeTable.LoadDefaultTypeFiles();

// Use the class _tempRunspace field while the runspace is being opened so that StopProcessing can be handled at that time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,8 @@ private List<RemoteRunspace> CreateRunspacesForSSHHostParameterSet()
host,
this.KeyFilePath,
port,
Subsystem);
Subsystem,
ConnectingTimeout);
var typeTable = TypeTable.LoadDefaultTypeFiles();
string rsName = GetRunspaceName(index, out int rsIdUnused);
index++;
Expand All @@ -1118,7 +1119,8 @@ private List<RemoteRunspace> CreateRunspacesForSSHHostHashParameterSet()
sshConnection.ComputerName,
sshConnection.KeyFilePath,
sshConnection.Port,
sshConnection.Subsystem);
sshConnection.Subsystem,
sshConnection.ConnectingTimeout);
var typeTable = TypeTable.LoadDefaultTypeFiles();
string rsName = GetRunspaceName(index, out int rsIdUnused);
index++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1907,6 +1907,20 @@ internal override BaseClientSessionTransportManager CreateClientSessionTransport
/// </summary>
public sealed class SSHConnectionInfo : RunspaceConnectionInfo
{
#region Constants

/// <summary>
/// Default value for subsystem.
/// </summary>
private const string DefaultSubsystem = "powershell";

/// <summary>
/// Default value is infinite timeout.
/// </summary>
private const int DefaultConnectingTimeoutTime = Timeout.Infinite;

#endregion

#region Properties

/// <summary>
Expand Down Expand Up @@ -1945,6 +1959,16 @@ private string Subsystem
set;
}

/// <summary>
/// Gets or sets a time in milliseconds after which a connection attempt is terminated.
/// Default value (-1) never times out and a connection attempt waits indefinitely.
/// </summary>
public int ConnectingTimeout
{
get;
set;
}

#endregion

#region Constructors
Expand All @@ -1968,11 +1992,12 @@ public SSHConnectionInfo(
{
if (computerName == null) { throw new PSArgumentNullException(nameof(computerName)); }

this.UserName = userName;
this.ComputerName = computerName;
this.KeyFilePath = keyFilePath;
this.Port = 0;
this.Subsystem = DefaultSubsystem;
UserName = userName;
ComputerName = computerName;
KeyFilePath = keyFilePath;
Port = 0;
Subsystem = DefaultSubsystem;
ConnectingTimeout = DefaultConnectingTimeoutTime;
}

/// <summary>
Expand All @@ -1989,8 +2014,7 @@ public SSHConnectionInfo(
int port) : this(userName, computerName, keyFilePath)
{
ValidatePortInRange(port);

this.Port = port;
Port = port;
}

/// <summary>
Expand All @@ -2006,12 +2030,29 @@ public SSHConnectionInfo(
string computerName,
string keyFilePath,
int port,
string subsystem) : this(userName, computerName, keyFilePath)
string subsystem) : this(userName, computerName, keyFilePath, port)
{
ValidatePortInRange(port);
Subsystem = string.IsNullOrEmpty(subsystem) ? DefaultSubsystem : subsystem;
}

this.Port = port;
this.Subsystem = (string.IsNullOrEmpty(subsystem)) ? DefaultSubsystem : subsystem;
/// <summary>
/// Initializes a new instance of SSHConnectionInfo.
/// </summary>
/// <param name="userName">Name of user.</param>
/// <param name="computerName">Name of computer.</param>
/// <param name="keyFilePath">Path of key file.</param>
/// <param name="port">Port number for connection (default 22).</param>
/// <param name="subsystem">Subsystem to use (default 'powershell').</param>
/// <param name="connectingTimeout">Timeout time for terminating connection attempt.</param>
public SSHConnectionInfo(
string userName,
string computerName,
string keyFilePath,
int port,
string subsystem,
int connectingTimeout) : this(userName, computerName, keyFilePath, port, subsystem)
{
ConnectingTimeout = connectingTimeout;
}

#endregion
Expand Down Expand Up @@ -2064,11 +2105,12 @@ public override string CertificateThumbprint
internal override RunspaceConnectionInfo InternalCopy()
{
SSHConnectionInfo newCopy = new SSHConnectionInfo();
newCopy.ComputerName = this.ComputerName;
newCopy.UserName = this.UserName;
newCopy.KeyFilePath = this.KeyFilePath;
newCopy.Port = this.Port;
newCopy.Subsystem = this.Subsystem;
newCopy.ComputerName = ComputerName;
newCopy.UserName = UserName;
newCopy.KeyFilePath = KeyFilePath;
newCopy.Port = Port;
newCopy.Subsystem = Subsystem;
newCopy.ConnectingTimeout = ConnectingTimeout;

return newCopy;
}
Expand Down Expand Up @@ -2187,15 +2229,6 @@ internal int StartSSHProcess(

#endregion

#region Constants

/// <summary>
/// Default value for subsystem.
/// </summary>
private const string DefaultSubsystem = "powershell";

#endregion

#region SSH Process Creation

#if UNIX
Expand Down
Loading