且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在没有ssh延迟的情况下在远程计算机上运行命令

更新时间:2022-12-22 23:05:59

不确定是否实施了某些东西,或者只是收集了想法.

Not sure if you have something implemented or you just collect the thoughts.

Wikibooks 中描述了基本用例,但是最简单的方法是设置公共密钥身份验证和ssh_config(machine2的配置几乎相同):

The basic use case is described on wikibooks, but the easiest is to set up public key authentication and ssh_config (config for machine2 would be almost the same):

Host machine1
  HostName machine1.example.org
  ControlPath ~/.ssh/controlmasters/%r@%h:%p
  ControlMaster auto
  ControlPersist yes
  IdentityFile ~/.ssh/id_rsa-something

然后像这样调用远程脚本:

And then call the remote script like this:

ssh machine1 ./remote_script.py

第一次ssh调用将启动连接(并且会花费更长的时间),其他每个脚本调用都将使用现有的连接,并且几乎立即执行.

First ssh call will initiate the connection (and will take a bit longer), every other script call will use the existing connection and will be almost immediate.

如果您使用的是Python,则如果要升级一个级别(但实际上取决于用例),则可以使用paramiko甚至ansible来实现类似的行为.

If you are using Python, the similar behaviour you can achieve using paramiko or even ansible if you want to step one level up (but really depends on use case).