カールを使用してWebページにログインする方法は?

カールを使用してWebページにログインする方法は?
Testing site: http://testing-ground.scraping.pro/login
Username: admin
Password: 12345

カールヘルプ

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

Web フォームです。

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'>&lt;&lt;&nbsp;GO BACK</a></div>
wolf@linux:~$ 

ただし、資格情報を使用したカールにも-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'>&lt;&lt;&nbsp;GO BACK</a></div>
wolf@linux:~$ 

この状況でカールを使用する正しい方法は何ですか?

ベストアンサー1

これはうまくいくようです:

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'>&lt;&lt;&nbsp;GO BACK</a></div>
  • -s自動モード、進行状況バーなし。出力は標準出力に送信されます。
  • -L302リダイレクトに従う
  • --cookie-jar cookies.txtCookieの使用とCookieをファイルに書き込むcookies.txt
  • -d 'usr=admin&pwd=12345'データを公開usr=adminするpwd=12345

おすすめ記事