Net::SSH forward local port

Discussion in 'Ruby' started by hartj659, Jun 16, 2014.

  1. hartj659

    hartj659 Guest

    I'm trying to programmatically configure an ssh local port forward through a middle server, then run some commands to a remote ssh server on the far end. I created this ruby script:

    Net::SSH.start('middleserver', 'testuser', :password => 'testpwd') do |ssh|
    ssh.forward.local(22004, "remoteserver", 22)
    ssh.loop
    end

    After running this script, I ran this on the command line:

    ssh remoteuser@localhost -p 22004 "hostname"

    Works.

    Then I tried this:

    Net::SSH.start('middleserver', 'testuser', :password => 'testpwd') do |ssh|
    ssh.forward.local(22004, "remoteserver", 22)
    ssh.loop
    end

    Net::SSH.start('localhost', 'remoteuser', :password => 'remotepwd', :port => 22004) do |ssh|
    output = ssh.exec!("hostname")
    print output
    end

    This does not work. I guess this is a naive implementation.

    Any suggestions on how I should code this so I can forward commands from my localhost port to the remote ssh server?

    Thanks for your suggestions.
     
    hartj659, Jun 16, 2014
    #1
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.