UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2017-11-14 10:45 AM

xdev
Member
Registered: 2017-11-14
Posts: 2

ssh interaction using libexpect and c

Hi,

I have created a c program using expect to login a server using ssh, the following is the program

#include <sys/wait.h>
#include <tcl8.6/expect.h>
int main()
{
exp_is_debugging = 0;
exp_timeout = 60;


FILE *expect = exp_popen((char *) "ssh user@ip");
if (expect == 0) {
    return 1;
}

enum { denied, invalid, command_not_found, 
        command_failed, prompt };

switch (exp_fexpectl(expect,
            exp_glob, "password: ", prompt,
            exp_end)) {
    case prompt:
            // continue 
            break;
    case EXP_TIMEOUT:
            return 1;
}

   fprintf(expect, "%s\r","password");

 switch (exp_fexpectl(expect,
            exp_glob, "denied", denied, // 1 case       
            exp_glob, "invalid password", invalid, // another case
            exp_glob, "#", prompt, // third case           
            exp_end)) {
    case denied:
    printf("denied %d",exp_glob); 
             break;
    case invalid:
            break;
    case EXP_TIMEOUT:
    printf("EXP_TIMEOUT"); 
            break;
    case prompt:
            printf("loggedin");        
            break;
    default:
    //return 0;
            break;
}
}

I am able to login to the server but it immediately gone to my terminal after this, what i am looking to login to the server and need to interact with that terminal. Now, it is showing my terminal after that, do anyone have any idea on this ? i need to stay on that logged server's terminal

Offline

Board footer

Powered by FluxBB