Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

channel:(NMSSHChannel *)channel didReadData:(NSString *)message only called once on creation #20

Closed
devdavidkarlsson opened this Issue · 9 comments

3 participants

@devdavidkarlsson

When a session is created, a shell is started and commands are written: no responses are recieved and the callback methods for the buffer is never called:
(Executing a single command using channel:execute works)

  -(void) createSessionWithAdress:(NSString*)address username:(NSString*)user   password:(NSString*)pass{
      session = [NMSSHSession connectToHost:address withUsername:user];
      if (session.isConnected) {
        [session authenticateByPassword:pass];
      if (session.isAuthorized) {
          NSError *err = nil;
          session.channel.delegate = self;
          //self.receiveView.text = [session.channel execute:@"ls" error:&err]; // works

          [session.channel startShell:&err];
          NSLog(@"Authentication succeeded");
       }
    }
}




- (void)channel:(NMSSHChannel *)channel didReadData:(NSString *)message{
    NSLog(@"Read data!");
    receiveView.text = [NSString stringWithFormat:@"%@   \n%@",receiveView.text,message];
}

- (void)channel:(NMSSHChannel *)channel didReadError:(NSString *)error{
    receiveView.text = [NSString stringWithFormat:@"%@    \n%@",receiveView.text,error];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    NSLog(@"RETURN PRESSED");
    NSError* err = nil;

    bool commandSucess = [session.channel write:sendView.text error:&err];
    [session.channel write:@"/n" error:&err];

    if (commandSucess) {
        NSLog(@"Command written successfully");
    }else{
        NSLog(@"Command not written successfully");
    } 
    return YES;
}
@Lejdborg
Owner

I'm not completely sure since I've not contributed anything to the shell code myself, but I think you missed to set session.channel.requestPty = YES

@Lejdborg Lejdborg closed this
@devdavidkarlsson

Updated the code as requested with no success :-/

    -(void) createSessionWithAdress:(NSString*)address username:(NSString*)user password:(NSString*)pass{
        session = [NMSSHSession connectToHost:address
                         withUsername:user];
        [session.channel setRequestPty:YES];
        [session.channel setPtyTerminalType:NMSSHChannelPtyTerminalAnsi];
        session.channel.delegate = self;

        if (session.isConnected) {
        [session authenticateByPassword:pass];
        if (session.isAuthorized) {
            NSError *err = nil;
            [session.channel startShell:&err];  
            NSLog(@"Authentication succeeded");
        }
    }
}
@Frugghi
Collaborator

Hey David probably the error is
[session.channel write:@"/n" error:&err];
it should be
[session.channel write:@"\n" error:&err];

@devdavidkarlsson

Did not help either.

When the writing is done, nothing else happens. I want the callbacks to receive and handle some kind of response from the server. However, the writing is not followed by anything, see my NSLog below. Again using the execute command works, but i need the shell.

2013-06-30 14:47:42.500 hackers_ssh[95486:c07] RETURN PRESSED
2013-06-30 14:47:42.500 hackers_ssh[95486:c07] NMSSH: Writing 'ls' on shell
2013-06-30 14:47:42.501 hackers_ssh[95486:c07] NMSSH: Writing '' on shell
2013-06-30 14:47:42.502 hackers_ssh[95486:c07] Command written successfully

@Lejdborg Lejdborg reopened this
@Frugghi
Collaborator

Maybe the ssh server you are connecting to use a different line-break? What OS is the ssh server running?

Otherwise can be a problem with the global queue, I replaced it in my last commit https://github.com/Frugghi/NMSSH

@devdavidkarlsson

The server is: Linux login 3.2.0-35-virtual #55-Ubuntu SMP Wed Dec 5 18:02:05 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux. In your commit: the app crashes at EXC_BAD_ACCESS at:

    // Fetch response from output buffer
    dispatch_async(self.channelQueue, ^{

In startShell, so i switched channelQueue from assign to strong (iOS 6.1 http://stackoverflow.com/questions/12511976/app-crashes-after-xcode-upgrade-to-4-5-assigning-retained-object-to-unsafe-unre), no error now but still not the expected functionality, outputs to log remains the same.

So what i tried next in NMSSH, ...channel.m

// Fetch response from output buffer
    NSLog(@"Dispatching channel queue");
    if (self.channelQueue == nil) {
        NSLog(@"NO QUEUE EXISTS ATTEMPTING TO CREATE");
        [self setChannelQueue:dispatch_queue_create("com.NMSSH.channelQueue", DISPATCH_QUEUE_CONCURRENT)];

    }
    dispatch_async(self.channelQueue, ^{
        NSLog(@"block running in background");

        for (;;) {
            NSLog(@"loop running in background");
            ......

NSLog gives:

2013-06-30 20:13:01.667 hackers_ssh[568:c07] NMSSH: SSH session started
2013-06-30 20:13:01.753 hackers_ssh[568:c07] NMSSH: User auth list: publickey,password
2013-06-30 20:13:01.961 hackers_ssh[568:c07] NMSSH: Starting shell
2013-06-30 20:13:02.402 hackers_ssh[568:c07] NMSSH: Shell allocated
2013-06-30 20:13:02.402 hackers_ssh[568:c07] Dispatching channel queue
2013-06-30 20:13:02.403 hackers_ssh[568:c07] Authentication succeeded
2013-06-30 20:13:02.403 hackers_ssh[568:1b03] block running in background
2013-06-30 20:13:02.403 hackers_ssh[568:1b03] loop running in background
2013-06-30 20:13:02.403 hackers_ssh[568:c07] Added: login.student.lth.se:22
2013-06-30 20:13:02.404 hackers_ssh[568:c07] 6
2013-06-30 20:13:03.273 hackers_ssh[568:c07] Storing history
2013-06-30 20:13:03.275 hackers_ssh[568:c07] stored latestArray
2013-06-30 20:13:05.763 hackers_ssh[568:c07] EDITING
2013-06-30 20:13:05.914 hackers_ssh[568:c07] EDITING
2013-06-30 20:13:07.465 hackers_ssh[568:c07] RETURN PRESSED
2013-06-30 20:13:07.465 hackers_ssh[568:c07] NMSSH: Writing 'ls' on shell
2013-06-30 20:13:07.466 hackers_ssh[568:c07] NMSSH: Writing '' on shell
2013-06-30 20:13:07.466 hackers_ssh[568:c07] Command written successfully

Nothing else, The loop should keep running in a background thread right? so it seems it gets interrupted somehow...

@devdavidkarlsson

Changing about a bit in NMSSH tells me the following: the loop stops at the lines:
rc = libssh2_channel_read(self.channel, buffer, (ssize_t)sizeof(buffer)-1);
erc = libssh2_channel_read_stderr(self.channel, errorBuffer, (ssize_t)sizeof(errorBuffer)-1);

This is how i prove it:

dispatch_async(self.channelQueue, ^{
    NSLog(@"background thread created to run in background");

    for (;;) {
        NSLog(@"loop running in background");
        long rc;
        long erc;
        char buffer[0x4000];
        char errorBuffer[0x4000];

        do {
            NSLog(@"do loop started");
            rc  = libssh2_channel_read(self.channel, buffer, (ssize_t)sizeof(buffer)-1);
            erc = libssh2_channel_read_stderr(self.channel, errorBuffer, (ssize_t)sizeof(errorBuffer)-1);

            NSLog(@"rc and erc assigned");

The issue I am experiencing sounds a little like: http://www.libssh2.org/mail/libssh2-devel-archive-2011-03/0042.shtml
But i guess the problem can be caused by something else to, like data/commands not being sent as it should to the server making no data appear att the blocked session (http://www.libssh2.org/libssh2_session_set_blocking.html).
However, wireshark tells me that atleast something is being sent, but if i send "mkdir MYSSHTEST", nothing is created on the serverside. So I guess there should be something wrong in the send part of the app i guess.

@Frugghi
Collaborator

Guess I found the error, the non-blocking mode was set only for keyboard-interactive authentication. If the issue is solved I'll submit the pull request.

@devdavidkarlsson

Correct, your fix did the trick! Thank you

@Lejdborg Lejdborg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.