duanliao2310 2015-06-07 19:17 采纳率: 100%
浏览 28

通过多个主机进行SSH

Is it possible to SSH to another host while in an SSH session in golang? I tried chaining some stuff together like this, but the printout says the remote addr of client 2 is 0.0.0.0 and an error is given when I try to execute anything on an ssh.Session from client2.

host1 := "host1.com"
host2 := "host2.com"
client1, err := ssh.Dial("tcp", host, config)
if err != nil {
    panic("Failed to dial: " + err.Error())
}

conn, err := client1.Dial("tcp", host2)
if err != nil {
    panic(err)
}

sshConn, newChan, requestChan, err := ssh.NewClientConn(conn, host2, config)
if err != nil {
    panic(err)
}

client2 := ssh.NewClient(sshConn, newChan, requestChan)

fmt.Println("Client 2 RemoteAddr():", client2.RemoteAddr())
  • 写回答

1条回答 默认 最新

  • doufeixi6014 2015-06-07 22:00
    关注

    The problem is that you are running all of this code on the same host. Let's call the host you run your app on host0 for convenience.

    You start out making a connection from host0 to host1. That works fine. What you seem to want to do next is make a connection from host1 to host2. To do that you need to run the SSH code on host1. But your app, which is and always has been running on host0, is not in the place to do that.

    You will need to put a program on host1 that does the connection to host2 and then send a command down the SSH connection to host1 that will start that program.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题