i'm trying to create a custom program (or a script) to login to a web page and开发者_运维问答 then download a file. The login page has a form with 2 text fields (username and password) and a hidden field "authenticity_token". I've read about authenticity token.
I tried a custom bash script using curl but it doesn't work. Can anyone please take a look at my code and tell me what is wrong with it?
#!/bin/bash
#Pulls the login page and strips out the auth token
authToken=`curl -L -c cookies.txt --silent 'http://xxx.xxxxx.xx' | grep authenticity_token | awk -F'value="' '{print $2;}' | awk -F'="' '{print $1;}'`
echo $authToken
# Posts all the pre-URI-encoded stuff and appends the URI-encoded auth token
curl -L -c cookies.txt -b cookies.txt --data-urlencode authenticity_token=$authToken --data-urlencode 'username=my-username' --data-urlencode 'password=my-pass' 'http://xxx.xxxxx.xxx/login'
# access the page i want
curl -b cookies 'http://xxx.xxxxx.xxx/page_i_want_to_access'
When i use my browser to login to the webpage, if the loging is successful the server redirects me to the next page but when i use my script the server replies with packet whose header is "200 OK" instead of a "302 redirect" (I'm using wireshark to capture the pakets). So i think the authentication is not working.
Any suggestions please?
精彩评论