0

Summary:
I am writing a script to check if a directory exists on a remote machine. I need a solution which can allow me to check for that directory and return the result in a usable way. This whole process is automated within a much larger script so I need a functional way to tell the parent script the directory exists or not.

Restrictions:
The tools that I have are limited. $REMOTE_2 can only be accessed through $REMOTE_1. Also, $REMOTE_2 can only be connected via telnet (no ssh available).

Current goal:
I am trying to set a local variable to be then read back to chose a return code. I am open to other options, but this is the closest I've come to a working solution so far.

I realize that $found will take from the parent process, but this is not my desired result and am not sure what syntax I need to return true or false when trying to echo the $found variable.

/usr/bin/expect<<EOF
spawn ssh $USER@$REMOTE_1
expect "*$USER*"

send -- "telnet $REMOTE_2\r"
expect "*login:*"

send -- "root\r"
expect "*$*"

# Everything prior to this can't be changed. Everything after it can be.

send "if \[ -d $DIRECTORY_LOCATION \] ; then found=true; else found=false ; fi\r"

send -- "echo **$found**\r"
expect {
    "*true*" {
        exit 0
    }
    "*false*" {
        exit 1
    }
}
EOF

I believe this type of solution can work, but I am not sure how to use the remote variable that I store within the if statement, later on to allow me to choose which return code to use.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Jens Petersen
  • 189
  • 2
  • 11
  • You're already exiting with different values (`0` vs. `1`) so why not just check `$?`. –  Aug 15 '19 at 11:01
  • That's the goal, but getting to that step correctly is where it's failing. `send -- "echo $found\r"` actually returns `send -- "echo \r"` since it tries to take $found from the parent shell rather than the remote machine. – Jens Petersen Aug 15 '19 at 15:06

0 Answers0