Shell-Script
檢查 ssh-key 多個伺服器
我需要編寫一個腳本來檢查在多個 Redhat 伺服器(大約 980 個伺服器)上使用 ssh-key 對使用者進行的身份驗證。
使用者可以編輯使用者 ID 和私有 ssh 密鑰位置的腳本。
腳本必須:
- 檢查成功登錄或失敗(如果要求輸入密碼)並輸出到日誌文件;
- 從 servers.txt 讀取伺服器 IP/主機名;
- 如果伺服器離線則跳過。
最好的方法是什麼?
像這樣的東西:
#!/bin/bash # I assume "logfile" is the log file. If you just want the last run # in the log fil, use date> logfile. # It is always a good idea to get a time stamp in this kind of logs. date >> logfile # The read takes the input from servers.txt, which is done at the # bottom using `done < servers.txt`. # Some people like to do `cat servers.txt | while read -r hostname ; do` # but they get negative comments on stackexchange :-) while read -r hostname ; do # Test if the host is up with a simple ping # Throw away all output. if ping -c1 "$hostname" > /dev/null 2>/dev/null; then # We now test if a host is up with a simple command, echo. # with -o PasswordAuthentication=no, we make sure that password # authentication is not used. Output the result to the logfile. if ssh -o PasswordAuthentication=no "$hostname" echo ' '; then echo "OK - $hostname" >>logfile else echo "AArrrghhh $hostname" >> logfile fi else # I assumed you want some idea of how many servers are skipped. echo "skipped $hostname" >> logfile fi done < servers.txt
這是一個快速的寫作,它可能需要一些調整。評論應該給你一些關於檢查什麼的提示。