Linux传递命令到跳板服务器

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

Linux pass command to jump server

问题

流程如下。

客户端 A -> 跳板服务器 B -> 服务器 C

如果我想连接到跳板服务器 B,我可以使用

ssh <user>@<jump-server>

在进入跳板服务器后,它会要求您输入要连接的 IP 地址(服务器 C)。

如何将输入直接传递给跳板服务器终端?
我尝试过

ssh -t <user>@<jump-server> '<server>'

但这并不起作用。
我想要的解决方案是直接连接到服务器 C,而无需每次都输入任何内容。
无论是纯 ssh 还是 Linux 脚本。

英文:

The flow is like this.

client A -> jump server B -> server C

If I want to connect to jump server B. I can use

ssh <user>@<jump-server>

After enter the jump server, it have the terminal request you to enter the ip (server C) to connect.

How can I passing the input to the jump server terminal directly?
I had try

ssh -t <user>@<jump-server> '<server>'

But this does not work.
The solution that what I want is directly connect to server C without any typing every time.
No matter it is pure ssh or linux script.

答案1

得分: 1

可能你可以使用 expect 来解决它。

my-expect-script.exp

spawn ssh <user>@<jump-server>
send "<serverC-IP>\n"
interact

启动它的方式:

expect my-expect-script.exp

要安装 expect,请使用 sudo apt install expect

英文:

Probably you could solve it with expect.

my-expect-script.exp

spawn ssh &lt;user&gt;@&lt;jump-server&gt;
send &quot;&lt;serverC-IP&gt;\n&quot;
interact

Start it with:

expect my-expect-script.exp

To install expect use sudo apt install expect

答案2

得分: 0

你可以在你的~/.ssh/config文件中进行设置。它会看起来像这样,

主机 <server>
  代理跳转 <jumpserver>
  用户 <username>
英文:

You can set it up in your ~/.ssh/config file. It would look something like this,

Host &lt;server&gt;
  ProxyJump &lt;jumpserver&gt;
  User &lt;username&gt;

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

发表评论

匿名网友

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

确定