gpt4 book ai didi

linux - 使用 Shell 脚本提供密码

转载 作者:太空宇宙 更新时间:2023-11-04 09:57:42 25 4
gpt4 key购买 nike

我已将我的客户端和服务器设置为无密码登录。就像通过将服务器的 RSA key 复制到所有客户端的/root/.ssh/id-rsa.pub 来进行无密码登录一样。但是这个,我是手动完成的。我喜欢使用 shell 脚本自动执行此过程,并通过脚本向机器提供密码。如果这个问题解决了那么我还想使用 rsync 来自动将项目推送到所有服务器。任何人都可以在这方面帮助我。

谢谢

最佳答案

此脚本出现在 Debian(和衍生产品)机器中,用于分发 key 。它称为 ssh-copy-id。你会像这样使用它:

ssh-copy-id [-i identity_file] [user@]machine

然后您将输入密码并完成复制。您只需执行一次此操作,然后就可以像往常一样通过 ssh 使用 rsync。

#!/bin/sh

# Shell script to install your identity.pub on a remote machine
# Takes the remote machine name as an argument.
# Obviously, the remote machine must accept password authentication,
# or one of the other keys in your ssh-agent, for this to work.

ID_FILE="${HOME}/.ssh/identity.pub"

if [ "-i" = "$1" ]; then
shift
# check if we have 2 parameters left, if so the first is the new ID file
if [ -n "$2" ]; then
if expr "$1" : ".*\.pub" ; then
ID_FILE="$1"
else
ID_FILE="$1.pub"
fi
shift # and this should leave $1 as the target name
fi
else
if [ x$SSH_AUTH_SOCK != x ] ; then
GET_ID="$GET_ID ssh-add -L"
fi
fi

if [ -z "`eval $GET_ID`" ] && [ -r "${ID_FILE}" ] ; then
GET_ID="cat ${ID_FILE}"
fi

if [ -z "`eval $GET_ID`" ]; then
echo "$0: ERROR: No identities found" >&2
exit 1
fi

if [ "$#" -lt 1 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: $0 [-i [identity_file]] [user@]machine" >&2
exit 1
fi

{ eval "$GET_ID" ; } | ssh $1 "umask 077; test -d .ssh || mkdir .ssh ; cat >> .ssh/authorized_keys" || exit 1

cat <<EOF
Now try logging into the machine, with "ssh '$1'", and check in:

.ssh/authorized_keys

to make sure we haven't added extra keys that you weren't expecting.

EOF

关于linux - 使用 Shell 脚本提供密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/224712/

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