1

I have been tweaking this for quite a bit and I am stuck with a strange output stream and no result. Basically, I'm trying to find certain ssh devices on our expansive network that have a specific password and processor. Here is the script:

#/bin/bash
for i in 54
do
  for j in 1 13 14 15
  do
    out=$(expect -c "spawn /usr/bin/ssh [email protected].$i.$j cat /proc/cpuinfo | grep MyString
      expect {
        -re \".*Are.*.*yes.*no.*\" {
        send \"yes\n\"
        exp_continue
        }

        \"*?assword:*\" {
        send \"mypasswd\"
        send \"\n\"
        exp_continue
        }
      }")
    if [["$out" != ""]]
    then
      echo "10.2.$i.$j" >> rpiout.txt
    fi
  done
done

The ssh command works by itself, just fine. Also, the expect script works fine. ALSO, if I insert an "echo $out" right before the "if [[...]]" statement, I get the expected output from the SSH command. Trying to write the file, however, I get this output to the command line and NO log file...:

./check.sh: line 19: [[spawn /usr/bin/ssh some_guy:@10.2.54.1 cat /proc/cpuinfo | grep MyString
some_guy:@10.2.54.1's password:
Permission denied, please try again.
some_guy:@10.2.54.1's password:
Permission denied, please try again.
some_guy:@10.2.54.1's password:
: No such file or directoryy,password).
./check.sh: line 19: [[spawn /usr/bin/ssh some_guy:@10.2.54.13 cat /proc/cpuinfo | grep MyString
some_guy:@10.2.54.13's password:
: No such file or directory
./check.sh: line 19: [[spawn /usr/bin/ssh some_guy:@10.2.54.14 cat /proc/cpuinfo | grep MyString
some_guy:@10.2.54.14's password:
: No such file or directory
: No such file or directoryawn /usr/bin/ssh some_guy:@10.2.54.15 cat /proc/cpuinfo | grep MyString

The first one asking for the password 3 times is correct (because it is not one of the target devices). The second 2 are non-existant IP devices, but the last 2 should return a positive result.

Note that in the "error" "./check.sh: line 19: [[spawn...", line 19 is the one that starts with "if [[...".

Any help to get me out of this mess is greatly appreciated!!

1
  • what can you see when the top line would be #!/bin/bash -vx? Commented May 24, 2016 at 6:32

1 Answer 1

1

in bash [[ is not just syntax, it's a command. Like any other command, it requires whitespace to separate it from its arguments.

Not

if [["$out" != ""]]

but

if [[ "$out" != "" ]]

or

if [[ -n "$out" ]]

Additionally, due to the way expect echoes the command just like you'd see at the terminal, it's unlikely that the output will ever be empty. Try something like this:

out=$( expect <<END
    spawn -noecho /usr/bin/ssh [email protected].$i.$j sh -c {grep -q MyString /proc/cpuinfo || echo _NOT_FOUND_}
    expect {
        -re ".*Are.*.*yes.*no.*" {
            send "yes\r"
            exp_continue
        }
        "*?assword:*" {
            send "mypasswd\r"
            exp_continue
        }
        eof
    }
END
)

if [[ $out == *_NOT_FOUND_* ]]; then
    echo "MyString not found on host 10.2.$i.$j"
fi

Where _NOT_FOUND_ is some string that you would not see in /proc/cpuinfo

The -noecho is crucial here to keep "_NOT_FOUND_" out of $out unless you echo it.

Sign up to request clarification or add additional context in comments.

1 Comment

The space was the issue... DOH! Between posting and seeing your answer I changed the logic to be more specific than just looking for any output. I was looking for a string match, so the echo deal didn't come into play, but I will keep it in mind for the future. Thanks!!

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.