利用curl发送post数据进行网络认证

int32位 posted @ Aug 26, 2013 08:01:23 PM in linux , 3228 阅读
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!

可以把脚本放到自启动脚本目录下, 这样不用每次进行网页认证. 另外对于没有图形界面的纯字符终端也非常有用. 还有就是当chrome打开时, 如果没有认证, 则会把所有已保存的页面重定向到登录页面, 造成非常麻烦!

#!/bin/bash
declare -f showHelp
showHelp()
{
cat <<EOF
usage: $0 [-u username] [-p password] [-i ip] [-h]
EOF
exit 1
}

# 解析参数
while getopts ":u:p:i:h" arg
do
	case $arg in
		u)
			user=$OPTARG
			;;
		p)
			password=$OPTARG
			;;
		h)
			showHelp
			;;
		i)
			ip=$OPTARG
			;;
		:|?|*)
			showHelp
			;;
	esac
done

# 如果没有获取参数,则交互式输入
if [ -z "$user" ]
then
	read -p"username:" user
fi

if [ -z "$password" ]
then
	stty -echo
	read -p"password:" password
	stty echo
	echo
fi

# 检查参数是否成功输入
[ -z "$user" ] && showHelp
[ -z "$password" ] && showHelp

# 发送post数据, 注意这里没有保存cookies
ip=${ip:-"10.3.8.211"} # 这里设置认证ip的缺省值
curl -A "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.0)" -d "username=$user&upass=$password&save_me=1&R1=0" "$ip"  -o output.html 2>/dev/null

# 本来想用nc的,结果发现即使不登录也会返回0 
# FIXME: 下面这段检测网络是否正确连接的代码,不健壮,不保证输出是正确结果, 运行后请手动ping
if ping -c 1 -w 2 baidu.com &>/dev/null
then
	echo "The internet is Ok!"
else
	echo "Fail to login!"
fi
转载请注明:http://krystism.is-programmer.com/若有错误,请多多指正,谢谢!
  • 无匹配
  • 无匹配
Clay Lowe 说:
2025年5月11日 02:17

Below you will understand what is important, the idea provides one of the links with an exciting site: Kissimmee Termite Control

Clay Lowe 说:
2025年5月11日 02:17

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. Orlando Exterminator

Clay Lowe 说:
2025年5月11日 02:19

I like to recommend exclusively fine plus efficient information and facts, hence notice it: Bed Bug Treatment Oviedo

Clay Lowe 说:
2025年7月19日 07:26

These websites are really needed, you can learn a lot.  traffic and truck driving citation

Clay Lowe 说:
2025年10月28日 19:36

In this case you will begin it is important, it again produces a web site a strong significant internet site: health and wellness retreats Mexico

Clay Lowe 说:
2025年10月28日 19:36

I like to recommend exclusively fine plus efficient information and facts, hence notice it: Fertility Treatments Mexico

Clay Lowe 说:
2025年10月28日 19:36

Why do only so much written on this subject? Here you see more.  integrative wellness coach Vancouver women

Sophia Andrade 说:
2025年12月25日 22:01

정보가 명확하고 정확해 보입니다. 출장마사지


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter