0

I know there are other solution such as expect, or Python paramiko, but bash shell is the only option available for now.

Out of these 3 IPs, only 172.16.1.2 has SSH server installed and they have similar password.

wolf@linux:~$ cat ip.txt
172.16.1.1
172.16.1.2
172.16.1.3
wolf@linux:~$

This is the Bash script

wolf@linux:~$ cat sshSession.sh
while read host
do
    export SSH_ASKPASS='~/ePass'
    setsid ssh -T user@$host
    if [ $? = 0 ]; then
        exit
        echo "$host | SSH Authentication OK"
    else
        echo "$host | SSH Authentication PROBLEM"
    fi
done < ip.txt
wolf@linux:~$ 

Output

wolf@linux:~$ ./sshSession.sh
ssh: connect to host 172.16.1.1 port 22: No route to host
172.16.1.1 | SSH Authentication PROBLEM
Welcome to Ubuntu Server

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

-bash: line 1: 172.16.1.3: command not found
172.16.1.2 | SSH Authentication PROBLEM
wolf@linux:~$ 

There are a few problems here.

  1. I just want to get the authentication status only, not to log in into the server.

  2. There is something wrong with the output -bash: line 1: 172.16.1.3: command not found

  3. 172.16.1.2 | SSH Authentication PROBLEM - This is the only host installed with SSH, while others not.

Desired Output

wolf@linux:~$ ./sshSession.sh
172.16.1.1 | SSH Authentication PROBLEM
172.16.1.2 | SSH Authentication OK
172.16.1.3 | SSH Authentication PROBLEM

What's wrong in the script and how to fix it?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • (OT: **1** You’re missing a shebang. It looks like you’re getting Bash, but there’s no guarantee. **2** `exit ; echo` won’t work. **3** No need to export `SSH_ASKPASS` more than once.) – Biffen Oct 20 '20 at 08:54
  • 1
    `ssh user@host true` should tell you whether you can connect and authenticate, without starting an interactive session. – Biffen Oct 20 '20 at 08:55
  • 1
    Tangentially see also [Why is testing `$?` to see if a command succeeded or not, an anti-pattern?](/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Oct 20 '20 at 08:56
  • 1
    As for the ‘command not found’: _Probably_ a quoting issue (use [ShellCheck](https://www.shellcheck.net/)!) and/or some invisible character in the input (e.g. `\r`). Running the script with `bash -x sshSession.sh` (or adding `set -x` at the top of the script) should help you debug. – Biffen Oct 20 '20 at 08:57
  • Thanks @Biffen, `ssh user@host true` very helpful. I've tested shebang and it doesn't give any difference. –  Oct 20 '20 at 08:59
  • 1
    (@Wolf The shebang won’t _solve_ anything (this time). But it’s a good idea, especially if you want to use the script on an other machine.) – Biffen Oct 20 '20 at 09:01
  • Thanks @Biffen, I've updated the code and output above. Half of the problems have been solved. –  Oct 20 '20 at 09:06

1 Answers1

1

You seem to be assuming that the exit gets written into the ssh session, but that's not how it works. You execute exit after the ssh process terminates. See Pass commands as input to another command (su, ssh, sh, etc)

Also, you seem to be passing standard input from your while loop into the running ssh instance. See Shell script while read line loop stops after the first line

Also, Why is testing "$?" to see if a command succeeded or not, an anti-pattern?

Also, use read -r as a general principle (though I guess it doesn't really matter here). And don't repeatedly set SSH_ASKPASS to the same value. (And generally don't export a variable more than once; it's harmless but usually reveals more than you like about your understanding of what export actually does.) And, quote your variables.

export SSH_ASKPASS='~/ePass'
while read -r host
do
    if setsid ssh -T -n user@"$host" true; then
        echo "$host | SSH Authentication OK"
    else
        echo "$host | SSH Authentication PROBLEM"
    fi
done < ip.txt
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Thanks @tripleee, problem solved. Anyway, how do I remove `ssh: connect to host 172.16.1.1 port 22: No route to host` from the output? –  Oct 20 '20 at 09:12
  • 2
    Redirect standard error to `/dev/null` if you don't want to see it. – tripleee Oct 20 '20 at 09:12
  • Got it, thanks `if setsid ssh -T -n user@"$host" true 2>/dev/null; then` –  Oct 20 '20 at 09:13