scp脚本到需要密码的多个主机

时间:2014-01-14 22:51:37

标签: shell scp

我正在尝试编写一个shell脚本,它将给定源文件,目标目录和hosts文件,将文件scp到多个主机。我已经让脚本工作但是有一些问题,但文件到达那里。唯一的问题是我正在处理的网络需要每个遥控器的密码。我需要对150加遥控器执行此操作,并且需要自动运行,可能需要使用日志记录功能。如果任何人可以帮助我,将不胜感激。这是我到目前为止所得到的......

# This is a script to copy files from one host to a group of hosts
# There are three variables accepted via commandline
# $1 = first parameter (/source_path/source_filename)
# $2 = second parameter (/target_directory/)
# $3 = third paramter (file that contains list of hosts)
SOURCEFILE=$1
TARGETDIR=$2
HOSTFILE=$3
if [ -f $SOURCEFILE ]
then
    printf "File found, preparing to transfer\n"
    while read server
    do
        scp -p $SOURCEFILE ${server}:$TARGETDIR
done < $HOSTFILE
else
    printf "File \"$SOURCEFILE\" not found\n"
    exit 1
fi
exit 0

1 个答案:

答案 0 :(得分:2)

SSH公钥认证是一个很好的解决方案。

简而言之,运行:

ssh-keygen -t dsa

生成公钥/私钥对。

然后,将~/.ssh/id_dsa.pub添加到remote:~/.ssh/authorized_keys

现在,ssh不会要求输入密码,因为它会进行公钥验证质询。

我在这里写了一篇文章,详细描述了这一点:

http://matt.might.net/articles/ssh-hacks/

您不必为此修改脚本。