调用“期望” C++ 中的脚本过程

发布于 2024-11-29 02:54:35 字数 564 浏览 1 评论 0原文

我使用expect/spawn实现了一个shell,并将命令从远程服务器发送到SCP文件,该服务器在需要时自动发送密码。

该脚本在 UNIX 终端上运行良好。

尽管如此,我还是尝试通过 C++ 过程使用此脚本。它已被 system() 甚至 popen() 函数调用但没有成功。 返回此错误:“ioctl(raw): I/O error” 有人可以有任何线索吗?

这是我的脚本:

 #!/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
        "

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
        "

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2024-12-06 02:54:35

我尝试的第一件事是放弃 bash 脚本(无论如何似乎都存在引用问题)

#! /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 }
}

但真正的问题是 stdio 通道/pty 如何被期望进程继承(我不确定这里的正确术语)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文