BASH/获取服务器运行时间列表
来自康健生活
< BASH
获取服务器运行时间列表
一个脚本获取一定列表服务器的运行时间。
while使用重定向机制
while使用重定向机制,ip.txt文件中的信息都已经读入并重定向给了整个while语句。所以当我们在while循环中再一次调用read语句,就会读取到下一条记录。问题就出在这里,ssh语句正好回读取输入中的所有东西。为了禁止ssh读所有东西增加一个< /dev/null,将ssh 的输入重定向输入
while read ips;
do
echo $ips;
upt=`ssh root@${ip1} uptime < /dev/null;`;
echo $upt;
done < ip.txt;
- < /dev/null是告诉ssh如果要获取就从/dev/null获取,避免了获取while缓存
- for没有一次把文件内容缓存获取过来
- 看一下man ssh -n参数 Redirects stdin from /dev/null (actually, prevents reading from stdin).和</dev/null道理一样。
for
for ips in `cat ip.txt`; do
echo ${ips};
upt=`ssh root@${ips} uptime`;
echo $upt;
done
No categories