Class: LibSSH::Session

Inherits:
Object
  • Object
show all
Defined in:
ext/libssh_ruby/session.c,
ext/libssh_ruby/session.c

Overview

Wrapper for ssh_session struct in libssh.

Instance Method Summary collapse

Constructor Details

#initializeObject

Create a new SSH session.

See Also:

Since:

  • 0.1.0



57
58
59
60
61
62
63
# File 'ext/libssh_ruby/session.c', line 57

static VALUE m_initialize(VALUE self) {
  SessionHolder *holder;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  holder->session = ssh_new();
  return self;
}

Instance Method Details

#add_identity(path_format) ⇒ nil

Add the identity file name format.

Parameters:

  • path_format (String)

    Format string for identity file.

Returns:

  • (nil)

See Also:

Since:

  • 0.1.0



423
424
425
426
427
428
429
430
431
# File 'ext/libssh_ruby/session.c', line 423

static VALUE m_add_identity(VALUE self, VALUE path) {
  SessionHolder *holder;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  RAISE_IF_ERROR(ssh_options_set(holder->session, SSH_OPTIONS_ADD_IDENTITY,
                                 StringValueCStr(path)));

  return Qnil;
}

#bindaddr=(addr) ⇒ nil

Set the address to bind the client to.

Parameters:

  • addr (String)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



163
164
165
# File 'ext/libssh_ruby/session.c', line 163

static VALUE m_set_bindaddr(VALUE self, VALUE addr) {
  return set_string_option(self, SSH_OPTIONS_BINDADDR, addr);
}

#compression=(algorithm) ⇒ nil

Set the compression to use for both directions communication

Parameters:

  • algorithm (TrueClass, FalseClass)
  • algorithm (String)

    e.g. “yes”, “no”, “zlib”, “[email protected]”, “none”

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'ext/libssh_ruby/session.c', line 294

static VALUE m_set_compression(VALUE self, VALUE compression) {
  SessionHolder *holder;
  if (compression == Qtrue || compression == Qfalse) {
    const char *val;
    if (compression == Qtrue) {
      val = "yes";
    } else {
      val = "no";
    }

    TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
    RAISE_IF_ERROR(
        ssh_options_set(holder->session, SSH_OPTIONS_COMPRESSION, val));

    return Qnil;
  } else {
    return set_string_option(self, SSH_OPTIONS_COMPRESSION, compression);
  }
}

#compression_level=(level) ⇒ nil

Set the compression level to use for zlib functions

Parameters:

  • level (Fixnum)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



322
323
324
# File 'ext/libssh_ruby/session.c', line 322

static VALUE m_set_compression_level(VALUE self, VALUE level) {
  return set_int_option(self, SSH_OPTIONS_COMPRESSION_LEVEL, level);
}

#connectnil

Connect to the SSH server.

Returns:

  • (nil)

See Also:

Since:

  • 0.1.0



450
451
452
453
454
455
456
457
458
459
460
# File 'ext/libssh_ruby/session.c', line 450

static VALUE m_connect(VALUE self) {
  SessionHolder *holder;
  struct nogvl_session_args args;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  args.session = holder->session;
  rb_thread_call_without_gvl(nogvl_connect, &args, RUBY_UBF_IO, NULL);
  RAISE_IF_ERROR(args.rc);

  return Qnil;
}

#disconnectnil

Disconnect from a session.

Returns:

  • (nil)

See Also:

Since:

  • 0.3.0

Since:

  • 0.1.0



476
477
478
479
480
481
482
483
484
485
# File 'ext/libssh_ruby/session.c', line 476

static VALUE m_disconnect(VALUE self) {
  SessionHolder *holder;
  struct nogvl_session_args args;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  args.session = holder->session;
  rb_thread_call_without_gvl(nogvl_disconnect, &args, RUBY_UBF_IO, NULL);

  return Qnil;
}

#fdFixnum

Get the fd of a connection

Returns:

  • (Fixnum)

See Also:

Since:

  • 0.3.0



510
511
512
513
514
# File 'ext/libssh_ruby/session.c', line 510

static VALUE m_fd(VALUE self) {
  SessionHolder *holder;
  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  return INT2FIX(ssh_get_fd(holder->session));
}

#get_publickeyKey

Get the server public key from a session.

Returns:

See Also:

Since:

  • 0.1.0



598
599
600
601
602
603
604
605
606
607
608
# File 'ext/libssh_ruby/session.c', line 598

static VALUE m_get_publickey(VALUE self) {
  SessionHolder *holder;
  KeyHolder *key_holder;
  VALUE key;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  key = rb_obj_alloc(rb_cLibSSHKey);
  key_holder = libssh_ruby_key_holder(key);
  RAISE_IF_ERROR(ssh_get_publickey(holder->session, &key_holder->key));
  return key;
}

#gssapi_client_identity=(identity) ⇒ nil

Set the GSSAPI client identity that libssh should expect when connecting to the server

Parameters:

  • identity (String)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



359
360
361
# File 'ext/libssh_ruby/session.c', line 359

static VALUE m_set_gssapi_client_identity(VALUE self, VALUE identity) {
  return set_string_option(self, SSH_OPTIONS_GSSAPI_CLIENT_IDENTITY, identity);
}

#gssapi_delegate_credentials=(enable) ⇒ nil

Set whether GSSAPI should delegate credentials to the server

Parameters:

  • enable (FalseClass, TrueClass)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



383
384
385
386
# File 'ext/libssh_ruby/session.c', line 383

static VALUE m_set_gssapi_delegate_credentials(VALUE self, VALUE enable) {
  return set_int_option(self, SSH_OPTIONS_GSSAPI_DELEGATE_CREDENTIALS,
                        INT2FIX(RTEST(enable) ? 1 : 0));
}

#gssapi_server_identity=(identity) ⇒ nil

Set the GSSAPI server identity that libssh should expect when connecting to the server

Parameters:

  • identity (String)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



371
372
373
# File 'ext/libssh_ruby/session.c', line 371

static VALUE m_set_gssapi_server_identity(VALUE self, VALUE identity) {
  return set_string_option(self, SSH_OPTIONS_GSSAPI_SERVER_IDENTITY, identity);
}

#host=(host) ⇒ nil

Set the hostname or IP address to connect to.

Parameters:

  • host (String)

Returns:

  • (nil)

See Also:

Since:

  • 0.1.0



116
117
118
# File 'ext/libssh_ruby/session.c', line 116

static VALUE m_set_host(VALUE self, VALUE host) {
  return set_string_option(self, SSH_OPTIONS_HOST, host);
}

#hostkeys=(key_types) ⇒ nil

Set the preferred server host key types

Parameters:

  • key_types (Array<String>)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



281
282
283
# File 'ext/libssh_ruby/session.c', line 281

static VALUE m_set_hostkeys(VALUE self, VALUE hostkeys) {
  return set_comma_separated_option(self, SSH_OPTIONS_HOSTKEYS, hostkeys);
}

#key_exchange=(methods) ⇒ nil

Set the key exchange method to be used

Parameters:

  • methods (Array<String>)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



269
270
271
# File 'ext/libssh_ruby/session.c', line 269

static VALUE m_set_key_exchange(VALUE self, VALUE kex) {
  return set_comma_separated_option(self, SSH_OPTIONS_KEY_EXCHANGE, kex);
}

#knownhosts=(path) ⇒ nil

Set the known hosts file name

Parameters:

  • path (String)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



175
176
177
# File 'ext/libssh_ruby/session.c', line 175

static VALUE m_set_knownhosts(VALUE self, VALUE path) {
  return set_string_option(self, SSH_OPTIONS_KNOWNHOSTS, path);
}

#log_verbosity=(verbosity) ⇒ nil

Set the session logging verbosity.

Parameters:

  • verbosity (Symbol)

    :none, :warn, :info, :debug, or :trace.

Returns:

  • (nil)

See Also:

Since:

  • 0.1.0



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'ext/libssh_ruby/session.c', line 73

static VALUE m_set_log_verbosity(VALUE self, VALUE verbosity) {
  ID id_verbosity;
  int c_verbosity;
  SessionHolder *holder;

  Check_Type(verbosity, T_SYMBOL);
  id_verbosity = SYM2ID(verbosity);

  if (id_verbosity == id_none) {
    c_verbosity = SSH_LOG_NONE;
  } else if (id_verbosity == id_warn) {
    c_verbosity = SSH_LOG_WARN;
  } else if (id_verbosity == id_info) {
    c_verbosity = SSH_LOG_INFO;
  } else if (id_verbosity == id_debug) {
    c_verbosity = SSH_LOG_DEBUG;
  } else if (id_verbosity == id_trace) {
    c_verbosity = SSH_LOG_TRACE;
  } else {
    rb_raise(rb_eArgError, "invalid verbosity: %" PRIsVALUE, verbosity);
  }

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  RAISE_IF_ERROR(ssh_options_set(holder->session, SSH_OPTIONS_LOG_VERBOSITY,
                                 &c_verbosity));

  return Qnil;
}

#parse_config(path = nil) ⇒ Boolean

Parse the ssh_config file.

Parameters:

  • Path (String, nil)

    to ssh_config. If nil, the default ~/.ssh/config will be used.

Returns:

  • (Boolean)

    Parsing the ssh_config was successful or not.

See Also:

Since:

  • 0.1.0



395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# File 'ext/libssh_ruby/session.c', line 395

static VALUE m_parse_config(int argc, VALUE *argv, VALUE self) {
  SessionHolder *holder;
  VALUE path;
  char *c_path;

  rb_scan_args(argc, argv, "01", &path);

  if (NIL_P(path)) {
    c_path = NULL;
  } else {
    c_path = StringValueCStr(path);
  }

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  if (ssh_options_parse_config(holder->session, c_path) == 0) {
    return Qtrue;
  } else {
    return Qfalse;
  }
}

#port=(port) ⇒ nil

Set the port to connect to.

Parameters:

  • port (Fixnum)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



151
152
153
# File 'ext/libssh_ruby/session.c', line 151

static VALUE m_set_port(VALUE self, VALUE port) {
  return set_int_option(self, SSH_OPTIONS_PORT, port);
}

#protocol=(protocol) ⇒ nil

Set allowed SSH protocols

Parameters:

  • protocol (Array<Integer>)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'ext/libssh_ruby/session.c', line 222

static VALUE m_set_protocol(VALUE self, VALUE protocols) {
  SessionHolder *holder;
  VALUE protocol;
  int i, ssh1 = 0, ssh2 = 0;

  Check_Type(protocols, T_ARRAY);

  for (i = 0; i < RARRAY_LEN(protocols); i++) {
    protocol = rb_ary_entry(protocols, i);
    Check_Type(protocol, T_FIXNUM);
    switch (FIX2INT(protocol)) {
      case 1:
        ssh1 = 1;
        break;
      case 2:
        ssh2 = 1;
        break;
      default:
        rb_raise(rb_eArgError, "protocol should be 1 or 2");
    }
  }

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  RAISE_IF_ERROR(ssh_options_set(holder->session, SSH_OPTIONS_SSH1, &ssh1));
  RAISE_IF_ERROR(ssh_options_set(holder->session, SSH_OPTIONS_SSH2, &ssh2));

  return Qnil;
}

#proxycommand=(command) ⇒ nil

Set the command to be executed in order to connect to server

Parameters:

  • command (String)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



347
348
349
# File 'ext/libssh_ruby/session.c', line 347

static VALUE m_set_proxycommand(VALUE self, VALUE proxycommand) {
  return set_string_option(self, SSH_OPTIONS_PROXYCOMMAND, proxycommand);
}

#server_knownFixnum

Check if the server is knonw.

Returns:

  • (Fixnum)

See Also:

Since:

  • 0.1.0



493
494
495
496
497
498
499
500
501
# File 'ext/libssh_ruby/session.c', line 493

static VALUE m_server_known(VALUE self) {
  SessionHolder *holder;
  int rc;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  rc = ssh_is_server_known(holder->session);
  RAISE_IF_ERROR(rc);
  return INT2FIX(rc);
}

#stricthostkeycheck=(enable) ⇒ nil

Set the parameter StrictHostKeyChecking to avoid asking about a fingerprint

Parameters:

  • enable (TrueClass, FalseClass)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



334
335
336
337
# File 'ext/libssh_ruby/session.c', line 334

static VALUE m_set_stricthostkeycheck(VALUE self, VALUE enable) {
  return set_int_option(self, SSH_OPTIONS_STRICTHOSTKEYCHECK,
                        INT2FIX(RTEST(enable) ? 1 : 0));
}

#timeout=(sec) ⇒ nil

Set a timeout for the connection in seconds

Parameters:

  • sec (Fixnum)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



198
199
200
# File 'ext/libssh_ruby/session.c', line 198

static VALUE m_set_timeout(VALUE self, VALUE sec) {
  return set_long_option(self, SSH_OPTIONS_TIMEOUT, sec);
}

#timeout_usec=(usec) ⇒ nil

Set a timeout for the connection in micro seconds

Parameters:

  • usec (Fixnum)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



210
211
212
# File 'ext/libssh_ruby/session.c', line 210

static VALUE m_set_timeout_usec(VALUE self, VALUE usec) {
  return set_long_option(self, SSH_OPTIONS_TIMEOUT_USEC, usec);
}

#user=(user) ⇒ nil

Set the username for authentication.

Parameters:

  • user (String)

Returns:

  • (nil)

See Also:

Since:

  • 0.2.0

Since:

  • 0.1.0



128
129
130
# File 'ext/libssh_ruby/session.c', line 128

static VALUE m_set_user(VALUE self, VALUE user) {
  return set_string_option(self, SSH_OPTIONS_USER, user);
}

#userauth_listArray<Symbol>

Get available authentication methods from the server.

Returns:

  • (Array<Symbol>)

See Also:

Since:

  • 0.1.0



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'ext/libssh_ruby/session.c', line 538

static VALUE m_userauth_list(VALUE self) {
  SessionHolder *holder;
  int list;
  VALUE ary;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  list = ssh_userauth_list(holder->session, NULL);
  RAISE_IF_ERROR(list);

  ary = rb_ary_new();
  if (list & SSH_AUTH_METHOD_NONE) {
    rb_ary_push(ary, ID2SYM(id_none));
  }
  if (list & SSH_AUTH_METHOD_PASSWORD) {
    rb_ary_push(ary, ID2SYM(id_password));
  }
  if (list & SSH_AUTH_METHOD_PUBLICKEY) {
    rb_ary_push(ary, ID2SYM(id_publickey));
  }
  if (list & SSH_AUTH_METHOD_HOSTBASED) {
    rb_ary_push(ary, ID2SYM(id_hostbased));
  }
  if (list & SSH_AUTH_METHOD_INTERACTIVE) {
    rb_ary_push(ary, ID2SYM(id_interactive));
  }
  if (list & SSH_AUTH_METHOD_GSSAPI_MIC) {
    rb_ary_push(ary, ID2SYM(id_gssapi_mic));
  }
  return ary;
}

#userauth_noneFixnum

Try to authenticate through then “none” method.

Returns:

  • (Fixnum)

See Also:

Since:

  • 0.1.0



522
523
524
525
526
527
528
529
530
# File 'ext/libssh_ruby/session.c', line 522

static VALUE m_userauth_none(VALUE self) {
  SessionHolder *holder;
  int rc;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  rc = ssh_userauth_none(holder->session, NULL);
  RAISE_IF_ERROR(rc);
  return INT2FIX(rc);
}

#userauth_publickey_autoFixnum

Try to automatically authenticate with public key and “none”.

Returns:

  • (Fixnum)

See Also:

Since:

  • 0.1.0



581
582
583
584
585
586
587
588
589
590
# File 'ext/libssh_ruby/session.c', line 581

static VALUE m_userauth_publickey_auto(VALUE self) {
  SessionHolder *holder;
  struct nogvl_session_args args;

  TypedData_Get_Struct(self, SessionHolder, &session_type, holder);
  args.session = holder->session;
  rb_thread_call_without_gvl(nogvl_userauth_publickey_auto, &args, RUBY_UBF_IO,
                             NULL);
  return INT2FIX(args.rc);
}

#write_knownhostnil

Write the current server as known in the known_hosts file.

Returns:

  • (nil)

See Also:

Since:

  • 0.1.0



616
617
618
619
620
621
622
# File 'ext/libssh_ruby/session.c', line 616

static VALUE m_write_knownhost(VALUE self) {
  SessionHolder *holder;

  holder = libssh_ruby_session_holder(self);
  RAISE_IF_ERROR(ssh_write_knownhost(holder->session));
  return Qnil;
}