用于登录网站并下载文件的 Bash 脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11840465/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script to login to website and download file
提问by Marc S.
I am a bit stuck with my bash script. I need it to login to a website which https based... it needs to login with a username and password and then it needs to locate a particular link, the text of the link is always the same but the location it points to changes, it needs to grab that location and download it with wget.
我对我的 bash 脚本有点困惑。我需要它登录到一个基于 https 的网站......它需要使用用户名和密码登录,然后它需要找到一个特定的链接,链接的文本总是相同的,但它指向的位置发生了变化,它需要获取该位置并使用 wget 下载它。
Anhbody have any tips, i need it to be portable so i prefer not to rely on external programs..
Anhbody 有任何提示,我需要它是便携的,所以我不想依赖外部程序。
Thank you
谢谢
回答by Zsolt Botykai
bashis not ideal for those kind of tasks. Although you can try something like:
bash不适合这类任务。尽管您可以尝试以下操作:
curl --user name:password https://www.example.com/
But if you need to find a link on the page, you can try:
但是如果你需要在页面上找到一个链接,你可以尝试:
curl --user name:password https://www.example.com/ | grep WHAT_EVER_IDENTIFIES_LINK
Then get it's output via curlagain. 
然后通过curl再次获取它的输出。
But I would recommend something like mechanizefor the task. There are simillar libraries for python and Ruby, etc.
但我会推荐像机械化这样的任务。python 和 Ruby 等都有类似的库。
回答by John V
This code works to login to the website, but I am not sure how to proceed to identify the link and wget it...
此代码可用于登录该网站,但我不确定如何继续识别链接并获取它...
#!/bin/bash
#REQUIRED PARAMS
username=""
password=""
#EXTRA OPTIONS
uagent="Mozilla/5.0" #user agent (fake a browser)
sleeptime=0 #add pause between requests
touch "cookie.txt" #create a temp. cookie file
#INITIAL PAGE
echo "[+] Fetching" && sleep $sleeptime
initpage=`curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent"                "https://ny2.free2surfvpn.com/?src=connect"`
token=`echo "$initpage" | grep "authenticity_token" | sed -e 's/.*value="//' | sed -e   's/" \/>.*//'`
#LOGIN
echo "[+] Submitting the login form..." && sleep $sleeptime
loginpage=`curl -s -b "cookie.txt" -c "cookie.txt" -L --sslv3 -A "$uagent" -d     "authenticity_token=$token&username=$username&password=$password"     "https://mobile.twitter.com/session"`
#HOME PAGE
echo "[+] Getting page" && sleep $sleeptime
homepage=`curl -s -b "cookie.txt" -c "cookie.txt" -L -A "$uagent"     "https://ny2.free2surfvpn.com/?src=connect"`
rm "cookie.txt"

