Call “expect” script in C++ process

爱⌒轻易说出口 提交于 2019-12-08 12:17:34

问题


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)



来源:https://stackoverflow.com/questions/7027151/call-expect-script-in-c-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!