简体   繁体   English

SSH 隧道带终端

[英]SSH tunnel with terminal

I'm a beginner at ssh so be kind with my limited knowedge;)我是 ssh 的初学者,所以请善待我有限的知识;)

What I want to do is as follow:我想做的是如下:

SSH to a PC and then from this PC SSH to another one, see picture below: SSH 到 PC,然后从这台 PC SSH 到另一台,见下图:

SSH Tunnel SSH 隧道

Here are the commands I run when I do it manually:以下是我手动执行时运行的命令:

ssh user@155.254.0.1 

After this command I will be prompt to enter the password.在此命令之后,我将提示输入密码。

From here I ssh again to the next "PC" with the following command:从这里我 ssh 再次使用以下命令到下一个“PC”:

ssh root@190.22.0.1 -y

and then I get prompt to enter the password.然后我得到提示输入密码。

I tried to use a python script to do it automatically by I was not able to come to the next seconds step.我尝试使用 python 脚本自动执行此操作,但无法进入下一秒步骤。

Here is how the python code looks like: python 代码如下所示:

import subprocess

cmd_1 = ["ls"]

cmd_3 = ['ls', '-l']

def send_top_cmd():
    cmd_2 = ['top', "-b", "-n", "5"]
    com2 = subprocess.Popen(cmd_2, stdout=out)
    com2.wait()

def send_ssh_pc_1():
    cmd = ["sshpass", "-p", "'user'", "ssh", "swupdate@155.254.0.1"]
    ssh_sga = subprocess.Popen(cmd, stdout=out)
    ssh_sga.wait()

def send_ssh_pc_2():
    cmd = ["sshpass", "-p", "'root'", "ssh", "root@190.22.0.1"]
    ssh_hpa = subprocess.Popen(cmd, stdout=out)
    ssh_hpa.wait()

def send_exit():
    cmd = ["exit"]
    process = subprocess.Popen(cmd, stdout=out)
    cmd = ["exit"]
    process = subprocess.Popen(cmd, stdout=out)
    print("done")

with open('output.txt', 'w') as out:
    send_ssh_pc_1() # ssh PC 1
    send_ssh_pc_2() # ssh PC 2
    send_top_cmd()  # Send a simply command
    send_exit()

The script fails at the "send_ssh_pc_2()" since I dont have sshpass installed and there's no possibility to install it there:(该脚本在“send_ssh_pc_2()”处失败,因为我没有安装 sshpass,也无法在那里安装它:(

Is there a easier way to do it automatically?有没有更简单的方法可以自动完成?

So much easier to write as an answer instead of comment.作为答案而不是评论更容易写。

First, enable RSA authentication for both of your SSH boxes.首先,为您的两个 SSH 盒子启用 RSA 身份验证。 Then you don't need to worry about passing password.然后您无需担心传递密码。 https://www.ssh.com/academy/ssh/public-key-authentication https://www.ssh.com/academy/ssh/public-key-authentication

Then open SSH tunnel from your computer with following command:然后使用以下命令从您的计算机打开 SSH 隧道:

ssh -L 2222:190.22.0.1:22 user@155.254.0.1 

That will enable tunnel from your local computer port 2222 to host in address 190.22.0.1 port 22. So next you can open SSH connection to the target computer like this.这将启用从本地计算机端口 2222 到地址 190.22.0.1 端口 22 中的主机的隧道。所以接下来您可以像这样打开与目标计算机的 SSH 连接。

ssh -p 2222 root@localhost  

If your RSA private key is authorized to both user@155.254.0.1 and root@190.22.0.1 no passwords should be asked and you have SSH connection to 192.22.0.1 from your workstation.如果您的 RSA 私钥同时被授权给 user@155.254.0.1 和 root@190.22.0.1,则不应询问密码,并且您的工作站具有与 192.22.0.1 的 SSH 连接。

Of course you can tunnel any TCP traffic, not just SSH.当然,您可以隧道任何 TCP 流量,而不仅仅是 SSH。

*** ADDED *** *** 添加 ***

Here is example of content of authorized_keys -file (some content removed).这是 authorized_keys -file 内容的示例(删除了一些内容)。


ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA3fauf5H3kN92Gxx8xerCF***********************************************************************************************************************PPIrUMdf1948pqLspom8SIyeqJeKX8wVqcJch35O0Q4UVlbw== user@host
ssh-rsa AAAAB3Nzaasdfrgaa4634w4gfdewrtfauf5H3kN92Gxx8xerCF***********************************************************************************************************************PPIrUMdf1948pqLspossdfgqrbbsrdtwetdsfgsfdgsd== admin@anotherhost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM