Chinaunix首页 | 论坛 | 博客
     该分类下暂时没有公开的博文,给他留言提醒一下吧。
给主人留下些什么吧!~~

totola1472015-10-29 21:38

我想自己实现expect  但是失败了   

-----------------------------------
spawn(){

if [ ! -p p_input ];then
        mkfifo p_input
fi

if [ ! -p p_output ];then
        mkfifo p_output
fi

echo ok
local v_command
v_command="$1"
$v_command >p_output  &

}

expect(){

local v_str
read v_str < p_output
echo "$v_str"

}

send(){

local v_str
v_str=$1
echo "send"
echo "$v_str">p_input 
echo "send finished"
}


spawn "ssh -t -t root@192.168.1.166"  

message=$(expect)


echo "message:$message"

send "123456"

回复  |  举报

totola1472015-10-29 17:44

改用fd 的脚本执行实现不了该功能

father(){

exec 9<&0
read -u9 str 
echo "str:$str"
echo "finished"

}


child(){

exec 9>&1
while true
do
echo ok >&9
sleep 1
done
}


child &

father &
~

回复  |  举报

totola1472015-10-29 16:38

--------------------------为了避免超出500字  分隔符   -------------------------------------------


我的问题是,这里父shell 子shell 对话 使用的是命名管道,觉得不够优雅,我想通过fd方式实现,但是不成功,我对fd的操作的理解还是比较浅,
通过fd的方式我的设想是   
比如说,将父shell的标准输入0 重定向为 10  子shell的标准输出 重定向为10;
将父shell的标准输出重定向为11 子shell的标准输入重定向为 11;

用文件描述符 10  11  来修改父shell 子shell 输入输出的行文来实现对话   ;相比于命名管道,没有创建管道文件,避免了管道文件的管理;

父shell中
exec 10<&0
read -u10 
echo xxx >11

子shell中
exec 1 >&10 
read -u11 
echo xxx>10

但是程序总是报错 您能否指导一下  ;
感谢您的帮助; 147zhangzhe@163.com

回复  |  举报

totola1472015-10-29 16:38

大家问的都是expect指令 刚好我也要问   请问expect 实现原理是怎样的  是C语言写的吗     我猜测是  父子线程对话的方式实现   将会有交互操作的执行指令放到子shell里执行 并将标准输出重定向给父shell  父shell 接收到子shell的信息 做逻辑判断,依据输入在反馈给子shell ; 父shell 子shell就像对话模式一样

我写了一个基于这个对话方式的测试的demo

if [ ! -p test1 ];then
        mkfifo test1
fi
if [ ! -p test2 ];then
        mkfifo test2
fi

father(){
local str
while true
do
        read str <test1
        echo "$str"
        sleep 1
        echo "hello,child" >test2

done

}

child(){
local str
echo "hello,Dad" >test1
while true
do
        read str <test2
        echo "$str"
        sleep 1
        echo "hello,Dad" >test1

done

}

father &
child &
--------------------------为了避免超出500字  分隔符   -------------------------------------------

回复  |  举报

ocean_mk2015-02-01 20:25

您好,新人求教,我写了一个expect的shell脚本,当在当前终端控制台执行的时候没有问题,但是一旦使用crontab定时任务之后,就会报
spawn id exp3 not open while executing的错误,我加了您好友,望指导

回复  |  举报
留言热议
请登录后留言。

登录 注册