开发手册 欢迎您!
软件开发者资料库

Linux 使用bash脚本登陆网站方法及示例代码

本文主要介绍Linux中,通过bash脚本的方式实现网站访问登陆的方法,以及相关的示例代码。

使用openssl

#!/bin/bashshopt -s extglobURL='https://stackoverflow.com/'IFS=: read  -r user pass < <(gpg -qd 

后台运行openssl:

exec {wwwE}<> <(: - O)exec {wwwI}<> <(: - b)exec {wwwO}< <(  exec stdbuf -o0 openssl s_client -quiet -connect "$hst":443 <&$wwwI 2>&$wwwE)osslpid=$!

doReq函数可以创建 2 个变量:$cookieand $htstatus和 3 个数组:$hthead, $htbody 和 $hterr:

doReq() {    hthead=() htbody=() hterr=()    local target=$1 method=${2:-GET} head=true line cookies    printf >&$wwwI '%s\r\n' "$method $target HTTP/1.1" "Host: $hst" \           "User-Agent: aobs/0.01" "Connection: keep-alive" "Accept: */*"    [ "$cookie" ] && printf >&$wwwI '%s' "$cookie"    if [ "$method" = "POST" ];then        printf >&$wwwI '%s\r\n%s\r\n\r\n%s' "Content-Length: ${#3}" \                       'Content-Type: application/x-www-form-urlencoded' "$3"    else printf >&$wwwI '\r\n'    fi    read -t 10 -ru $wwwO line    htstatus=${line%$'\r'} ; hthead=("$htstatus")    while read -t .3 -ru $wwwO line;do        [ "${line%$'\r'}" ] || head=false;        if $head ;then            hthead+=("${line%$'\r'}");            case $line in                [sS]et-[cC]ookie:* ) line=${line#*: };                                 cookies+=("${line%%;*}");;            esac        else htbody+=("${line%$'\r'}") ;fi    done    if read -t 0 -ru $wwwE;then        while read -t .1 -ru $wwwE line;do            hterr+=("${line%$'\r'}")            case $line in                depth* | verify* ) ;;                * ) echo "ERR: $line" ;;    esac ; done ; fi    [ ! -v "cookie" ] && [ "${cookies[0]}" ] &&        printf -v cookie 'Cookie: %s\r\n' "${cookies[@]}"}

登陆:

doReq /users/login POST "email=$user&password=$pass"

显示网站信息:

doReq /
for ((i=${#htbody[@]};i--;)) ;do
line="${htbody[i]}"
case $line in
*badge1* ) line="${htbody[i-1]}${htbody[i]}${htbody[i+1]}"
line=${line//>+([0-9])<} line=${line//<*([^>])>}
printf '%b\n' "${line//●/ \\U25cf }" ;;
esac ; done

现在可以随时运行doReq,因为连接保持打开状态。完成后,在退出之前,可以停止openssl并关闭您的 fd:

ls -l /dev/fd/ ; ps --tty $(tty) ufw
kill $osslpid
exec {wwwE}<&-
exec {wwwI}>&-
exec {wwwO}<&-
ls -l /dev/fd/ ; ps --tty $(tty) ufw

getSo.sh脚本内容:

#!/bin/bash# getSo.sh - Sample HTTPS client w/Connection_Keep-Alive, using OpenSSL# (C) 2020 - F-Hauri.ch# Licensed by GNU GENERAL PUBLIC LICENSE Version 3shopt -s extglobURL='https://stackoverflow.com/'IFS=: read  -r user pass < <(gpg -qd  <(: - O)exec {wwwI}<> <(: - b)exec {wwwO}< <(  exec stdbuf -o0 openssl s_client -quiet -connect "$hst":443 <&$wwwI 2>&$wwwE)osslpid=$!doReq() {    hthead=() htbody=() hterr=()    local target=$1 method=${2:-GET} head=true line cookies    printf >&$wwwI '%s\r\n' \   "$method $target HTTP/1.1" \   "Host: $hst" \   "User-Agent: aobs/0.01" \   "Connection: keep-alive" \   "Accept: */*"    [ "$cookie" ] && printf >&$wwwI '%s' "$cookie"    if [ "$method" = "POST" ];thenprintf >&$wwwI '%s\r\n%s\r\n\r\n%s' \       "Content-Length: ${#3}" \       'Content-Type: application/x-www-form-urlencoded' \       \       "$3"    elseprintf >&$wwwI '\r\n'    fi    read -t 10 -ru $wwwO line    htstatus=${line%$'\r'}    hthead=("$htstatus")    while read -t .3 -ru $wwwO line;do[ "${line%$'\r'}" ] || head=false;if $head ;then    hthead+=("${line%$'\r'}");    case $line in[sS]et-[cC]ookie:* ) line=${line#*: };     cookies+=("${line%%;*}");;    esacelse    htbody+=("${line%$'\r'}") ;fi    done    if read -t 0 -ru $wwwE;thenwhile read -t .1 -ru $wwwE line;do    hterr+=("${line%$'\r'}")    case $line indepth* | verify* ) ;;* ) echo "ERR: $line" ;;    esacdone    fi    [ ! -v "cookie" ] && [ "${cookies[0]}" ] &&printf -v cookie 'Cookie: %s\r\n' "${cookies[@]}"}trUnicode() {     local lhs rhs chr;    local -n string=$1;    while [ "$string" ] && [ -z "${string//*&#*([0-9]);*}" ]; do        rhs=${string#*&#*([0-9]);}        lhs=${string%&#*([0-9]);$rhs}        chr=${string%;$rhs}        printf -v chr '\\U%X' "${chr#$lhs&#}"        printf -v chr %b "$chr"        string="$lhs $chr $rhs"    done}doReq /users/login POST "email=$user&password=$pass"while true;do    doReq /    out=''    for ((i=${#htbody[@]};i--;)) ;doline="${htbody[i]}"case $line in    *badge1* )startdiv=$i enddiv=$iwhile [ "${htbody[startdiv]//*
*}" ];do ((enddiv++));doneline=""for ((bdg=startdiv;bdg<=enddiv+1;bdg++)) ;do line+="${htbody[bdg]//*([$'\n\r\t']} "doneline=${line//>+([0-9])<}line=${line//<+([^>])>}line=${line//+([ ])/ }trUnicode lineout+=" $line";; *"title=\"your reputation:"* ) line=${line#*reputation:}out="Rep: ${line%%\"*} $out";;esac done if [ "$out" ] ;then[ "$out" = "$last" ] && printf '\e[A'printf "%(%d %b %T)T " -1echo "${out}"last="$out"printf -v tosleep '120-(%(%S)T%%20)' -1tosleep=$((tosleep))read -sn 1 -rt ${tosleep:1} _ && breakpenalty=0 else(( penalty++ > 10 )) && breakunset cookiedoReq /users/login POST "email=$user&password=$pass"read -rt 0 _ && break fidonels -l /dev/fd/ps --tty "$(tty)" ufwkill $osslpidexec {wwwE}<&-exec {wwwI}>&-exec {wwwO}<&-ls -l /dev/fd/ps --tty "$(tty)" ufw