From 45733983c75cc961a81f5c304403a542f6663d1f Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 26 Feb 2008 11:27:25 -0500 Subject: [PATCH] We can now issue SSH commands to the server --- Source/AppController.m | 66 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/Source/AppController.m b/Source/AppController.m index cb03446..56a7ae4 100644 --- a/Source/AppController.m +++ b/Source/AppController.m @@ -115,7 +115,7 @@ [self setStatus:@"Bad username/password" isError:YES]; goto shutdown; } - + /* stat([[dragRegion filePath] UTF8String], &fileInfo); LIBSSH2_CHANNEL *channel = libssh2_scp_send(ssh, "~/tempupload.pdf", 0755, (unsigned long)fileInfo.st_size); @@ -156,8 +156,72 @@ libssh2_channel_wait_closed(channel); libssh2_channel_free(channel); channel = NULL; + */ + + LIBSSH2_CHANNEL *channel = libssh2_channel_open_session(ssh); + if (!channel) + { + [self setStatus:@"Could not open SSH channel for printing" isError:YES]; + goto shutdown; + } + + if (libssh2_channel_request_pty(channel, "vanilla")) + { + [self setStatus:@"Could not open ANSI TTY" isError:YES]; + goto shutdown; + } + + if (libssh2_channel_shell(channel)) + { + [self setStatus:@"Failed to open remote shell" isError:YES]; + goto shutdown; + } + + [self setStatus:@"Opened remote SSH shell" isError:NO]; + + libssh2_channel_flush_ex(channel, LIBSSH2_CHANNEL_EXTENDED_DATA_NORMAL); + + /* + char *e = malloc(1024 * sizeof(char)); + libssh2_session_last_error(ssh, &e, NULL, 0); + NSLog(@"e = %s", e); + */ + + libssh2_channel_set_blocking(channel, 0); + + char b[1024]; + int r; + do + { + memset(&b, '\0', sizeof(b)); + sleep(1); + r = libssh2_channel_read(channel, b, sizeof(b)); + NSLog(@"b(%i) = %s", r, b); + } while (r > 0); + + char *cmd = "touch /u17/ugrad/rsesek/temp.foo\r\n\0"; + NSLog(@"numbytes = %i", libssh2_channel_write(channel, cmd, sizeof(char) * strlen(cmd))); + + do + { + memset(&b, '\0', sizeof(b)); + sleep(1); + r = libssh2_channel_read(channel, b, sizeof(b)); + NSLog(@"b(%i) = %s", r, b); + } while (r > 0); + + [self setStatus:@"Printed!" isError:NO]; + + libssh2_channel_send_eof(channel); + libssh2_channel_eof(channel); + libssh2_channel_close(channel); shutdown: + if (channel) + { + libssh2_channel_free(channel); + channel = NULL; + } libssh2_session_disconnect(ssh, "Normal disconnect."); libssh2_session_free(ssh); -- 2.22.5