Bash 期望脚本,发送命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14476103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash expect script, send command
提问by Michal B
I have the following script working. I would like to result command: $nazwa send -- "/system identity print\r" executed saved to a file, but it is probably badly written. At the moment, I can only write path to the file tmp.
我有以下脚本工作。我想结果命令: $nazwa send -- "/system identity print\r" 执行保存到一个文件,但它可能写得不好。目前,我只能写入文件 tmp.path 的路径。
#!/bin/bash
HOSTNAME="xx.xx.xx.xx"
PORT="22422"
USER="admin"
PASS="pass"
TMP=$(mktemp)
PATH="/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin"
# create expect script
cat > $TMP << EOF
#exp_internal 1 # Uncomment for debug
set timeout -1
spawn ssh -p$PORT $USER@$HOSTNAME
match_max 100000
expect -exact "password:"
send -- "$PASS\r"
expect " > "
$nazwa send -- "/system identity print\r"
expect " > "
send -- "quit\r"
expect eof
EOF
# run expect script
#cat $TMP # Uncomment for debug
expect -f $TMP
echo $TMP >> log.log
# remove expect script
rm $TMP
回答by glenn Hymanman
What is $nazwadoing there? Remove it:
什么是$nazwa做什么呢?去掉它:
send -- "/system identity print\r"
Since your are actually developing your script, uncomment exp_internal 1to get expect to help you.
由于您实际上是在开发脚本,因此取消注释exp_internal 1以获得期望对您有所帮助。

