不同主机使用不同的SSH端口/密钥,使用TortoisePLink

huangapple go评论57阅读模式
英文:

Different SSH ports/keys for different hosts, with TortoisePLink

问题

TortoiseHg(Mercurial)通常使用TortoisePLink/plink通过SSH连接。不同的SSH主机具有不同的设置,包括连接的私钥和端口。

在其全局的 hgrc 设置中,TortoiseHg 允许您为不同的主机指定不同的登录名和密码,但不包括私钥和端口。

如何在全局范围内覆盖SSH端口/私钥,但仅适用于某些主机而不适用于其他主机?

英文:

TortoiseHg (Mercurial) normally uses TortoisePLink/plink to connect via SSH. Different SSH hosts have different settings, including private keys and ports to connect to.

In its global hgrc settings, TortoiseHg lets you specify different logins and passwords for different hosts, but not private keys nor ports.

How to override SSH port/private key globally, but only for some hosts and not others?

答案1

得分: 2

我将列举一些不完美的方法以保证完整性,然后给出我认为更好的方法:

##### ssh 设置

Mercurial 的 `ssh` 设置可用于全局传递额外的参数给 plink:
```bash
ssh=路径\到\TortoisePLink.exe -i "private1.key" -i "private2.key" -P 端口号

但相同的 ssh 字符串用于所有主机,因此无法配置不同的端口。并且虽然可以列出多个私钥,但有些服务器将拒绝除第一个之外的所有私钥。

putty 的保存会话

plink 来自 putty 软件包,putty 可以将服务器配置(会话)存储在注册表中,路径为 HKEY_CURRENT_USER\SOFTWARE\SimonTatham\PuTTY\Sessions。如果在主机位置使用会话名称,例如 ssh://session-name/仓库路径,则 plink 可以使用这些会话。

但由于某些原因,plink 会忽略存储的端口设置,并使用默认的 SSH 端口。可能是一个 bug,但未修复。

本地 hgrcs

您可以通过创建本地的 .hgrc 文件来覆盖每个仓库的 ssh 设置,但如果您有大量的仓库,这将变得不方便。

  • 您必须记住每次都要这样做
  • 不能只是“从地址克隆仓库”
  • 如果您的仓库可以同步到两个不同的服务器怎么办?
  • 如果其中一个服务器更改了其 SSH 端口,您将不得不找到并更新每个仓库的所有位置。
明确的端口

您可以在远程仓库地址中明确指定端口:

ssh://服务器名称:端口号/仓库路径

与本地 hgrcs 一样存在相同的问题。

在为此努力了一段时间后,我终于想到了似乎可以工作的方法。我们可以在一个脚本中将 TortoisePLink 包装起来,以添加每个服务器的参数:

ssh=路径\到\TortoisePLinkWrapper.cmd <所有常规参数>

为您想要覆盖的每个主机创建一个名为 用户名@主机名.cfg 的文件,其内容应该是传递给 TortoisePLink 的附加参数,例如:

-P 1234 -i "路径\到\密钥文件"

将这个文件放在相同的目录中:

@echo off
set SERVER_PARAMS=
set SERVER_NAME=
call :find_server %*
if "%SERVER_NAME%"=="" goto :call_plink
set "SERVER_CONFIG_FILE=%~dp0%SERVER_NAME%.cfg"
if NOT EXIST "%SERVER_CONFIG_FILE%" goto :call_plink
set /p SERVER_PARAMS=<"%SERVER_CONFIG_FILE%"

:call_plink
rem 如果您在 ssh 命令之后传递了一些参数,TortoisePLink 会忽略其中一些参数,因此在大多数参数之前添加额外的命令
"%ProgramFiles%\TortoiseHg\lib\TortoisePLink.exe" %SERVER_PARAMS% %*
exit /B

:find_server
if [%1]==[] exit /B
rem 已知的消耗更多位置的参数
rem https://putty.org.ru/htmldoc/chapter7.html
if [%1]==[-P] shift & shift & goto :find_server
if [%1]==[-l] shift & shift & goto :find_server
if [%1]==[-pw] shift & shift & goto :find_server
if [%1]==[-proxycmd] shift & shift & goto :find_server
if [%1]==[-sercfg] shift & shift & goto :find_server
if [%1]==[-D] shift & shift & goto :find_server
if [%1]==[-L] shift & shift & goto :find_server
if [%1]==[-R] shift & shift & goto :find_server
if [%1]==[-i] shift & shift & goto :find_server
if [%1]==[-hostkey] shift & shift & goto :find_server
if [%1]==[-m] shift & shift & goto :find_server
if [%1]==[-nc] shift & shift & goto :find_server
if [%1]==[-sshlog] shift & shift & goto :find_server
if [%1]==[-sshrawlog] shift & shift & goto :find_server
set "PARAM=%~1"
rem 其他标志参数:
if "%PARAM:~0,1%"=="-" shift & goto :find_server
rem 第一个位置参数
set "SERVER_NAME=%~1"
exit /B
英文:

I'll list a number of imperfect approaches for completeness, then give the one I've found that I think is better:

ssh setting

Mercurial ssh setting can be used to pass additional params to plink globally:

ssh=path\to\TortoisePLink.exe -i &quot;private1.key&quot; -i &quot;private2.key&quot; -P port

But the same ssh string is used for all hosts, so different ports cannot be configured. And while you can list several private keys, some servers will refuse all but the first one.

putty's saved sessions

plink comes from putty package, and putty can store server configurations (sessions) in the registry, HKEY_CURRENT_USER\SOFTWARE\SimonTatham\PuTTY\Sessions. plink can use these if you use a session name in the host place, e.g. ssh://session-name/path/to/repository.

But for some reason plink ignores stored port settings and will use the default SSH port. Probably a bug, but it's unfixed.

Local hgrcs

You can override ssh for each repository by creating a local .hgrc file, but it's unwieldy if you have lots of repos.

  • You have to remember to do this every time
  • Cannot just "clone repo from address"
  • What if your repo can be synced to two different servers?
  • What if one of the servers changes its SSH port, you'll have to find and update every repo everywhere.
Explicit ports

You can specify ports explicitly in the remote repository address:

ssh://server.name:port/path/to/repository

Same problems as with local hgrcs.

After struggling with this for a while, I have finally thought of something that seems to work. We can wrap TortoisePLink in a script that adds per-server params:

ssh=path\to\TortoisePLinkWrapper.cmd &lt;all the normal common params&gt;

Create a file called username@host.name.cfg for every host you want to override, and its contents should be additional params to pass to TortoisePLink, e.g.:

-P 1234 -i &quot;path\to\key.file&quot;

Place this in the same dir:

@echo off
set SERVER_PARAMS=
set SERVER_NAME=
call :find_server %*
if &quot;%SERVER_NAME%&quot;==&quot;&quot; goto :call_plink
set &quot;SERVER_CONFIG_FILE=%~dp0%SERVER_NAME%.cfg&quot;
if NOT EXIST &quot;%SERVER_CONFIG_FILE%&quot; goto :call_plink
set /p SERVER_PARAMS=&lt;&quot;%SERVER_CONFIG_FILE%&quot;

:call_plink
rem TortoisePLink ignores some params if you pass them after the ssh command, so add extra commands before the bulk of them
&quot;%ProgramFiles%\TortoiseHg\lib\TortoisePLink.exe&quot; %SERVER_PARAMS% %*
exit /B

:find_server
if [%1]==[] exit /B
rem Known arguments which eat some more positions
rem https://putty.org.ru/htmldoc/chapter7.html
if [%1]==[-P] shift &amp; shift &amp; goto :find_server
if [%1]==[-l] shift &amp; shift &amp; goto :find_server
if [%1]==[-pw] shift &amp; shift &amp; goto :find_server
if [%1]==[-proxycmd] shift &amp; shift &amp; goto :find_server
if [%1]==[-sercfg] shift &amp; shift &amp; goto :find_server
if [%1]==[-D] shift &amp; shift &amp; goto :find_server
if [%1]==[-L] shift &amp; shift &amp; goto :find_server
if [%1]==[-R] shift &amp; shift &amp; goto :find_server
if [%1]==[-i] shift &amp; shift &amp; goto :find_server
if [%1]==[-hostkey] shift &amp; shift &amp; goto :find_server
if [%1]==[-m] shift &amp; shift &amp; goto :find_server
if [%1]==[-nc] shift &amp; shift &amp; goto :find_server
if [%1]==[-sshlog] shift &amp; shift &amp; goto :find_server
if [%1]==[-sshrawlog] shift &amp; shift &amp; goto :find_server
set &quot;PARAM=%~1&quot;
rem Other flag argument:
if &quot;%PARAM:~0,1%&quot;==&quot;-&quot; shift &amp; goto :find_server
rem First positional argument
set &quot;SERVER_NAME=%~1&quot;
exit /B

huangapple
  • 本文由 发表于 2023年6月15日 23:40:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定