Linux记录-ssh无密码执行脚本
1.配置sudo无密码登陆
vim /etc/sudoers.d/app
app ALL=(ALL) ALL
app ALL=(ALL) NOPASSWD: ALL
Defaults !env_reset
2.脚本参考
#!/bin/bash
set -e
version=xxx
pre_version=xxx
user=xxx
ssh_port=22
dir=xx
iplist=(xxx)
for ip in ${iplist[@]}
do
ssh -p $ssh_port -tt $user@$ip << eeooff
xxx
eeooff
scp -P $ssh_port xx $user@$ip:$dir
ssh -p $ssh_port -tt $user@$ip << eeooff
ln -sf xxx
xxx
eeooff
done
#!/bin/bash yum -y install sshpass # confirm the user of the operation echo "The current user is `whoami`" # 1.generate the key pair # 判断key是否已经存在,如果不存在就生成新的key if [ -f ~/.ssh/id_rsa ];then echo "rsa ssh-key file already exists" /bin/true else echo "rsa ssh-key file does not exists" ssh-keygen -t rsa -f ~/.ssh/id_rsa -P "" >/dev/null 2>&1 if [ $? -eq 0 ];then echo "generate rsa ssh-key" /bin/true else echo "generate rsa ssh-key" /bin/false exit 1 fi fi # 2.distribution public key for host in $(cat ./ssh-ip | grep -v "#" | grep -v ";" | grep -v "^$") do ip=$(echo ${host} | cut -f1 -d ":") password=$(echo ${host} | cut -f2 -d ":") user=root port_ip=$(echo ${user}@${ip}) sshpass -p "${password}" ssh-copy-id -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no ${port_ip} if [ $? -eq 0 ];then echo "${ip} distribution public key" /bin/true else echo "${ip} distribution public key" /bin/false exit 1 fi done vim ssh-ip # 格式如下: ip:密码