简体   繁体   English

在bash脚本中使用cURL登录Javascript

[英]Javascript login using cURL in bash script

I am trying to write a bash script in which I need to login to a website which uses javascript in its form. 我正在尝试编写一个bash脚本,我需要登录到一个在其表单中使用javascript的网站。 I have never used cURL so any help would be appreciated. 我从未使用过cURL,所以任何帮助都会受到赞赏。 I know I need to use cookies and I have the http headers, but I don't know what I need to do with these. 我知道我需要使用cookie并且我有http标头,但我不知道我需要做些什么。

The headers are 标题是

Response Headers

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Content-Length  0
Content-Type    text/html;charset=UTF-8
Date    Thu, 17 May 2012 11:25:15 GMT
Expires Tue, 01 Jan 1980 00:00:00 GMT
Last-Modified   Thu, 17 May 2012 11:25:16 GMT
Pragma  no-cache
Server  Apache-Coyote/1.1
X-Powered-By    Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA  date=200807181417)/JBossWeb-2.0

Request Headers    
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding gzip, deflate
Accept-Language en-us,en;q=0.5
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie  SMLOGOUT=true; JSESSIONID=8D5757001A594D5CBB07C9250D1CB2B7; JSESSIONIDSSO=A0569CD1D6C981989F0FE691E9AFC314
Host    https:/example.com
Referer https://example.com
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
X-WCF-Fragment  true

Any help or pointing me in the right direction would be appreciated. 任何帮助或指向我正确的方向将不胜感激。 Thanks 谢谢

From the request header it can be easily seen that you are sending some post data. 从请求标题中可以很容易地看出您正在发送一些帖子数据。 But you didn't provide it. 但你没有提供它。 I give you a simple example on how a http request can be converted to curl command. 我举一个关于如何将http请求转换为curl命令的简单示例。

Suppose you have this request where you are posting 2 form variables var1 and var2 get POSTed to http://www.example.com/form.url . 假设您有这个请求,您要将2个表单变量var1var2发布到http://www.example.com/form.url The request would look like this, 请求看起来像这样,

POST /form.url HTTP/1.1
Host: www.example.com
Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8

var1=val1&var2=val2

When its converted to curl its like, 当它转变为卷曲时,

curl -d 'var1=val1&var2=val2' \
--header 'Accept:  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
--header 'Accept-Encoding gzip, deflate' \
--header 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' \
'http://www.example.com/form.url'

Note: 注意:

  1. This way you can add as much as header you want. 这样您就可以添加所需的标题。 But its better you add only the headers which is necessary. 但最好只添加必要的标题。 Because curl will pass most headers for you (eg Host , User-Agent , Content-Type , Accept etc). 因为curl会为你传递大多数标题(例如HostUser-AgentContent-TypeAccept等)。

  2. If you want to manage cookie add a file cookie.txt in the current directory and -b cookie.txt -c cookie.txt command switches to curl. 如果要管理cookie,请在当前目录中添加文件cookie.txt ,然后使用-b cookie.txt -c cookie.txt命令切换到curl。 Means, 手段,

     -b/--cookie <name=string/file> Cookie string or file to read cookies from (H) -c/--cookie-jar <file> Write cookies to this file after operation (H) 
  3. -d switch stands for the data string that will be passed in the request body. -d开关代表将在请求主体中传递的数据字符串。

     -d/--data <data> HTTP POST data (H) 

Now I hope you can build your command. 现在我希望你能建立自己的命令。

In http, there are two ways of sending data to a URL: 在http中,有两种方法可以将数据发送到URL:

  • POST POST
  • GET 得到

In GET , the data is sent as part of the URL. GET中 ,数据作为URL的一部分发送。 You may see a URL that looks like this: 您可能会看到如下所示的网址:

http://foo.com/bar/barfoo/script.js?param1=this&param2=that&param3=the_other

This URL is sending data to a JavaScript at http://foo.com/bar/barfoo/script.js . 此URL将数据发送到http://foo.com/bar/barfoo/script.js上的JavaScript。 It is sending to this script the following parameters: 它向此脚本发送以下参数:

  • param1 = this param1 = this
  • param2 = that param2 = that
  • param3 = the_other param3 = the_other

In a POST operation, the data is sent differently and is not encoded in the URL itself. POST操作中,数据以不同方式发送,并且不在URL本身中编码。 THis happens a lot when using web forms (like what you are trying to do). 使用Web表单时会发生很多事情(就像您要做的那样)。 You can use the --form parameter in curl to pass POST data much like you word in a HTTP form. 您可以在curl使用--form参数来传递POST数据,就像在HTTP表单中一样。

From the CURL manage: 从CURL管理:

-F/--form <name=content>
    (HTTP) This lets curl emulate a filled-in form in which a user has
    pressed the submit  button.  This  causes  curl  to  POST  data using
    the Content-Type multipart/form-data according to RFC 2388. This
    enables uploading of binary files etc. To force the 'content' part to
    be  a  file, prefix the file name with an @ sign. To just get the
    content part from a file, prefix the file name with the symbol <. The
    difference between @ and <  is  then  that  @  makes  a  file  get
    attached  in  the  post as a file upload, while the < makes a text
    field and just get the contents for that text field from a file.


Example, to send your password file to the server, where 'password' is the
name of  the  form- field to which /etc/passwd will be the input:

    curl -F password=@/etc/passwd www.mypasswords.com

To  read content from stdin instead of a file, use - as the filename. This
goes for both @ and < constructs.

You can also tell curl what Content-Type to use by using 'type=', in a
manner similar to:

    curl -F "web=@index.html;type=text/html" url.com

  or

    curl -F "name=daniel;type=text/foo" url.com

You can also explicitly change the name field of a file upload part by
setting filename=, like this:

    curl -F "file=@localfile;filename=nameinpost" url.com

Hope this helps a bit. 希望这个对你有帮助。

Sorry but curl doesn't do javascript. 对不起,但curl不做javascript。

consider 考虑

  • manually deciphering the javascript and replicating the behavior in a perl/ruby/python script 手动解密javascript并在perl / ruby​​ / python脚本中复制行为
  • employing selenium 使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM