-1

Thanks.

I am new to expect script. Am trying to pass a "host" variable value into expect but getting error. Am not able to find how to handle this.

I take hostname value and save it variable called "host" and then do ssh $host using expect.

a. some remote host's key would have got already in know_hosts file so that time, when we do ssh $host, we get directly prompt (#)

Am trying to handle this in expect 1st regex line

b. other scenario is that, remote hosts key won't present in know_hosts file, that time when we do ssh $host, it will get prompt asking for password:

that am trying to handle in 2nd regex in expect

when I run the script, am getting error saying :

**

can't read "host": no such variable
    while executing
"set cmd ssh $host"

**

bash script :

    #!/usr/bin/bash

    host=`echo $REMOTE_HOSTNAME`
    export host

# logic to add remote hosts key into know_hosts file (didn't paste it here)

    output=$(expect -c '
    set prompt "(%|#|-|:|\\$) $"
    #gets stdin host; <<<"$host"
    puts $::env(host)
    set cmd ssh $host
    send -- "$cmd\r"

    set outcome ""
    regexp {^[.*#$} ignore outcome
    send -- "ls -la\r"
    expect -re $prompt
    send -- "exit\r"

    set results $expect_out(buffer)
    regexp {^root.*:$} $expect_out(buffer) results] } {
            send -- "pwd\r"
            expect -re $prompt
            send -- "date\r"
            send -- "exit\r"
    }
    ')

Any help much appreciated.

==========================================

updated :

==========================================

I was able to solve the host variable issue, figured it out that I need to give single quotes for host variable.

but now am getting 2 lines output ie,

first line as hostname printed second line app active number along with hostname

how can I get only app active number as output ?

working script :

        #!/usr/bin/bash

        host=`echo $REMOTE_HOSTNAME`
        export host

    # logic to add remote hosts key into know_hosts file (didn't paste it here)

output=$(expect -c '
#exp_internal 1
match_max 5000
set prompt "(%|#|-|:|\\$) $"
puts $::env(host)
send -- "ssh '$host'\r"
set cmd [exec appmon -print Streams active | grep -v Error | grep -Ev "^$"  | awk -F {:} {{print $2}}]
send -- "$cmd\r"
set outcome ""
regexp {#$} ignore outcome
expect *
puts $expect_out(buffer)
#set results [lindex [split $expect_out(buffer) \n] 3]
regexp {^root.*:$} $expect_out(buffer) results] } {
        send -- "pw\r"
        expect -re $prompt
        send -- "date\r"
        send -- "exit\r"
}
set out  [lindex [split $expect_out(buffer) \n] 1]
puts $out
')
echo "======="
echo "$output"
echo "======="

output getting :

======= cvxe-abdhe01

14272e-abdhe01

I was expecting to see only

14274

HULK
  • 89
  • 1
  • 14
  • Possible duplicate of [How to pass variables from shell script to expect script?](http://stackoverflow.com/questions/15422757/how-to-pass-variables-from-shell-script-to-expect-script) – codeforester Feb 19 '17 at 18:07

1 Answers1

1

Be careful what you are talking about. We have shell variables and we have exported shell variables a.k.a. environment variables. In your code, you have an environment variable, because you export it.

I answer here therefore for accessing environment variables from within the expect program. If you are really concerned about shell variables, please clarify this in your posting.

First, I strongly recommend to use ALL UPPERCASE names for environment variables. Having lower case in the name of an environ might or might not work, depending on the concrete case.

Now assume I have an environment variable FOO, and I want to access it within expect. This can be done in two ways, using the builtin array env:

$::env(FOO)
[set ::env(FOO)]

BTW, in cases like yours, you do not need the double-colon; for instance $env(FOO) would do the same. However, if you get used to write the ::, you will be on the safe side in case you have to maintain more complex expect programs.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Thanks user1934428 for valid inputs on env variable naming and how to use them in expect. – HULK Feb 20 '17 at 14:33