5

Linux shell脚本使用while循环执行ssh的注意事项

 2 years ago
source link: https://blogread.cn/it/article/7420?f=hot1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Linux shell脚本使用while循环执行ssh的注意事项

浏览:6016次  出处信息

   如果要使用ssh批量登录到其它系统上操作时,我们会采用循环的方式去处理,那么这里存在一个巨大坑,你必须要小心了。

   一、场景还原:

   我现在是想用一个脚本获取一定列表服务器的运行时间,首先我建立一个名字为ip.txt的IP列表(一个IP一行),再建好密钥实现不用密码直接登录。然后写脚本如下:

#!/bin/bash
while read ips;
do
echo $ips;
done < ip.txt

   脚本实现了逐行读取列表中的IP,但是:

#!/bin/bash
while read ips;
do
echo $ips;
upt=`ssh root@$ips "uptime"`;
echo $upt;
done < ip.txt

   脚本只对第一个IP做了检测,就直接跳出来了。

   二、问题分析:

   while使用重定向机制,ip.txt文件中的信息都已经读入并重定向给了整个while语句,所以当我们在while循环中再一次调用read语句,就会读取到下一条记录。问题就出在这里,ssh语句正好回读取输入中的所有东西。为了禁止ssh读所有东西增加一个< /dev/null,将ssh 的输入重定向输入。

   三、解决策略:

   1、使用for循环代表while,因为for没有一次把文件内容缓存获取过来,代码段修改如下:

for ips in `cat ip.txt`; do
echo ${ips};
upt=`ssh root@${ips} uptime`;
echo $upt;
done

   2、若坚持使用while循环,那么需要对ssh增加-n参数,为什么增加了-n参数也可以解决问题呢?通过man ssh查看-n参数的说明:

   Redirects stdin from /dev/null (actually, prevents reading from stdin)

   修改后的代码如下:

#!/bin/bash
while read ips;
do
echo $ips;
upt=`ssh -n root@$ips "uptime"`;
echo $upt;
done < ip.txt

建议继续学习:

QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK