Curl

如何使用 curl 登錄網路?

  • July 15, 2020
Testing site: http://testing-ground.scraping.pro/login
Username: admin
Password: 12345

捲曲幫助

-u, --user <user:password> Server user and password

這是網路表格

wolf@linux:~$ curl http://testing-ground.scraping.pro/login 2>/dev/null | sed -n '/<form/,/form>/'p
   <form action="login?mode=login" method="POST">
       <label for="usr">User name:</label>
       <input id="usr" name="usr" type="text" placeholder="enter 'admin' here">
       <label for="pwd">Password:</label>
       <input id="pwd" name="pwd" type="text" placeholder="enter '12345' here">
       <input type="submit" value="Login">
   </form>
wolf@linux:~$ 

沒有憑據的捲曲

wolf@linux:~$ curl http://testing-ground.scraping.pro/login?mode=login 2>/dev/null | egrep 'DENIED|WELCOME'
       <li>If you see <span class="success">WELCOME :)</span> then the user credentials were sent, the cookie was passed and HTTP redirect was processed</li>
       <li>If you see <span class="error">ACCESS DENIED!</span> then either you entered wrong credentials or they were not sent to the server properly</li>
<h3 class='error'>ACCESS DENIED!</h3><a href='login'><< GO BACK</a></div>
wolf@linux:~$ 

但是,帶有憑據的 curl-u admin:12345也顯示相同的頁面…

wolf@linux:~$ curl http://testing-ground.scraping.pro/login?mode=login -u admin:12345 2>/dev/null | egrep 'DENIED|WELCOME'
       <li>If you see <span class="success">WELCOME :)</span> then the user credentials were sent, the cookie was passed and HTTP redirect was processed</li>
       <li>If you see <span class="error">ACCESS DENIED!</span> then either you entered wrong credentials or they were not sent to the server properly</li>
<h3 class='error'>ACCESS DENIED!</h3><a href='login'><< GO BACK</a></div>
wolf@linux:~$ 

在這種情況下使用 curl 的正確方法是什麼?

這似乎有效:

curl -s -L --cookie-jar cookies.txt -d 'usr=admin&pwd=12345' http://testing-ground.scraping.pro/login?mode=login | grep -E 'DENIED|WELCOME'

輸出:

       <li>If you see <span class="success">WELCOME :)</span> then the user credentials were sent, the cookie was passed and HTTP redirect was processed</li>
       <li>If you see <span class="error">ACCESS DENIED!</span> then either you entered wrong credentials or they were not sent to the server properly</li>
<h3 class='success'>WELCOME :)</h3><a href='login'><< GO BACK</a></div>
  • -s沒有進度條的安靜模式。輸出被發送到標準輸出。
  • -L跟隨 302 重定向
  • --cookie-jar cookies.txt使用 cookie 並將 cookie 寫入文件cookies.txt
  • -d 'usr=admin&pwd=12345'POST 數據usr=adminpwd=12345

引用自:https://unix.stackexchange.com/questions/598627