Linux Expect Expect_out(buffer) 不包含任何内容

发布于 2024-11-03 09:22:49 字数 715 浏览 0 评论 0原文

我一直在尝试捕获 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜点 2024-11-10 09:22:49

该代码真是一团糟。特别是,bash 和 Expect/tcl 之间的交互会给您带来麻烦,因为当 bash 看到它不知道的变量 $var 时,它会用空字符串替换它。

虽然您可以通过更改引用方式来更新内容,但实际上最好重写内容以实际使用直接的 Expect/tcl 脚本,如下所示:

#!/usr/bin/env expect

set username "hoge"
set password "hoge"
set hostname "machine20"

set prompt "\[$username@$hostname ~\]$"

set timeout -1
spawn ssh -l $username $hostname
expect {
    "$username@$hostname's password:" {
        send "$password\r"
    }
    "Are you sure you want to continue connecting (yes/no)?" {
        send "yes\r"
        exp_continue
    }
}

# These next two lines look suspicious, BTW...
expect "$prompt"
sleep 2

expect "$prompt"
send "ps axuw | grep java | grep -vc grep\r"

expect -re -indices "(.*)"
send "echo result : $expect_out(buffer)"

但是,我实际上将远程主机配置为使用用于登录的 RSA 密钥(事实上,我将远程主机配置为使用它们,因为它们比密码更能抵抗攻击并且也更易于管理),然后执行此操作(使用当地的grep 因此不需要过滤):

ssh $username@$host ps axuw | grep java

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:

#!/usr/bin/env expect

set username "hoge"
set password "hoge"
set hostname "machine20"

set prompt "\[$username@$hostname ~\]$"

set timeout -1
spawn ssh -l $username $hostname
expect {
    "$username@$hostname's password:" {
        send "$password\r"
    }
    "Are you sure you want to continue connecting (yes/no)?" {
        send "yes\r"
        exp_continue
    }
}

# These next two lines look suspicious, BTW...
expect "$prompt"
sleep 2

expect "$prompt"
send "ps axuw | grep java | grep -vc grep\r"

expect -re -indices "(.*)"
send "echo result : $expect_out(buffer)"

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):

ssh $username@$host ps axuw | grep java
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文