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