You can do this even more directly with the "x/crypto/ssh" without the nc command, since there is a method to dial a connection from the remote host and presents it as a net.Conn.
Once you have an ssh.Client, you can use the Dial method to get a virtual net.Conn between you and the final host. You can then turn that into a new ssh.Conn with ssh.NewClientConn, and create a new ssh.Client with ssh.NewClient
// connect to the bastion host
bClient,err:=ssh.Dial("tcp",bastionAddr,config)iferr!=nil{log.Fatal(err)}// Dial a connection to the service host, from the bastion
conn,err:=bClient.Dial("tcp",serviceAddr)iferr!=nil{log.Fatal(err)}ncc,chans,reqs,err:=ssh.NewClientConn(conn,serviceAddr,config)iferr!=nil{log.Fatal(err)}sClient:=ssh.NewClient(ncc,chans,reqs)// sClient is an ssh client connected to the service host, through the bastion host.
https://stackoverflow.com/questions/35906991/go-x-crypto-ssh-how-to-establish-ssh-connection-to-private-instance-over-a-ba
You can do this even more directly with the "x/crypto/ssh" without the `nc` command, since there is a method to dial a connection from the remote host and presents it as a net.Conn.
Once you have an `ssh.Client`, you can use the [`Dial`](https://godoc.org/golang.org/x/crypto/ssh#Client.Dial) method to get a virtual net.Conn between you and the final host. You can then turn that into a new `ssh.Conn` with [`ssh.NewClientConn`](https://godoc.org/golang.org/x/crypto/ssh#NewClientConn), and create a new `ssh.Client` with [`ssh.NewClient`](https://godoc.org/golang.org/x/crypto/ssh#NewClient)
```go
// connect to the bastion host
bClient, err := ssh.Dial("tcp", bastionAddr, config)
if err != nil {
log.Fatal(err)
}
// Dial a connection to the service host, from the bastion
conn, err := bClient.Dial("tcp", serviceAddr)
if err != nil {
log.Fatal(err)
}
ncc, chans, reqs, err := ssh.NewClientConn(conn, serviceAddr, config)
if err != nil {
log.Fatal(err)
}
sClient := ssh.NewClient(ncc, chans, reqs)
// sClient is an ssh client connected to the service host, through the bastion host.
```
Implement better SSH handling.
Maybe functional programming.
Inspiration:
https://github.com/melbahja/goph
https://stackoverflow.com/questions/35906991/go-x-crypto-ssh-how-to-establish-ssh-connection-to-private-instance-over-a-ba
You can do this even more directly with the "x/crypto/ssh" without the
nc
command, since there is a method to dial a connection from the remote host and presents it as a net.Conn.Once you have an
ssh.Client
, you can use theDial
method to get a virtual net.Conn between you and the final host. You can then turn that into a newssh.Conn
withssh.NewClientConn
, and create a newssh.Client
withssh.NewClient