Linux Expect Expect_out(buffer) 不包含任何内容
我一直在尝试捕获 grep 的结果,登录到远程计算机,在 Expect 命令中使用 ssl 。 我读了“ except_out(buffer)”变量来包含生成进程的输出,但它似乎是空的...... 指针将不胜感激!
#!/bin/bash
username=hoge
password=hoge
hostname=machine20
prompt="\[$username@$hostname ~\]$"
expect -c "
set timeout -1
spawn ssh -l $username $hostname
expect {
\"$username@$hostname's password:\" {
send \"$password\n\"
} \"Are you sure you want to continue connecting (yes/no)?\" {
send \"yes\n\"
expect \"$username@$hostname's password:\"
send \"$password\n\"
}
}
expect \"$prompt\"
sleep 2
expect \"$prompt\"
send \"ps axuw | grep java | grep -vc grep\n\"
expect -re -indices \"(.*)\"
send \"echo result : $expect_out(buffer)\"
预计版本:5.43.0
I've been trying to capture the result of grep, logging into a remote machine, using ssl in Expect command.
I read "except_out(buffer)" variable to contain the output of the spawned process, but it seemed empty...
A pointer'd be greatly appreciated!
#!/bin/bash
username=hoge
password=hoge
hostname=machine20
prompt="\[$username@$hostname ~\]$"
expect -c "
set timeout -1
spawn ssh -l $username $hostname
expect {
\"$username@$hostname's password:\" {
send \"$password\n\"
} \"Are you sure you want to continue connecting (yes/no)?\" {
send \"yes\n\"
expect \"$username@$hostname's password:\"
send \"$password\n\"
}
}
expect \"$prompt\"
sleep 2
expect \"$prompt\"
send \"ps axuw | grep java | grep -vc grep\n\"
expect -re -indices \"(.*)\"
send \"echo result : $expect_out(buffer)\"
expect version : 5.43.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该代码真是一团糟。特别是,bash 和 Expect/tcl 之间的交互会给您带来麻烦,因为当 bash 看到它不知道的变量
$var
时,它会用空字符串替换它。虽然您可以通过更改引用方式来更新内容,但实际上最好重写内容以实际使用直接的 Expect/tcl 脚本,如下所示:
但是,我实际上将远程主机配置为使用用于登录的 RSA 密钥(事实上,我将远程主机配置为仅使用它们,因为它们比密码更能抵抗攻击并且也更易于管理),然后执行此操作(使用当地的
grep
因此不需要过滤):That code is a real mess. In particular, you've got interactions between bash and expect/tcl which are causing you trouble because when bash sees
$var
for a variable it doesn't know, it replaces it with the empty string.While you could update things by changing how you do quoting, it's actually better to rewrite things to actually use a direct expect/tcl script, like this:
However, I'd actually configure the remote host to use RSA keys for logins (indeed, I'd configure the remote host to only use them as they're much more attack-resistant than passwords and easier to manage too) and then just do this (with a local
grep
so it doesn't need to be filtered):