sh 使用用户名和密码将公共ssh密钥上传到gitlab

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 使用用户名和密码将公共ssh密钥上传到gitlab相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env bash

set -e

# Colors
red='\033[0;31m'
no_color='\033[0m'

# Inputs
gitlab_host=$1
gitlab_email=$2
gitlab_password=$3

if [[ -z "${gitlab_host}" ]]; then
    echo -e "${red}Missing argument 1 for gitlab host${no_color}"
    exit 1
fi

if [[ -z "${gitlab_email}" ]]; then
    echo -e "${red}Missing argument 2 for gitlab email${no_color}"
    exit 1
fi

if [[ -z "${gitlab_password}" ]]; then
    echo -e "${red}Missing argument 3 for gitlab password${no_color}"
    exit 1
fi

# curl for the login page to get a session cookie and the sources with the auth tokens
body_header=$(curl -L -s \
-c /tmp/gitlab-cookies.txt \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' \
-H 'Accept-Encoding: gzip;q=0, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.9,es;q=0.8' \
-H 'Connection: keep-alive' \
-H "Host: ${gitlab_host}" \
-H 'Upgrade-Insecure-Requests: 1' \
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" \
-i "https://${gitlab_host}/users/sign_in" \
-s)

# grep the authenticity token for the user login for
csrf_token=$(echo ${body_header} | perl -ne 'print "$1\n" if /new_user.*?authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p)

# grep the gitlab session
gitlab_session=$(cat /tmp/gitlab-cookies.txt | perl -ne 'print "$1\n" if /[[:blank:]]_gitlab_session[[:blank:]](.+$)/' | sed -n 1p)

# send login credentials with curl, using cookies and token from previous request to get logged in cookie
curl -L -s \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' \
-H 'Accept-Encoding: gzip;q=0, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.9,es;q=0.8' \
-H 'Cache-Control: max-age=0' \
-H 'Connection: keep-alive' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H "Cookie: _gitlab_session=${gitlab_session}" \
-H "Host: ${gitlab_host}" \
-H "Origin: https://${gitlab_host}" \
-H "Referer: https://${gitlab_host}/users/sign_in" \
-H 'Upgrade-Insecure-Requests: 1' \
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" \
-b /tmp/gitlab-cookies.txt \
-c /tmp/gitlab-cookies.txt \
-i "https://${gitlab_host}/users/sign_in" \
    --data "user[login]=${gitlab_email}&user[password]=${gitlab_password}" \
    --data-urlencode "authenticity_token=${csrf_token}" \
    -o /dev/null

# send curl GET request to personal access token page to get authenticity token
body_header=$(curl -L -s \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' \
-H 'Accept-Encoding: gzip;q=0, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.9,es;q=0.8' \
-H 'Cache-Control: max-age=0' \
-H 'Connection: keep-alive' \
-H "Cookie: _gitlab_session=${gitlab_session}" \
-H "Host: ${gitlab_host}" \
-H 'Upgrade-Insecure-Requests: 1' \
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" \
-b /tmp/gitlab-cookies.txt \
-c /tmp/gitlab-cookies.txt \
-i "https://${gitlab_host}/profile/keys" \
-s)
csrf_token=$(echo ${body_header} | perl -ne 'print "$1\n" if /authenticity_token"[[:blank:]]value="(.+?)"/' | sed -n 1p)

sleep 1

public_key_name="Auto Generated on $(date) by $0"
curl -L -s \
-X 'POST' \
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' \
-H 'Accept-Encoding: gzip;q=0, deflate, br' \
-H 'Accept-Language: en-US,en;q=0.9,es;q=0.8' \
-H 'Cache-Control: max-age=0' \
-H 'Connection: keep-alive' \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Cookie: _gitlab_session=${gitlab_session}" \
-H "Host: ${gitlab_host}" \
-H "Origin: https://${gitlab_host}" \
-H "Referer: https://${gitlab_host}/profile/keys" \
-H 'Upgrade-Insecure-Requests: 1' \
-H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36" \
--data-urlencode "utf8=✓" \
--data-urlencode "authenticity_token=${csrf_token}" \
--data-urlencode "key[title]=${public_key_name}" \
--data-urlencode "key[key]=$(cat ~/.ssh/surge.pub)" \
-b /tmp/gitlab-cookies.txt \
-s \
"https://${gitlab_host}/profile/keys" \
-o /dev/null

以上是关于sh 使用用户名和密码将公共ssh密钥上传到gitlab的主要内容,如果未能解决你的问题,请参考以下文章

sh 将SSH密钥添加到代理。此步骤也可用于避免在执行git push / pull时输入SSH密码

ssh 密钥密码在 Windows 中有效,但在 linux 中无效

sh 将密码添加到SSH密钥

【git】增加新用户和上传新项目

以及git如何使用ssh密钥(将ssh密钥添加到g

Git------应用TortoiseGit为github账号添加SSH keys