0

I am developing a code. From server, A ssh session will be established to Server B using spawn, and then from Server B sftp file transfer to Server C. Server A and B can be passwordless or it may ask for a password depending upon earlier user configuration. Please find below the code:

 #!/bin/bash
 /usr/bin/expect <<EOD
 lassign $argv 1 2 3 4 file1 file2
 spawn ssh -oStrictHostKeyChecking=no root@$1
 sleep 2
 expect {
 "#" {
 send_user "successfully logged in!\r"
     }
 "Password:" {
  send "$2\r"
   }
 }
 EOD
 sleep 3
 pinging=$( ping $3 -w1 | head -n 2 | grep -i time )
 if [[ $pinging == *"time"* ]];
 then
            echo "ping successfull to:" $3
 else
            echo "ping not successfull:"$3 "Contact IT Support"
            exit
 fi

The issue is the if-else loop is not getting executed properly for Password prompt or no Password prompt after login to ssh remote session.Its aksing for Password even though login was successful.Also Ping to Server C is working from Server B not from Server A. I am very new to expect TCL commands

3
  • 1
    A first bug to fix is replacing <<EOD by <<'EOD'. Read the very short section Here Documents in man bash.
    – meuh
    Commented Apr 23, 2020 at 6:44
  • Show how you invoke this script. What are $1, $2, $3? Commented Apr 23, 2020 at 21:30
  • i invoke the script from server A ./testsftp.sh <IP of Server B> <Password of server B> <IP of Server C>
    – newuser
    Commented Apr 24, 2020 at 6:59

1 Answer 1

1

You can't mix expect and shell like that. After you send the password the expect script ends, and the expect process exits, and the ssh connection is gone. If you want to ping C from B, you need to put that part into the expect code.

1
  • Thank You. I will consider it as an answer and will try to add and check
    – newuser
    Commented Apr 24, 2020 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .