• 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:密码 
  • 相关阅读:
    园 首页 新随笔 联系 管理 订阅 订阅 RTSP协议转换RTMP直播协议
    sequence diagram
    Model Binding
    asp.net mvc
    系统日志和异常的处理①
    随机森林之oob error 估计
    Extjs相关知识点梳理
    Extjs报错处理
    webbrowser在html中写入内容并添加js
    tcpdump一个命令的剖析
  • 原文地址:https://www.cnblogs.com/xinfang520/p/12777748.html
Copyright © 2020-2023  润新知