Class: LibSSH::Session

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

Instance Method Summary collapse

Constructor Details

#initializeObject



52
53
54
55
56
57
58
# File 'ext/libssh_ruby/session.c', line 52

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



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

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



159
160
161
# File 'ext/libssh_ruby/session.c', line 159

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



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'ext/libssh_ruby/session.c', line 290

static VALUE m_set_compression(VALUE self, VALUE compression) {
  SessionHolder *holder;
  char *val;
  if (compression == Qtrue || compression == Qfalse) {
    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



318
319
320
# File 'ext/libssh_ruby/session.c', line 318

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



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

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;
}

#get_publickeyKey

Get the server public key from a session.

Returns:

See Also:

Since:

  • 0.1.0



564
565
566
567
568
569
570
571
572
573
574
# File 'ext/libssh_ruby/session.c', line 564

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



355
356
357
# File 'ext/libssh_ruby/session.c', line 355

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



379
380
381
382
# File 'ext/libssh_ruby/session.c', line 379

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



367
368
369
# File 'ext/libssh_ruby/session.c', line 367

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



112
113
114
# File 'ext/libssh_ruby/session.c', line 112

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



277
278
279
# File 'ext/libssh_ruby/session.c', line 277

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



265
266
267
# File 'ext/libssh_ruby/session.c', line 265

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



171
172
173
# File 'ext/libssh_ruby/session.c', line 171

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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'ext/libssh_ruby/session.c', line 68

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



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

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



147
148
149
# File 'ext/libssh_ruby/session.c', line 147

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



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

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



343
344
345
# File 'ext/libssh_ruby/session.c', line 343

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



468
469
470
471
472
473
474
475
476
# File 'ext/libssh_ruby/session.c', line 468

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



330
331
332
333
# File 'ext/libssh_ruby/session.c', line 330

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



194
195
196
# File 'ext/libssh_ruby/session.c', line 194

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



206
207
208
# File 'ext/libssh_ruby/session.c', line 206

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



124
125
126
# File 'ext/libssh_ruby/session.c', line 124

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



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'ext/libssh_ruby/session.c', line 502

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



485
486
487
488
489
490
491
492
493
# File 'ext/libssh_ruby/session.c', line 485

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



546
547
548
549
550
551
552
553
554
555
# File 'ext/libssh_ruby/session.c', line 546

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



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

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;
}