ssh与scp

 

 

 

 

function _ssh() {
        local user=$1
        local ip=$2
        local cmd=$3

        local quoted_cmd

        if [ "$user" != root ]; then
                quoted_cmd=$(quote "$cmd")
                cmd="sudo -sk <<< $quoted_cmd" #  support multicommands in one line
        fi

        echo "execute [$cmd] on $ip"

        if [ "$SSH_AUTH" = password ]; then
                sshpass -p "$SSH_PASSWORD" ssh -p "$SSH_PORT" -o StrictHostKeyChecking=no -o PreferredAuthentications=password,publickey "$user"@"$ip" "$cmd"
        elif [ "$SSH_AUTH" = pubkey ]; then
                sshpass -p "$SSH_PASSWORD" ssh -p "$SSH_PORT" -o StrictHostKeyChecking=no -o PreferredAuthentications=publickey,password -i "$SSH_KEY_FILE" "$user"@"$ip" "$cmd"
        else
                exit 1; "unsupport auth type [$SSH_AUTH], only password/pubkey is valid"
        fi
}

 

 _ssh "$user" "$ip" "mkdir -p ${DOCKER_DATA_PATH}"

 

 

 

function _scp() {
        local user=$1
        local ip=$2
        local local_path=$3
        local target_path=$4

        if [ "$SSH_AUTH" = password ]; then
                sshpass -p "$SSH_PASSWORD" scp -r -P "$SSH_PORT" -o StrictHostKeyChecking=no -o PreferredAuthentications=password,publickey "$local_path" "$user"@"$ip":"$target_path"
        elif [ "$SSH_AUTH" = pubkey ]; then
                sshpass -p "$SSH_PASSWORD" scp -r -P "$SSH_PORT" -o StrictHostKeyChecking=no -o PreferredAuthentications=publickey,password -i "$SSH_KEY_FILE" "$local_path" "$user"@"$ip":"$target_path"
        else
                exit 1;echo "unsupport auth type [$SSH_AUTH], only password/pubkey is valid"
        fi
}

 

 

 

 

 

 

 

 

 

############

posted @ 2023-06-05 17:39  igoodful  阅读(9)  评论(0编辑  收藏  举报