bash 在远程计算机上运行 cat 并使用 expect 发送输出变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7043690/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:34:36  来源:igfitidea点击:

Run cat on remote computer and send output a variable using expect

bashexpectcat

提问by lugger1

I have a bash+expect script which has to connect via ssh to the remote comp (and i can't use ssh keys, need password identification in here), read the file there, find specific line with the "hostname" (like "hostname aaaa1111") and store this hostname into the variable to be used after while. How can i get the value of the "hostname" parameter? I thought that line content will be in $expect_out(buffer) variable (so i can scan it and analyze), but it's not. My script is:

我有一个 bash+expect 脚本,它必须通过 ssh 连接到远程 comp(我不能使用 ssh 密钥,这里需要密码识别),读取那里的文件,找到带有“主机名”的特定行(如“主机名 aaaa1111") 并将此主机名存储到变量中,以便在之后使用。如何获取“主机名”参数的值?我认为该行内容将在 $expect_out(buffer) 变量中(因此我可以对其进行扫描和分析),但事实并非如此。我的脚本是:

    #!/bin/bash        
    ----bash part----
    /usr/bin/expect << ENDOFEXPECT
    spawn bash -c "ssh root@$IP"  
    expect "password:"
    send "xxxx\r"
    expect ":~#"
    send "cat /etc/rc.d/rc.local |grep hostname \r"
    expect ":~#"
    set line $expect_out(buffer)
    puts "line = $line, expect_out(buffer) = $expect_out(buffer)"
    ...more script...
    ENDOFEXPECT

When i try to see line variable, i see only this: line = , expect_out(buffer) = (buffer)What is the right way to get the line from the file into the variable? Or is it possible to open the file on the remote computerwith expect, scan the file and get what i need to the variable? Here http://en.wikipedia.org/wiki/Expectthere is an example:

当我尝试查看行变量时,我只看到以下内容:line = , expect_out(buffer) = (buffer)将行从文件中获取到变量的正确方法是什么?或者是否可以使用expect在远程计算机上打开文件,扫描文件并获取我需要的变量?这里http://en.wikipedia.org/wiki/Expect有一个例子:

    # Send the prebuilt command, and then wait for another shell prompt.
    send "$my_command\r"
    expect "%"
    # Capture the results of the command into a variable. This can be displayed, 
    set results $expect_out(buffer)

seems that it doesn't work in this case?

似乎在这种情况下不起作用?

回答by Sander van Knippenberg

You might just want to try and do it all from expect, as expect can control bash.

您可能只想尝试从 expect 开始做这一切,因为 expect 可以控制 bash。

The following should do what you've described. Not sure if this is exactly what you are trying to do.

以下应该做你所描述的。不确定这是否正是您想要做的。

#!/bin/sh
# the next line restarts using tclsh \
exec expect "##代码##" "$@"


spawn bash 
send "ssh root@$IP\r"
expect "password:"
send "xxxx\r"
expect ":~#"
send "cat /etc/rc.d/rc.local |grep hostname \n"
expect ":~#"
set extractedOutput $expect_out(buffer)
set list [split $extractedOutput "\n"]
foreach line $list {
    set re {(?x)
        .*
        (*)              
        -S.*
    }
    regexp $re $line total extractedValue
    if {[info exists extractedValue] && [string length $extractedValue] > 1} {
        set exportValue $extractedValue
        break    # We've got a match!
}

send "exit\r" # disconnect from the ssh session

if {[info exists exportValue] && [string length $exportValue] > 1}{
    send "export VARIABLE $exportValue\r"
} else {
    send_user "No exportValue was found - exiting\n"
    send "exit\r"
    close
    exit 1
}

# now you can do more things in bash if you like