Hi I am new to shell script and I am having trouble using Expect command. I can't get the output for the command I executed in expect block.
I have a ssh script that will connect to a server and check if a log directory exists or not. And if it's not exist, to create one. And if can't create, log the error message.
USER=testuser
IP=101.101.101.101
LOGDIR=/var/aaa/bbb/log
# check directory
sudo ssh -l ${USER} ${IP} ls -ld ${LOGDIR} > /dev/null 2>&1
if [ $? -ne 0 ]; then
# make directory
# if error, output log
fi
I want to automate the login process when doing this so I am using expect command as below:
# check directory
PASS=testpassword
expect -c "
set timeout 5
spawn ssh -l ${USER} ${IP} ls -ld ${LOGDIR} > /dev/null 2>&1
expect \"${USER}\'s password:\"
send \"${PASS}\n\"
expect \"$\"
"
if [ $? -ne 0 ]; then
# make directory
# if error, output log
fi
However, I am unable to retrieve the output from the command of "spawn ssh" line.
How can I get the output from the command inside expect block?
And because I'm not familiar with shell script, a simple code sample will be much appreciated.