我正在处理bash脚本,以搜索所有在端口8291上打开的mikrotik设备,将其SSH到每个设备并执行一些命令。
如果我通过ip.txt进行正常的while循环,那么回声就可以了。
while read REMOTE_CLIENT
do
echo "$REMOTE_CLIENT"
done < $REMOTE_CLIENT
#!/bin/bash
IP_RANGE="10.0-8.0-1.*"
SSH_USER="Username"
SSH_PASS="Password"
CMD='/system identity print'
LOCAL_DIR=$(pwd)
LOG=$LOCAL_DIR/Mikrotik_SSH.log
REMOTE_CLIENT=$LOCAL_DIR/ip.txt
echo -e "\033[1mMikrotik SSH utility\033[0m"
echo ""
sleep 1
echo -e "\e[93mRunning NMap Scan - Might Take up to 5 min\e[0m"
echo ""
nmap -n -sT -T4 -p 8291 --open $IP_RANGE -oG - | awk '/Up$/{print $2}' > ip.txt
echo -e "\e[31mNMap scan completed and IP's stored in ips.txt\e[0m"
echo ""
sleep 1
echo -e "\e[92mExecuting ssh & injecting command $CMD\e[0m"
echo ""
echo $CMD > $LOG
echo "--------------------------------------------------------------------------------" >> $LOG
while read HOST;
do
if sshpass -p $SSH_PASS ssh -T -o StrictHostKeyChecking=no -o IPQoS=throughput $SSH_USER@$HOST $CMD >> $LOG;
then
echo "$HOST Passed" >> $LOG;
echo "--------------------------------------------------------------------------------";
echo "$HOST == Passed";
else
echo "$HOST == Failed" >> $LOG;
echo "--------------------------------------------------------------------------------";
echo "$HOST == Failed";
fi
echo "--------------------------------------------------------------------------------";
echo "--------------------------------------------------------------------------------" >> $LOG;
done < $REMOTE_CLIENT
echo "Done!"
Mikrotik_SSH.log
/system identity print
--------------------------------------------------------------------------------
name: Some Name
10.4.0.205 Passed
--------------------------------------------------------------------------------
希望循环遍历ip.txt中的每个IP,而不是仅止于一个。还想让CMD = $ LOCAL_DIR / Mikrotik_Command.txt