远程计算机上运行的猫,并使用期望发送输出变量 [英] Run cat on remote computer and send output a variable using expect

查看:192
本文介绍了远程计算机上运行的猫,并使用期望发送输出变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也有通过ssh连接到远程补偿一个bash + expect脚本(我不能使用SSH密钥,需要在这里密码识别),读取该文件存在,查找特定符合主机名 (如主机名aaaa1111)与此主机名存储到变量而后使用。我怎样才能获得主机名参数的值?我认为该行的内容将在$ expect_out(缓冲)变量(这样我就可以对其进行扫描和分析),但事实并非如此。我的脚本是:

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:

    #!/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

当我试试,看看行变量,我只看到这一点:行=,expect_out(缓冲)=(缓冲)什么是获得该行的正确方法文件入变量?
或者是有可能打开该文件的在远程计算机上的期待与,扫描文件,并得到什么,我需要的变量?
在这里, http://en.wikipedia.org/wiki/Expect 有一个例子:

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 computer with expect, scan the file and get what i need to the variable? Here http://en.wikipedia.org/wiki/Expect there is an example:

    # 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?

推荐答案

您可能只是想尝试做这一切的期望,因为期望能控制的是bash。

You might just want to try and do it all from expect, as expect can control 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 "$0" "$@"


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

这篇关于远程计算机上运行的猫,并使用期望发送输出变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆