Call “expect” script in C++ process

2019-06-13 16:46发布

问题:

I realized a shell using expect/spawn and send commands to SCP files from a remote server which send automatically the password when it is needed.

The script works fine on UNIX terminal.

Nevertheless, I tried to use this script throough a C++ process. It has been called by system() or even popen() function without sucess. This error is returned: "ioctl(raw): I/O error" Someone could have any clue?

This is my script:

 #!/bin/bash
 targetHost=$1
 password=$2
 sourceFile=$3

 destRep=$4       
 expect -c "        
        spawn /usr/bin/scp -q $targetHost:$sourceFile $destRep
        expect -i $spawn_id { 
          "*password:*" { send -i $spawn_id $password\r\n; interact } 
          eof { exit }
        }
        exit
        "

回答1:

The first thing I'd try is to ditch the bash script (there appear to be quoting issues anyway)

#! /usr/bin/env expect -f
foreach {targetHost password sourceFile destRep} $argv break
spawn /usr/bin/scp -q $targetHost:$sourceFile $destRep
expect -i $spawn_id { 
    "*password:*" { send -i $spawn_id $password\r; interact } 
    eof { exit }
}

But the real problem is how the stdio channels/pty get inherited by the expect process (I'm not sure of the proper terminology here)