bash - 预期脚本 : to perform actions after closing ssh

标签 bash expect

问题是在 bash 内的 Expect 脚本中关闭 ssh 后保留变量并执行操作。

这是我到目前为止所得到的:

echo "Getting package name..."

getPackageName=$(expect -c '
exp_internal 1
log_user 1
global expect_out

# puts "Getting package name..."

spawn ssh -q -o StrictHostKeyChecking=no -o PreferredAuthentications=password -o PubkeyAuthentication=no -o RSAAuthentication=no -l user 10.20.30.40
sleep 1

expect {
    "*sword*" {
        send "12341234\r"
    }
    timeout {
        send_user "Error: timeout\n"
        exit 1
    }
}

expect {
    "*user@*>*" {
        # getting name of the latest modified file
        send "cd /export/home/user/Releases/build/1.3.32.0 && find * -type f -printf '"'"'%T@ %p\\n'"'"' | sort -n | tail -1 | cut -f2- -d\" \"\r"
    }
    timeout {
        send_user "Error: timeout\n"
        exit 1
    }
}

expect {
    "BUILD_MAIN*" {
        # assigning value to variable
        set result_lines [split $expect_out(0,string) \r\n]
        set package_filename [lindex $result_lines 0]
        puts "package_filename: $package_filename"
    }
    timeout {
        send_user "Error: timeout\n"
        exit 1
    }
}

expect "*#"
send "exit\r"

# here I need to perform some actions on local machine after ssh logout
expect "Connection*"
send "export LATEST_BUILD=$package_filename\r"
send_user "Message sent to user"

')

因此,在底部 block 中,我尝试在关闭 ssh 后在本地计算机上设置环境变量 (LATEST_BUILD),并粘贴之前在 ssh session 期间定义的变量值 (package_filename)。

这里的要点是,我在输出中看到最后一个“消息发送给用户”,但之前的发送“export LATEST_BUILD=12345\r”显然不起作用。

最佳答案

#!/bin/bash

getPackageName=$(expect -c '

# A common prompt matcher
set prompt "%|>|#|\\\$ $"

# To suppress any other form of output generated by spawned process
log_user 0   

### Spawning ssh here ###
spawn ssh <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4e3b3d2b3c0e3636366036366036363660363636" rel="noreferrer noopener nofollow">[email protected]</a>
expect "password"
send "welcome!2E\r"
expect -re $prompt  

# Your further code 

send "exit\r"
expect eof

##### The below segment is not needed ######
##### if your intention is to get only the 'package_filename' value #####
#    spawn bash
#    expect -re $prompt
#    send "export LATEST_BUILD=54.030\r"
#    expect -re $prompt
#    send "echo \$LATEST_BUILD\r"
#    expect -re $prompt
#    send "exit\r"
#    expect eof
#
##### The End ######


# Enabling logging now ...  
log_user 1
# Print only the value which you want to return
puts "$package_filename"
')

echo $getPackageName

eof 用于标识文件结束事件,即连接关闭。

注意:导出的变量LATEST_BUILD仅可用于生成的bash session 。

更新:

log_user 用于随时关闭/打开 Expect 生成的日志记录。

log_user 0; # Turn off logging
log_user 1; # Turn on logging

我希望您的唯一目的是获取package_filename。因此,我们甚至不需要生成 bash shell。相反,只需最后打印该值,从而使其可供父 bash 脚本使用。

关于bash - 预期脚本 : to perform actions after closing ssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33866756/

相关文章:

linux - 在 nc 连接和括号中的命令中使用来自管道命令的标准输出

python - Tkinter 在 docker 中安装

bash读取神秘: `read -d' ' -s -n 1` eats hyphens

ssh - 没有超时退出期望{}

ssh - 我是否可以使用Expect SSH进入计算机并使用单个bash行进行交互?

tcl - 期望脚本,if 的两个分支都不会被执行

linux - bash 中的错误 - 预期存在 -e 的一元运算符

linux - TC 带宽限制脚本错误

linux - 将 puts 的输出同时发送到日志文件和标准输出?

Linux 退出前等待