gpt4 book ai didi

linux - 如何关闭 Expect 脚本中的 stderr 或退出后停止 "Connection to ... closed"消息?

转载 作者:太空宇宙 更新时间:2023-11-04 12:55:09 26 4
gpt4 key购买 nike

所以我一直在尝试使用 expect 学习 bash 脚本,但我遇到了一个烦人的问题,即使我通过将 log_user 设置为 0 来关闭输出,退出 ssh 服务器时的注销消息仍然打印为“登出连接到 blah.blah.blah 已关闭”。采用 hackish 方式并在脚本中发出 exit 命令是不可取的,并且忽略了在终端中打印新行,留下难看的终端提示。我如何抑制或在我的期望脚本中关闭此连接关闭消息?这是我的期望脚本的代码模板:

#!/usr/bin/expect -f
#-f flag tells expect to read commands from file

# Set Password Prompt and Password
set PASSP "password:"
set PASSW "your_password_here\n"

# Set SFTP Prompt
set SFTP "sftp> "

# Set Remote Prompt and Project Directories
# The GL server is super weird, so make sure RPROMPT ends in the
# name of the project compilation directory and PROJDIR is the
# relative path (from home but without the tilde) of the main
# project directory.

set RPROMPT "projdir_here]"
set PROJDIR "path_to_projdir_here"

# Set SSH Address
set ADDRESS your_username_here@your_address_here

# Annoying long output turned off
log_user 0

send_user "Logging in...\n"

spawn sftp $ADDRESS
expect $PASSP
send $PASSW

expect $SFTP
send_user "Uploading necessary files...\n"

send "cd $PROJDIR\n"
expect $SFTP
send "put *.cpp .\n"
expect $SFTP
send "put *.h .\n"
expect $SFTP
send "put Makefile .\n"
expect $SFTP
send "exit\n"

spawn ssh $ADDRESS
expect $PASSP
send $PASSW
expect "]"
send "cd $PROJDIR\n"

expect $RPROMPT
send_user "Cleaning remote directory...\n"

send "make clean\n"

# Output turned on temporarily so compiler feedback can be determined
expect $RPROMPT
log_user 1
send_user "Compiling on server...\n"

send "make\n"

# Turn off output and submit (this assumes that your makefile has a 'submit'
# target with the command to submit your file there)
expect $RPROMPT
log_user 0
send_user "\nSubmitting Requisite Files...\n"

send "make submit\n"

expect $RPROMPT
send_user "Quitting...\n"

# I can't figure out how to turn off stderr in expect so that an annoying 'connection closed'
# message is printed even the log_user has been set to 0
send "logout\n"

# Transfer control to user
interact

您可以看到,在程序结束时,在我使用交互命令之前,在发送“注销\n”并关闭与服务器的连接后打印了违规消息。尽管上面将 log_user 设置为 0,但仍会发生这种情况。我究竟该如何关闭脚本中的 stderr 输出?我什至在 expect man page 查看了 expect 的手册页。但那里似乎没有多大用处。我不明白如何在 expect 脚本中关闭 stderr 以便不打印此消息,将不胜感激!我真的只是希望脚本在我注销时保持安静。因为很明显我发出了注销命令,所以我不确定为什么这个输出会转到 stderr。 ssh 中是否有一条命令在用户退出时不会输出“Connection to...closed”消息?如果我可以通过管道将 stderr 遗忘或以某种方式抑制它会更好。常规方法似乎不起作用,因为这是一个 expect 脚本。

最佳答案

只是log_user的错误使用。它应该用作,

log_user 0
send "logout\r"
expect eof
log_user 1

请注意,我在发送 logout\r 后期待 eof 模式,因为它会导致连接关闭。在使用 send 命令时,始终使用 \r 代替 \n

阅读here了解有关 log_user 的更多信息以抑制输出。

与其提示定义为静态字符串,不如将其概括为,

# We escaped the `$` symbol with backslash to match literal '$' 
# The last dollar sign represents line-end.
set prompt "#|>|%|\\\$ $";

当使用 expect 时,我们必须伴随 -re 标志以将其指定为正则表达式。

expect -re $prompt

SshDemo.exp

#!/usr/bin/expect 
set prompt "#|>|\\\$ $"
spawn ssh dinesh@myserver
expect {
"(yes/no)" { send "yes\r";exp_continue}
"password"
}
send "welcome123\r"
expect -re $prompt
# Simply executing 'ls -l' command...
send "ls -l\r"
expect -re $prompt
log_user 0
send "logout\r"
expect eof
log_user 1

关于linux - 如何关闭 Expect 脚本中的 stderr 或退出后停止 "Connection to ... closed"消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35966497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com