【问题标题】:TCL Expect kills child of child processTCL Expect杀死子进程的子进程
【发布时间】:2018-01-16 15:06:47
【问题描述】:

我有一个像这样的期望脚本

#!/usr/bin/expect -f
set timeout 30
log_user 0

set PASSWORD $::env(PASSWORD)
set USERNAME $::env(USERNAME)
set TOKEN $::env(TOKEN)

puts stderr "Generating OTP"
spawn oathtool --totp $TOKEN
expect -re \\d+
set otp $expect_out(0,string)

puts stderr "Connecting to VPN server"
spawn -ignore HUP env openconnect -b https://vpn
expect "GROUP:"
send "Tech\n"
expect "Username:"
send "$USERNAME\n"
expect "Password:"
send "$PASSWORD\n"
expect "Password:"
send "$otp\n"
expect EOF

这个简单的脚本提供了 openconnect 的用户名和密码以在后台建立一个新的 VPN 连接,但它不会工作,因为子进程被期望杀死。如您所知,expect 将在完成之前发送 SIGHUP 信号,我试图解决它,但即使我放置了 -ignore HUP 标志,它也会杀死底层进程,我想结束我的脚本,但底层的 openconnect 在后台活下来。

你知道这里缺少什么吗?

考虑到 openconnect -b 将自己生成其他 PID。

【问题讨论】:

  • 这还不够清楚 - 你能详细说明一下吗:“但它不会工作,因为子进程被期望杀死,即使我放了 -ignore HUP 标志,它也会杀死这个底层进程"
  • 我用其他方法解决了这个问题,但我现在添加了更多细节,这个问题看起来更好吗?

标签: linux tcl expect openconnect


【解决方案1】:

以下使用 2 个批处理文件的方法对我有用:

未使用 openconnect 中的 -b 标志,而是使用 kill 命令将 openconnect 发送到后台。

名为 vpn2 的文件内容:

#!/usr/bin/expect -f
set timeout -1
spawn -ignore HUP -noecho /root/bin/v2vpn2 
expect "password"
sleep 3
send -- "my_password\r"
expect "SMS OTP"
interact 
expect "Established"
expect eof

名为 v2vpn2 的文件内容:

rm /var/log/vpn2.log > /dev/null 2>&1

touch /var/log/vpn2.log

# the word password is printed twice and so filtering here

tail -f /var/log/vpn2.log |  grep -m2 -wo "password" | sed '2q;d' &

tail -f /var/log/vpn2.log | grep --color=never -wo "SMS OTP" &

while /bin/true; do

        grep -q "Established" /var/log/vpn2.log 
        if (( $? == 0 )); then
                kill -STOP `pgrep openconnect` 
                kill -CONT `pgrep openconnect` 
                pkill vpn2
                exit
        fi
done & 

openconnect  -u "my_user_name"  my_vpn_url  >> /var/log/vpn2.log 2>&1

【讨论】:

    【解决方案2】:

    在这上面花了太多时间后,我通过添加解决了它

    expect -timeout -1 -ex "Client killed"
    

    并使用&调用脚本

    ./vpn.exp &
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 2014-09-30
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      相关资源
      最近更新 更多