2

I want to pass the $SPACE variable into an Expect script and get the output and again pass into the shell script.

#!/bin/bash -x

    SPACE=$(df -h | awk '{ print $5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1)
    #set user "root"
    #set ip "192\.168\.53\.197"
    #echo $SPACE

    expect<<EOF

    spawn ssh "root\@192\.168\.53\.197"

    expect "Password:"

    send "Karvy123$\r";

    expect "prompt"

    send "$SPACE\r";

    expect "prompt"

    EOF
5
  • Does it work? If not, what error do you get? Commented Feb 21, 2014 at 7:42
  • i get given error [root@localhost ~]# 47 -bash: 47: command not found Commented Feb 21, 2014 at 8:14
  • Where could 47 come from? Commented Feb 21, 2014 at 8:19
  • i think that value come from $SPACE Commented Feb 21, 2014 at 8:24
  • 1
    Also, note that the closing "EOF" must not have leading spaces: your shell script as you pasted here will not work. Commented Feb 21, 2014 at 11:19

1 Answer 1

8

Oh, I see. You want $SPACE to hold the COMMAND, not the VALUE. This construct will execute the command and save the output in the variable: x=$(cmd arg arg). So you are finding the space used on your local host and sending that value to the remote host.

You need something like this:

#!/bin/bash
export cmd="df -h | awk '{ print \$5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1"
remote_usage=$(
    expect <<'EOF'
    log_user 0
    spawn -noecho ssh "[email protected]"
    expect "Password:"
    send "*****\r"
    expect "prompt"
    send "$env(cmd)\r"
    expect -re {(?n)^(\d+)$.*prompt}
    send_user "$expect_out(1,string)\n"
    send "exit\r"
    expect eof
EOF
)
echo "disk usage on remote host: $remote_usage"

However, you don't have to do any of that. Set up SSH keys (with ssh-keygen and ssh-copy-id) and do

remote_usage=$( ssh [email protected] sh -c "df -h | awk '{ print \$5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1" )
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for ur reply...@glenn I'm getting following error--> can't read "expect_out(buffer)": no such variable while executing "send_user "$expect_out(buffer)\n""
#!/bin/bash export cmd="df -h | awk '{ print \$5 }' | grep -v Use |sort -n |tail -1 | cut -d % -f1" remote_usage=$( expect <<'EOF' log_user 0 spawn -noecho ssh "[email protected]" expect "Password:" send "Karvy123$\r" expect "prompt" send "$env(cmd)\r" expect -re {(?n)^(\d+)$.*prompt} set results $expect_out(buffer) send "exit\r" expect eof EOF )
use expect_out(1,string) instead of expect_out(buffer) -- the former will just be the digits output by the command; the latter will include the command, the prompt, etc
i'm using same code which you provide after that same error---> can't read "expect_out(1,string)": no such variable while executing "send_user "$expect_out(1,string)\n""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.