bash脚本通过ssh多个服务器在一个循环,并发出命令 [英] bash script to ssh multiple servers in a Loop and issue commands

查看:133
本文介绍了bash脚本通过ssh多个服务器在一个循环,并发出命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有一个服务器列表的文本文件。我想一个从文件, SSH 服务器读取服务器之一,并执行 LS 看目录内容。我的循环运行只有一次,当我运行 SSH 命令,然而,对于 SCP 运行在文本中的所有服务器文件并退出,我想循环运行,直到为SSH文本文件的末尾。以下是我的bash脚本,我怎么能使其运行在文本文件中的所有服务器,而这样做 SSH

I have a text file in which I have a list of servers. I'm trying to read the server one by one from the file, SSH in the server and execute ls to see the directory contents. My loop runs just once when I run the SSH command, however, for SCP it runs for all servers in the text file and exits, I want the loop to run till the end of text file for SSH. Following is my bash script, how can I make it run for all the servers in the text file while doing SSH?

#!/bin/bash
while read line
do
    name=$line
    ssh abc_def@$line "hostname; ls;"
#   scp /home/zahaib/nodes/fpl_* abc_def@$line:/home/abc_def/
done < $1

我运行该脚本 $ ./script.sh hostnames.txt

推荐答案

本code的问题是, SSH 开始从标准输入读取数据,你打算为读行。你可以告诉 SSH 从别的东西来代替,如的/ dev / null的阅读,避免食用所有其他主机名

The problem with this code is that ssh starts reading data from stdin, which you intended for read line. You can tell ssh to read from something else instead, like /dev/null, to avoid eating all the other hostnames.

#!/bin/bash
while read line
do
    ssh abc_def@"$line" "hostname; ls;" < /dev/null
done < "$1"

这篇关于bash脚本通过ssh多个服务器在一个循环,并发出命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆