baj
baj

Reputation: 249

How to read expect output in bash variable?

I use expect tu verify a password against ssh. I have tried to output via puts but I just get the end of the server response in the string.

How do I only get the puts values?

okk=$(expect -c "
set timeout 15
spawn ssh -p 22 [email protected] 
expect {
    \"(yes/no)\" {
        sleep 1
        send \"yes\n\"
        exp_continue
    }
    \"(y/n)\" {
        sleep 1
        send \"y\n\"
        exp_continue
    }
    password {
        sleep 1
        send \"$sshpw\r\"
        exp_continue
    }
    Password {
        sleep 1
        send \"$sshpw\r\"
        exp_continue
    }
    \"Last login\" {
        puts \"yes\"
        exit 1
    }
    \"Permission denied\" {
        return \"no\" 
        exit 1
    }
    timeout {
        puts \"timeout\"
        exit 1
    }
    eof {
        puts \"error\"
    }
}
sleep 1
expect eof
")

echo $okk

onsectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nis

Upvotes: 1

Views: 314

Answers (1)

Eugeniu Rosca
Eugeniu Rosca

Reputation: 5305

Two changes are needed to filter out everything except the output of puts:

  1. Use spawn -noecho ssh -p 22 [email protected]

  2. Add log_user 0 after set timeout 15

WARNING: if you hit a case that does not print anything in the EXPECT world, you will get an empty string in the BASH world. So, take care of this!

Upvotes: 1

Related Questions