-1

I have a list of IP's let's say IP.txt and I need to compose a script which tries to login to those ip's with username "fsp" and passwd let's say "abcd". if connection is succesfull , please send name of the server with some status , if not, please send me another output with let's say "failed name of server conection". Can you please help me out here ? these has to be in Linux /bin/bash...

I tried some "for loops" but it take me out only servers with succesfull state like :

for i in `cat IP.txt`; do
  sshpass -p "abcd" ssh fsp@$i  " cat /etc/os-release  "
done >> outputIP.txt
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 1
    See [DontReadLinesWithFor](https://mywiki.wooledge.org/DontReadLinesWithFor) and [BashFAQ #1](https://mywiki.wooledge.org/BashFAQ/001). – Charles Duffy May 31 '23 at 14:35
  • That said, note that to be fitting our format, a Stack Overflow question should be about a narrow, specific technical problem you encountered when writing a script yourself, with everything that isn't strictly necessary to reproduce that technical problem removed from the question. See [mre] guidelines. – Charles Duffy May 31 '23 at 14:37
  • ...if what you want is just general-purpose code review, instead of a solution to a narrow, specific technical problem, we have a separate site for that: [codereview.se]. – Charles Duffy May 31 '23 at 14:39

2 Answers2

-1

Your code will work like you want it to, if you add a check that the command completed with exit code 0 (success)

IPS_FILE="IPs.txt"
PASSWORD="abcd"
USERNAME="fsp"
OUTPUT_FILE="output.txt"

for ip in `cat $IPS_FILE`; do
    version=$(sshpass -p "$PASSWORD" ssh $USERNAME@$ip "cat /etc/os-release")
    if [ $? -eq 0 ]; then
        echo "$version" >> "$OUTPUT_FILE"
    else
        echo "Failed on IP $ip"
    fi
done

What this script does is it iterates your IPs, then executes the command and places the stdout of it into value variable. Then it checks if the exit code is 0 (success) by using $? that stores the exit code of the last executed command. You can add more logic to it if you want to handle different kinds of errors differently by referring to "RETURN VALUES" section of the sshpass manual page, for example here: https://manpages.ubuntu.com/manpages/focal/man1/sshpass.1.html

Gem
  • 7
  • 4
  • Thank's I tried and it turn out that "IPS_FILE does not exist" but I figured out another script and this one works for me: – Dumitrescu Cristi May 31 '23 at 14:08
  • Have a closer look at the `cat $(IPS_FILE)`. Further, you do not need to utilize `cat`, you can use `while read -r ip ; do ...your code.. done <"$IPS_FILE" to read the IPs.txt file line by line. You can also paste your code into https://www.shellcheck.net for first pass debugging. – j_b May 31 '23 at 14:17
  • 1
    See [Why you don't read lines with `for`](https://mywiki.wooledge.org/DontReadLinesWithFor) and [BashFAQ #1](https://mywiki.wooledge.org/BashFAQ/001). And putting the `>>"$OUTPUT_FILE"` on the `done` as the OP was already doing is more efficient than what you're doing here: their code was opening the file only once and leaving it open, whereas yours re-opens the file over and over. Error messages should have `>&2` on them so they go to stderr instead of stdout, making redirecting stdout safe. – Charles Duffy May 31 '23 at 14:36
  • 1
    (and `if version=$(...); then` lets you get out of the business of messing with `$?`; see https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – Charles Duffy May 31 '23 at 16:34
-1
#!/bin/bash

# Define the password to check
password="your_password"
# Read the server list from the file

servers_file="servers.txt"

servers=$(cat "$servers_file")

# Function to check the password for a single server

check_password() {
  server=$1

  echo "Checking password for server: $server"

  # Modify the following command as per your requirements to connect to the server and check the password

  sshpass -p "$password" ssh user@$server "echo 'Password check successful!'"

  # Check the return code of the command
 
  if [[ $? -eq 0 ]]; then
    echo "Password is okay for server: $server"
  else
    echo "Password is not okay for server: $server"
  fi
}
     

# Iterate over the servers and check the password for each one
for server in $servers; do
  check_password "$server"
done
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • 1
    Re: `for server in $servers`, you're effectively running into [BashPitfalls #1](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29). – Charles Duffy May 31 '23 at 14:38
  • 1
    And don't use `$?` in this way -- see [Why is testing "$?" to see if a command succeeded or not an antipattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – Charles Duffy May 31 '23 at 14:38