Sha256: e920754c78a4be8c44735ef826bbb7994de400cc3d0bf28c5f6a28b6263329bf

Contents?: true

Size: 1.91 KB

Versions: 15

Compression:

Stored size: 1.91 KB

Contents

Before you can do anything with Net::SSH, you need to require the @net/ssh@ module:

{{{lang=ruby,caption=Requiring Net::SSH
require 'net/ssh'
}}}

Once you have required the @net/ssh@ module, you can begin an SSH session by calling @Net::SSH.start@.  This may be used in one of two ways. If called without a block, it will return a reference to the new session as an instance of a @Net::SSH::Session@. Used this way, you must explicitly close the session when you are finished with it.

{{{lang=ruby,number=true,caption=Opening an SSH session
session = Net::SSH.start( 'host', 'user', 'passwd' )
...
session.close
}}}

The other approach involves attaching a block to the start method. When used this way, the new session is passed to the block, and the session is automatically closed when the block exits.

{{{lang=ruby,number=true,caption=Opening a transactional SSH session
Net::SSH.start( 'host', 'user', 'passwd' ) do |session|
  ...
end
}}}

If you need to specify a different port on the host to connect to (the default is 22), you can specify it immediately after the @host@ parameter, like so:

{{{lang=ruby,number=true,caption=Specifying the SSH port
Net::SSH.start( 'host', 1234, 'user', 'passwd' ) do |session|
  ...
end
}}}

h3. Using Keyword Arguments

Some people prefer using keyword arguments for functions with more than a couple of parameters. The @start@ method supports this approach as well, although the @host@ parameter is always positional and always comes first.

{{{lang=ruby,number=true,caption=Using keyword arguments
Net::SSH.start( 'host',
                :password=>'passwd', 
                :port=>1234,
                :username=>'user',
       ... ) do |session|
  ...
end
}}}

(More about the "@...@" stuff, later.)

h3. Failed Authentication

If the username and/or password given to @start@ are incorrect, authentication will fail. If authentication fails, a @Net::SSH::AuthenticationFailed@ exception will be raised.

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
net-ssh-1.0.1 doc/manual/parts/0006.txt
net-ssh-1.0.10 doc/manual/parts/0006.txt
net-ssh-1.0.6 doc/manual/parts/0006.txt
net-ssh-1.0.5 doc/manual/parts/0006.txt
net-ssh-1.0.9 doc/manual/parts/0006.txt
net-ssh-1.0.3 doc/manual/parts/0006.txt
net-ssh-1.0.2 doc/manual/parts/0006.txt
net-ssh-1.0.8 doc/manual/parts/0006.txt
net-ssh-1.0.7 doc/manual/parts/0006.txt
net-ssh-1.0.4 doc/manual/parts/0006.txt
net-ssh-1.1.2 doc/manual/parts/0006.txt
net-ssh-1.1.1 doc/manual/parts/0006.txt
net-ssh-1.1.0 doc/manual/parts/0006.txt
net-ssh-1.1.3 doc/manual/parts/0006.txt
net-ssh-1.1.4 doc/manual/parts/0006.txt