Bash script for executing linux commands on remote machines using ssh key |

Trending 2 months ago

I want to tally a book to get specifications of hardwares and personification specifications from aggregate distant machines by login utilizing ssh backstage key. this is my script

#!/bin/bash # Check if nan personification provided a file if [ $# -eq 0 ]; then echo "provide input file" echo "Usage: $0 <input_file>" exit 1 fi # Read input file input_file=$1 # Check if nan record exists if [ ! -f "$input_file" ]; then echo "Error: File $input_file not found." exit 1 fi # Read each statement from nan input file while IFS=, publication -r ip_address username; do echo "Machine IP: $ip_address" # Connect to nan server via SSH and execute nan command echo "System Users:" ssh -i /home/subash/Desktop/privatekey "$username"@"$ip_address" "ls -l /home/ | awk 'NR >1 {print \$3}'" 2>/dev/null echo "---------------" echo "Disk details:" ssh -i privatekey "$username"@"$ip_address" 'lsblk -o name,model,size,ro,mountpoint,type | grep -e portion -e disk' 2>/dev/null echo "---------------" echo "OPERATING SYSTEM:" ssh -i privatekey "$username"@"$ip_address" "cat /etc/os-release | grep '^NAME' | trim -d'=' -f2-" 2>/dev/null echo "---------------" done < "$input_file"`

The input record I'm providing person nan ip reside and nan username. I person added my nationalist cardinal to each nan machines authorized keys. erstwhile executing nan book nan while loop executes pinch nan first IP and provides nan expected output and exists it doesn't see nan adjacent statement of input. really tin I modify nan book to publication each nan input. Thanks successful Advance.

The sample input file:,user1,user2,user3

If I region nan ssh bid and people each nan IP and personification names it prints each nan values from nan input file.