【问题标题】:How to access a second router from a first router using PHP SSH如何使用 PHP SSH 从第一个路由器访问第二个路由器
【发布时间】:2023-01-14 08:16:20
【问题描述】:

下面给出的是我的问题。

  1. 我想要访问路由器 R2 并运行一些命令并获取输出。
  2. 无法从我的 PC 直接访问路由器 R2。要访问路由器 R2,我需要先访问直接连接到我的 PC 的路由器 R1,然后使用 ssh 命令访问 R2。
  3. 可以使用 ssh 访问路由器 R1。任何路由器都不允许 Telnet。
  4. 我找到了使用 PHP SSH 访问 R1 的代码,它工作正常。
        <?php
        $connection = ssh2_connect('IP of R1', 22);
        ssh2_auth_password($connection, 'username', 'password');
        $stream = ssh2_exec($connection, 'command');
        stream_set_blocking($stream, true);
        $output = stream_get_contents($stream);
        echo "<pre>{$output}</pre>";
        ?>
    
    1. 使用上面的代码,我可以运行所有命令并从路由器 R1 获取输出。但是我无法运行命令来访问路由器 R2。要从我访问 R2,我需要从 R1 运行以下命令。
    <#ssh <IP of R2> //then wait for username prompt
    enter username //wait for password prompt
    enter password //thats it.
    

    请提出一个解决方案,以便我可以通过 R1 访问 R2 并自动执行一些任务。

    谢谢你。

【问题讨论】:

  • 你可以从命令提示符/终端访问它吗?

标签: php ssh remote-access


【解决方案1】:
  • 设置 ssh 密钥身份验证并通过 ssh 工具或手动添加密钥相互交换公钥(不要忘记将 R1 公钥也设置为 R1 authorized_keys 自身)

ssh-copy-id

  • 将 R1 上的端口反转到另一个端口

ssh -N -R 1235:localhost:1234 R1_IP # R1 上的反向隧道监听外部的 1245 并将其反向到 localhost:1234

  • 在 R1 上设置转发规则以将传入转发到本地反向端口

ssh -N -L 1234:R2_IP:22 R1_IP # 转发 R1:1234 到 R2:22

现在从 PC 连接到 R1:1235,你将在 R2:22 上结束

/etc/ssh/sshd_config on R1 might need to following options set

网关端口 是

【讨论】:

    猜你喜欢
    • 2022-10-04
    • 2019-07-06
    • 1970-01-01
    • 2016-12-08
    • 2022-07-21
    • 2020-02-03
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    相关资源
    最近更新 更多