linux expect 实例

来源:互联网 发布:python ppt 编辑:程序博客网 时间:2024/06/04 19:20

1.expect如何读取一行一行读取文件

#!/usr/bin/expect -f 
set timeout -1

set ipfd [open "./iphost.txt" r]
while {1} {
    set sship [gets $ipfd]
    if {[eof $ipfd]} {
        close $ipfd
        send_user "close fd \n"
        break
    }


    # ip proc
    puts stdout $sship    
}

#expect eof  #添加该行会因为之前设置了无限超时而不能执行

2. expect 发送命令导致脚本无法继续运行

#!/usr/bin/expect
set timeout -1

set sship "127.0.0.1"

spawn sudo ssh $sship
expect "#"
send "cd libs \r"
expect "libs]#"
send "make > $sship.log 2>&1 \r"              # send "make \r"使得远程服务器返回大量交互信息,导致ssh 链路断开,后续脚本无法执行;解决方法采用重定向信息到日志文件
expect "libs]#"

send_user " make ok \n"




0 0
原创粉丝点击