简体   繁体   English

使用libexpect通过bash进行c sshfs自动连接

[英]c sshfs automatic connection thru bash using libexpect

I want to automate the mounting of a sshfs connection inside my C program (meant for linux) using only password for ssh. 我想只使用ssh的密码自动在我的C程序(Linux意味着)中自动安装sshfs连接。 (I don't want to use public/private keys.) (我不想使用公钥/私钥。)

It seems i could do it with libexpect. 看来我可以用libexpect做到。 Based on this thread it seems possible to use libexpect to give the password when required. 基于此线程 ,在需要时似乎可以使用libexpect来提供密码。 For example, i made a successful test program that creates a dir with sudo mkdir test and then provide the user password with libexpect. 例如,我制作了一个成功的测试程序,该程序使用sudo mkdir test创建一个目录,然后为用户密码提供libexpect。 So that pipe seems really to be bidirectional. 因此,该管道似乎确实是双向的。 Thou, with sshfs and my simple function below, it doesn't work: i do have a sshfs mount produced by it, but it doesn't work and i can't see the files. 您使用sshfs和下面的简单函数无法正常工作:我的确有一个sshfs挂载,但是它不起作用并且我看不到文件。

enum{ASKCONTINUE,ASKPWD};

int sshfs_connect (char data[10][200], char ip[]) {

    char commandline[300];
    sprintf(commandline,"sshfs %s@%s:%s %s -p %s -o ServerAliveInterval=15",
            data[4],ip,data[6],data[5],data[7]);

    bool shouldBreak = false;

    FILE* fp = exp_popen(commandline);

    while(shouldBreak == false)
    {
        switch(exp_fexpectl(fp,
              //if asked for continuing (authenticity can't be established)
              exp_glob, "ontinue connecting (yes/no)?", ASKCONTINUE, 
              //if asked for pwd 
              exp_glob, "s password:", ASKPWD,  
              exp_end))  //
        {
            case ASKCONTINUE:
                printf("asked continue? !\n");
                fprintf(fp,"%s\n","yes");
                break;

            case ASKPWD:
                printf("asked pwd ! sending pwd: %s\n",data[8]);
                fprintf(fp,"%s\n",data[8]);
                shouldBreak = true;
                break;

            case EXP_TIMEOUT:
                shouldBreak = true;
                break;

            case EXP_EOF:
                shouldBreak = true;
                break;
        }
    }

    fclose(fp);
    return 0;

}

It seems that expect (even the tcl version ) cannot be used with sshfs , probably because the spawned process does not have a controlling tty and so the command tries to get the password from the user by other means, eg using a gui popup. 似乎expect (甚至是tcl版本 )不能与sshfs一起使用,可能是因为生成的进程没有控制tty,因此该命令尝试通过其他方式(例如,使用gui弹出窗口)从用户获取密码。

What works is adding the option -o password_stdin to ask sshfs to read the password from stdin. 起作用的是添加选项-o password_stdin以要求sshfs从stdin读取密码。 It does this without writing a prompt, so libexpect is a bit heavyweight, but nonetheless this should work for you: send the password and wait for eof (which would probably be within the default timeout of 10 seconds). 它无需编写提示即可执行此操作,因此libexpect有点libexpect ,但这仍然可以为您工作:发送密码并等待eof(这可能在默认超时10秒之内)。

ssh = exp_popen("sshfs -o password_stdin user@host:/dir /mntpoint");
if(ssh==NULL)fatal("fail to spawn");
fprintf(ssh, "%s\n", password);
switch(exp_fexpectl(ssh,exp_end)) {
case EXP_EOF:
    break;  // ok
case EXP_TIMEOUT:
    break;  // fail
default:
    break;  // fail
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM