Command & Development/Shell Script

[Shell Script] List 내 서버 접속 / 정보 확인

Richard.Ryu 2024. 6. 13. 15:02
반응형
#!bin/bash

home_dir=/home/tool/
mkdir -p $home_dir
date=`date +%Y%m%d%H%M`
output_file="${home_dir}/values_${date}.csv"

#List file must in need 3 values, id, ip, hostname
####### Example List file values
#  1 192.168.10.1 Server1
#  2 192.168.10.2 Server2
#  3 192.168.10.3 Server3

file="$home_dir/****.list"

username=""
password=""

content=$(cat "$file")


IFS=$'\n'
for line in $content; do
    id=$(echo "$line" | cut -d ' ' -f 1)
    ip=$(echo "$line" | cut -d ' ' -f 2)
    hostname=$(echo "$line" | cut -d ' ' -f 3)
    echo "-----------------------------------------------------------------------------" | tee -a "$output_file"
    printf "%-12s %-18s %-30s \n" $id $ip $hostname | tee -a "$output_file"
    echo "-----------------------------------------------------------------------------" | tee -a "$output_file"

    if [[ -z "$id" || -z "$ip" || -z "$dn" ]]; then
       echo "Error: Missing value in line: $line"
       exit 1
    else
    sshpass -p "${password}" ssh -o StrictHostKeyChecking=no -o LogLevel=quiet "${username}"@"${ip}" -p 22 "cat /etc/TEST" | tee -a "$output_file"
    echo "" | tee -a "$output_file"
    sleep 1
    fi
done

echo "--------------complete---------------"

--------------Execution Result-------------------

-----------------------------------------------------------------------------
1                  192.168.10.1                 Server1
-----------------------------------------------------------------------------
!!!!!!!!!!!!!!!

-----------------------------------------------------------------------------
2                  192.168.10.2                 Server2
-----------------------------------------------------------------------------
!!!!!!!!!!!!!!!

-----------------------------------------------------------------------------
3                  192.168.10.3                 Server3
-----------------------------------------------------------------------------
!!!!!!!!!!!!!!!

--------------complete---------------
반응형