2018-01-07 01:14:34
[CURL] Are you scripting with curl and do you need to login to a website before doing some operation on a particular page?
You may need a mix of the following features of curl:
- UserAgent declaration. list of user agents
Some web sites won't allow you interaction or present a captcha if you don't set a common browser (e.g. firefox) UA
- Cookie management
You usually need to store and present the correct cookies to be logged to a page
- Content Type declaration
Normally when you do a POST with some data you must submit it as "x-www-form-urlencoded"
A typical curl usage can be
curl -A "UserAgent string foobar browser" -c /tmp/cookies.txt -L https://www.yourwebsite.com/login.php
curl will create a 'cookie-jar' at /tmp/cookies.txt where it stores all cookies received from the yourwebsite. Then you can use in your script a command like: curl -A "UserAgent string foobar browser" -c /tmp/cookies.txt -b /tmp/cookies.txt -L https://www.yourwebsite.com/anotherpage.php
to pass the cookies you saved on another page to have the correct interaction.
Last but not least consider using method POST to send login information to the page you want to interact with (i.e. login.php), like
curl -s -L -k -A "UserAgent string foobar browser" -X POST -H "Content-Type:application/x-www-form-urlencoded" -d "variablename=variablevalue" -c /tmp/cookies.txt -b /tmp/cookies.txt https://www.yourwebsite.com/login.php
To get a list of valid variables in the web page you want to access, you may first attempt a manual login with Firefox, inspecting it with the Web Developers Tools (F12 key). Look at the 'Network' tab during a login and you can get a variable list and also retrieve a curl string to reproduce it.
Combining multiple curl in your script you can mimick a complete real login to a website and in most cases you will be able to automate your operations.
I hope you found this useful! Share the channel with your friends: http://t.me/linuxcheatsheet
Bye!
G.
387 viewsedited 22:14