Skip to content

Commit e23479f

Browse files
billdenneyjeroen
andcommitted
Allow ssh_session_info() to work for disconnected sessions (#59)
Co-authored-by: Jeroen Ooms <[email protected]>
1 parent 96c457c commit e23479f

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
0.8.3
22
- Windows: update to libssh 0.10.5
33
- scp_upload() now copies the file mode permissions similar to scp -p
4+
- ssh_session_info() now works for disconnected sessions
45

56
0.8.2
67
- Fix strict-prototypes warnings

R/connect.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ assert_session <- function(x){
116116
#' @export
117117
print.ssh_session <- function(x, ...){
118118
info <- ssh_session_info(x)
119-
cat(sprintf("<ssh session>\nconnected: %s@%s:%d\nserver: %s\n", info$user, info$host, info$port, info$sha1))
119+
status <- ifelse(info$connected, 'connected', 'disconnected')
120+
cat(sprintf("<ssh session>\n%s@%s:%d (%s)\nserver: %s\n", info$user, info$host, info$port, status, info$sha1))
120121
}
121122

122123
#' @export

src/session.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ ssh_session ssh_ptr_get(SEXP ptr){
44
ssh_session ssh = (ssh_session) R_ExternalPtrAddr(ptr);
55
if(ssh == NULL)
66
Rf_error("SSH session pointer is dead");
7-
if(!ssh_is_connected(ssh))
8-
Rf_error("ssh session has been disconnected");
7+
//if(!ssh_is_connected(ssh))
8+
// Rf_error("ssh session has been disconnected");
99
return ssh;
1010
}
1111

@@ -212,17 +212,18 @@ SEXP C_ssh_info(SEXP ptr){
212212
ssh_key key;
213213
unsigned char * hash = NULL;
214214
size_t hlen = 0;
215-
assert_or_disconnect(myssh_get_publickey(ssh, &key), "ssh_get_publickey", ssh);
216-
assert_or_disconnect(ssh_get_publickey_hash(key, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen), "ssh_get_publickey_hash", ssh);
215+
if (connected) {
216+
assert_or_disconnect(myssh_get_publickey(ssh, &key), "ssh_get_publickey", ssh);
217+
assert_or_disconnect(ssh_get_publickey_hash(key, SSH_PUBLICKEY_HASH_SHA1, &hash, &hlen), "ssh_get_publickey_hash", ssh);
218+
}
217219

218220
SEXP out = PROTECT(Rf_allocVector(VECSXP, 6));
219221
SET_VECTOR_ELT(out, 0, make_string(user));
220222
SET_VECTOR_ELT(out, 1, make_string(host));
221223
SET_VECTOR_ELT(out, 2, make_string(identity));
222224
SET_VECTOR_ELT(out, 3, Rf_ScalarInteger(port));
223225
SET_VECTOR_ELT(out, 4, Rf_ScalarLogical(connected));
224-
SET_VECTOR_ELT(out, 5, make_string(ssh_get_hexa(hash, hlen)));
225-
226+
SET_VECTOR_ELT(out, 5, connected ? make_string(ssh_get_hexa(hash, hlen)) : Rf_ScalarString(NA_STRING));
226227
if(user) ssh_string_free_char(user);
227228
if(host) ssh_string_free_char(host);
228229
if(identity) ssh_string_free_char(identity);

0 commit comments

Comments
 (0)