GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Support 'user@host:port' syntax for SSH Transport (#6558) · PowerShell/PowerShell@38c53c5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/System.Management.Automation/engine/remoting/commands
    • PSRemotingCmdlet.cs
    • PushRunspaceCommand.cs
    • newrunspacecommand.cs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs‎

Copy file name to clipboardExpand all lines: src/System.Management.Automation/engine/remoting/commands/PSRemotingCmdlet.cs
+49-5Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,42 @@ internal static void ValidateSpecifiedAuthentication(PSCredential credential, st
825825

826826
#endregion
827827

828+
/// <summary>
829+
/// Parse a hostname used with SSH Transport to get embedded
830+
/// username and/or port.
831+
/// </summary>
832+
/// <param name="hostname">host name to parse</param>
833+
/// <param name="host">resolved target host</param>
834+
/// <param name="userName">resolved target user name</param>
835+
/// <param name="port">resolved target port</param>
836+
protected void ParseSshHostName(string hostname, out string host, out string userName, out int port)
837+
{
838+
host = hostname;
839+
userName = this.UserName;
840+
port = this.Port;
841+
try
842+
{
843+
Uri uri = new System.Uri("ssh://" + hostname);
844+
host = ResolveComputerName(uri.Host);
845+
ValidateComputerName(new string[]{host});
846+
if (uri.UserInfo != String.Empty)
847+
{
848+
userName = uri.UserInfo;
849+
}
850+
if (uri.Port != -1)
851+
{
852+
port = uri.Port;
853+
}
854+
}
855+
catch (UriFormatException)
856+
{
857+
ThrowTerminatingError(new ErrorRecord(
858+
new ArgumentException(PSRemotingErrorInvariants.FormatResourceString(
859+
RemotingErrorIdStrings.InvalidComputerName)), "PSSessionInvalidComputerName",
860+
ErrorCategory.InvalidArgument, hostname));
861+
}
862+
}
863+
828864
/// <summary>
829865
/// Parse the Connection parameter HashTable array.
830866
/// </summary>
@@ -856,8 +892,16 @@ internal SSHConnection[] ParseSSHConnectionHashTable()
856892
if (paramName.Equals(ComputerNameParameter, StringComparison.OrdinalIgnoreCase) || paramName.Equals(HostNameAlias, StringComparison.OrdinalIgnoreCase))
857893
{
858894
var resolvedComputerName = ResolveComputerName(GetSSHConnectionStringParameter(item[paramName]));
859-
ValidateComputerName(new string[] { resolvedComputerName });
860-
connectionInfo.ComputerName = resolvedComputerName;
895+
ParseSshHostName(resolvedComputerName, out string host, out string userName, out int port);
896+
connectionInfo.ComputerName = host;
897+
if (userName != String.Empty)
898+
{
899+
connectionInfo.UserName = userName;
900+
}
901+
if (port != -1)
902+
{
903+
connectionInfo.Port = port;
904+
}
861905
}
862906
else if (paramName.Equals(UserNameParameter, StringComparison.OrdinalIgnoreCase))
863907
{
@@ -1337,11 +1381,11 @@ protected virtual void CreateHelpersForSpecifiedComputerNames()
13371381
/// </summary>
13381382
protected void CreateHelpersForSpecifiedSSHComputerNames()
13391383
{
1340-
ValidateComputerName(ResolvedComputerNames);
1341-
13421384
foreach (string computerName in ResolvedComputerNames)
13431385
{
1344-
var sshConnectionInfo = new SSHConnectionInfo(this.UserName, computerName, this.KeyFilePath, this.Port);
1386+
ParseSshHostName(computerName, out string host, out string userName, out int port);
1387+
1388+
var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port);
13451389
var typeTable = TypeTable.LoadDefaultTypeFiles();
13461390
var remoteRunspace = RunspaceFactory.CreateRunspace(sshConnectionInfo, this.Host, typeTable) as RemoteRunspace;
13471391
var pipeline = CreatePipeline(remoteRunspace);
Collapse file

‎src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs‎

Copy file name to clipboardExpand all lines: src/System.Management.Automation/engine/remoting/commands/PushRunspaceCommand.cs
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,8 @@ private RemoteRunspace GetRunspaceForContainerSession()
12811281
/// </summary>
12821282
private RemoteRunspace GetRunspaceForSSHSession()
12831283
{
1284-
var sshConnectionInfo = new SSHConnectionInfo(this.UserName, ResolveComputerName(HostName), this.KeyFilePath, this.Port);
1284+
ParseSshHostName(HostName, out string host, out string userName, out int port);
1285+
var sshConnectionInfo = new SSHConnectionInfo(userName, host, this.KeyFilePath, port);
12851286
var typeTable = TypeTable.LoadDefaultTypeFiles();
12861287

12871288
// Use the class _tempRunspace field while the runspace is being opened so that StopProcessing can be handled at that time.
Collapse file

‎src/System.Management.Automation/engine/remoting/commands/newrunspacecommand.cs‎

Copy file name to clipboardExpand all lines: src/System.Management.Automation/engine/remoting/commands/newrunspacecommand.cs
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,18 +1067,20 @@ private List<RemoteRunspace> CreateRunspacesForSSHHostParameterSet()
10671067
{
10681068
// Resolve all the machine names
10691069
String[] resolvedComputerNames;
1070+
10701071
ResolveComputerNames(HostName, out resolvedComputerNames);
1071-
ValidateComputerName(resolvedComputerNames);
10721072

10731073
var remoteRunspaces = new List<RemoteRunspace>();
10741074
int index = 0;
10751075
foreach (var computerName in resolvedComputerNames)
10761076
{
1077+
ParseSshHostName(computerName, out string host, out string userName, out int port);
1078+
10771079
var sshConnectionInfo = new SSHConnectionInfo(
1078-
this.UserName,
1079-
computerName,
1080+
userName,
1081+
host,
10801082
this.KeyFilePath,
1081-
this.Port);
1083+
port);
10821084
var typeTable = TypeTable.LoadDefaultTypeFiles();
10831085
string rsName = GetRunspaceName(index, out int rsIdUnused);
10841086
index++;

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.