gpt4 book ai didi

linux - shell脚本ssh命令不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:31 33 4
gpt4 key购买 nike

我有一小部分服务器,我想在每个服务器上添加一个用户。我可以单独连接到每台服务器并运行命令。

sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug

我写了一个脚本来遍历列表并运行这个命令,但我遇到了一些错误。

#!/bin/bash
read -p "Enter server list: " file

if [[ $file == *linux* ]]; then
for i in `cat $file`
do
echo "creating amurug on" $i
ssh $i sudo /usr/sbin/useradd -c "Arun" -d /home/amurug -e 2014-12-12 -g users -u 1470 amurug
echo "==============================================="
sleep 5
done
fi

当我运行脚本时,它不执行命令。

creating amurug on svr102
Usage: useradd [options] LOGIN

Options:

我的脚本中的 ssh crommand 有什么问题?

最佳答案

试试这个脚本:

#!/bin/bash
read -p "Enter server list: " file

if [[ "$file" == *linux* ]]; then
while read -r server
do
echo "creating amurug on" "$server"
ssh -t -t "$server" "sudo /usr/sbin/useradd -c Arun -d /home/amurug \
-e 2014-12-12 -g users -u 1470 amurug"
echo "==============================================="
sleep 5
done < "$file"
fi

根据man bash:

-t

Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.

关于linux - shell脚本ssh命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24493030/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com