检查shell脚本内的Bitbucket登录凭证 [英] Check Bitbucket login credentials inside shell script

查看:340
本文介绍了检查shell脚本内的Bitbucket登录凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个shell / bash脚本来自动化一个工作流程。这个bash脚本可以克隆项目并在Bitbucket上创建新的回购,做作曲者安装/更新和更多的东西。

我的第一个计划是通过SSH完成这项工作,但有些情况下我需要HTTPS。对于所有人认为,经过HTTPS我需要首先检查Bitbucket的用户凭据。凭据由用户名和密码组成。



这是可能的。如果是这样,怎么做?

正如您在评论中所建议的那样, curl 可以为您执行HTTP基本身份验证。对于BitBucket,响应将是 401 Unauthorized 200 OK ,具体取决于用户名/密码对的有效性。你可以使用 grep 来测试输出(或者只有头文件),但更强大的方法是使用 -w / - write-out 选项,并结合 HEAD 请求和 -s 静音输出:

  http_status = $(curl -X HEAD -s -w'%{http_code}'\ 
-uusername:password -HContent-Type:application / json\
https://api.bitbucket.org/2.0/repositories/$repo_owner)


  if [[$ http_status == 200]];那么
echoCredentials valid
else
if [[$ http_status == 401]];然后
echoCredentials INVALID
else
echo非预期的HTTP状态码:$ http_status
fi
fi

或者,如果您计划测试多个状态代码,则可以使用 case 命令例如:

  case $ http_status in 
200)echoCredentials valid;;
301 | 302)echoAPI端点已更改;;
401)echoCredentials INVALID;;
5 *)echoBitBucket内部服务器错误;;
*)echo意外的HTTP状态码:$ http_status;;
esac


I'm currently writing a shell/bash script to automate a workflow. This bash script can clone projects and create new repo's on Bitbucket, do composer install/update's and more of that stuff.

My first plan was to do this all over SSH, but there are some situations that I need HTTPS. For all thinks that go's over HTTPS I need to check the user credentials for Bitbucket first. The credentials consisting of a username and password.

Is this possible. If so, how?

解决方案

As you suggested in comments, curl can do HTTP Basic Auth for you. For BitBucket, the response will be 401 Unauthorized or 200 OK, depending on the validity of username/password pair. You could test the output (or only headers) with grep, but a little bit more robust approach would be to use the -w/--write-out option, combined with a HEAD request and -silenced output:

http_status=$(curl -X HEAD -s -w '%{http_code}' \
              -u "username:password" -H "Content-Type: application/json" \
              https://api.bitbucket.org/2.0/repositories/$repo_owner)

Then, to test the status, you can use a simple conditional expression:

if [[ $http_status == 200 ]]; then
    echo "Credentials valid"
else
    if [[ $http_status == 401 ]]; then
        echo "Credentials INVALID"
    else
        echo "Unexpected HTTP status code: $http_status"
    fi
fi

Or, if you plan on testing multiple status codes, you can use the case command, for example:

case $http_status in
    200) echo "Credentials valid";;
    301|302) echo "API endpoint changed";;
    401) echo "Credentials INVALID";;
    5*) echo "BitBucket Internal server error";;
    *) echo "Unexpected HTTP status code: $http_status";;
esac

这篇关于检查shell脚本内的Bitbucket登录凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆