0

My script is working but it's not reading the next line of the file .it is giving output for only one user not providing for next user from the users.txt file

#!/bin/bash

# Read the file containing the list of users to check
input_file="users.txt"

# Create empty files to hold the output
found_file="users_found.txt"
not_found_file="users_not_found.txt"
echo "" > $found_file
echo "" > $not_found_file

# Loop through each user in the input file
while read user; do
# Try to run the ldapsearch command for the user
if ssh -n ldaptest /opt/ldap/ldapsearch --user $user >/dev/null 2>&1; then
# User was found in LDAP, so append to the found file
echo "$user" >> $found_file
 else
  # User was not found in LDAP, so append to the not found file
 echo "$user" >> $not_found_file
 fi
 done < $input_file

The above script will check user is available in ldap or not.if user is available in ldap then it sends its output to found_file else it will send user to not_found_file here it's not reading input on second line.

cat -vet users.txt
Testuser1      $
Testuser1         $
Testuser3    $
Testuser4    $
Randu
  • 3
  • 3

1 Answers1

0

This is a common issue while using for loop or while loop with ssh. After an execution ssh breaks the loop. Use below logic to get rid of this issue.

cat users.txt | while read user; do
   <Your ssh command>
done
S. Mondal
  • 54
  • 5
  • _After an execution ssh breaks the loop_ .... Why? – user1934428 Apr 14 '23 at 06:09
  • 1
    The problem is that ssh reads from standard input, therefore it eats all your remaining lines. – S. Mondal Apr 14 '23 at 06:19
  • Isn't this only if I'm using `-T` (Disable pseudo-terminal allocation) or `-W` (port forwarding)? – user1934428 Apr 14 '23 at 09:42
  • It's not working I'm getting same output – Randu Apr 14 '23 at 09:51
  • Do not use -n with your ssh command, its prevent ssh to reading from stdin. – S. Mondal Apr 15 '23 at 02:31
  • I have tried without -n as well I'm getting same output. – Randu Apr 15 '23 at 04:44
  • Can you provide your new code here ? – S. Mondal Apr 16 '23 at 05:37
  • found_file="users_found.txt" not_found_file="users_not_found.txt" echo "" > $found_file echo "" > $not_found_file cat users.txt | while read user; do if ssh ldaptest /opt/ldap/ldapsearch --user $user >/dev/null 2>&1; then echo "$user" >> $found_file else # User was not found in LDAP, so append to the not found file echo "$user" >> $not_found_file fi done – Randu Apr 16 '23 at 10:27
  • found_file="users_found.txt" not_found_file="users_not_found.txt" echo "" > $found_file echo "" > $not_found_file cat users.txt | while read user; do ssh ldaptest /opt/ldap/ldapsearch --user $user >/dev/null 2>&1 if [ $? == 0 ]; then echo "$user" >> $found_file else # User was not found in LDAP, so append to the not found file echo "$user" >> $not_found_file fi done – S. Mondal Apr 16 '23 at 15:40
  • That worked I have added in script thankyou – Randu Apr 16 '23 at 18:04
  • Glad to here that, you are welcome. – S. Mondal Apr 17 '23 at 04:15