Bash Linux Expect_out(缓冲区)不包含任何内容

Bash Linux Expect_out(缓冲区)不包含任何内容,bash,ssh,tcl,expect,sh,Bash,Ssh,Tcl,Expect,Sh,我一直在尝试捕获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 $hos

我一直在尝试捕获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,该代码真是一团糟。特别是,bash和expect/tcl之间的交互给您带来了麻烦,因为当bash看到它不知道的变量
$var
时,它会用空字符串替换它

虽然您可以通过更改引用方式来更新内容,但实际上最好重写内容以实际使用direct 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密钥进行登录(事实上,我会将远程主机配置为仅使用RSA密钥,因为它们比密码更具抗攻击性,也更易于管理),然后执行此操作(使用本地
grep
,因此不需要过滤):


也许你会感兴趣。不要逃避那些双引号。您希望提示连续出现两次,中间只有一个睡眠时间,这当然是正确的wrong@glenn:这是因为它都在bash脚本中,这会使事情变得更加复杂。嗨,Donal,谢谢你的评论。它真的帮了我。。。正如你所说,我终于用RSA密钥重写了这段代码。
ssh $username@$host ps axuw | grep java