所以我一直在尝试用期望来学习bash脚本,并且我遇到了一个烦人的问题,即使在我通过将log_user设置为0来关闭输出之后,当我退出ssh服务器时的注销消息仍然打印为“登出 与blah.blah.blah的连接关闭“。以hackish方式并在脚本中发出退出命令以期望是不可取的,并且忽略在终端中打印新行,留下难看的终端提示。我如何抑制或在我的expect脚本中关闭此连接已关闭的消息?这是我的expect脚本的代码模板:
#!/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
您可以看到在程序结束时,在使用interact命令之前,在发送“logout \ n”并且关闭与服务器的连接之后打印出有问题的消息。尽管log_user设置为0,但仍会发生这种情况。我如何在脚本中关闭stderr输出?我甚至在expect man page查看了预期的手册页,但那里似乎没什么用处。我不明白如何关闭期望脚本中的stderr,以便不打印此消息,非常感谢帮助!我真的只是希望在我退出时脚本安静。因为很明显我发出了一个logout命令,所以我不确定为什么这个输出会转到stderr。在用户退出时,ssh中是否有一个命令不会输出“Connection to ... closed”消息?如果我能将stderr管道遗忘或以某种方式压制它会更好。正常的方式似乎不起作用,因为这是一个期望脚本。
答案 0 :(得分:0)
这只是对log_user
的错误使用。它应该用作,
log_user 0
send "logout\r"
expect eof
log_user 1
请注意,我发送eof
后期望logout\r
模式,因为它会导致连接关闭。使用\r
命令时,始终使用\n
代替send
。
阅读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
答案 1 :(得分:0)
这是我基于上述建议的新改进脚本。我还决定使用scp而不是sftp,因为它更简洁,我只上传文件。
#!/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"
# Set Remote Prompt and Project Directories
# The ssh server is super weird, so make sure PROJDIR is the
# directory name of the main project directory and PATH is
# the relative path to the project directory from home (with the tilde)
set PROJDIR "your_projdir_name_here"
set PATH "your_projdir_path_here"
# Set SSH Address
set ADDRESS your_username_here@source_hostname_here
# Turn off annoying output
log_user 0
send_user "Logging in and uploading necessary files...\n"
# upload files via scp, (shows upload prompt for user, concise)
spawn bash -c "scp *.cpp *.h Makefile $ADDRESS:$PATH"
expect $PASSP
send "$PASSW\r"
interact
spawn ssh $ADDRESS
expect $PASSP
send "$PASSW\r"
expect "]"
send "cd $PATH\r"
expect "$PROJDIR]"
send_user "\nCleaning remote directory...\n"
send "make clean\r"
# Output turned on temporarily so compiler feedback can be determined
expect "$PROJDIR]"
log_user 1
send_user "Compiling on server...\n\n"
send "make\r"
# Turn off output and submit (this assumes that your makefile has a 'submit'
# target with the command to submit your file there)
expect "$PROJDIR]"
log_user 0
send_user "\n\nSubmitting Requisite Files...\n"
send "make submit\r"
expect "$PROJDIR]"
send_user "Quitting...\n"
send "logout\r"
expect eof
# Exit
exit