查看“Rrun”的源代码
←
Rrun
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
此脚本用于所有在<机器列表文件>中的主机上远程执行<命令>。 <small><nowiki>#!/bin/bash # This script remotely executes a command on a list of machines. # # Usage: # rrun <machine_list_file> <command> # # <machine_list_file>: Path to a file containing a list of machine names or IP addresses (one per line). # <command>: The command to execute on each machine. This should be quoted to prevent premature expansion. # Check if the second argument (the command) is provided. if [ -z "$2" ]; then echo "Title : Remote Run" echo "Usage : rrun <machine_list_file> <command>" echo " <machine_list_file>: Path to a file containing machine names/IPs (one per line)." echo " <command>: The command to execute on each machine (quote it!)." echo "" echo "Date : 2018-11-15" echo "Author : adaM (ldscfe@gmail.com)" exit 0 fi machine_list_file="$1" command_to_execute="$2" # Check if the machine list file exists and is readable. if [ ! -r "$machine_list_file" ]; then echo "Error: Machine list file '$machine_list_file' not found or not readable." >&2 exit 1 fi # Executes a command on remote machines. while IFS= read -r machine; do echo "--- Running on: $machine ---" ssh -n "root@$machine" "$command_to_execute" done < "$machine_list_file" exit 0</nowiki></small> === 注意 === 用 ssh 在远程主机上执行命令中的选项 -n 不可缺少,原因在于 ssh 命令默认会从标准输入(stdin)读取数据,而 while 循环正是从文件中读取机器 IP。执行 ssh 命令时,它会“抢走”本该留给循环后续迭代的输入,从而导致循环在第一台主机执行后就结束。 * 使用 ssh 的 -n 选项:该选项会使 ssh 不从标准输入读取数据 * 将标准输入重定向到 /dev/null,如:ssh "root@$machine" "$command_to_execute" < /dev/null [[分类:Develop]] [[分类:Linux]] [[分类:Shell]]
返回
Rrun
。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
目录
文章分类
侧边栏
帮助
工具
链入页面
相关更改
特殊页面
页面信息