dongyang0005 2015-10-08 10:53
浏览 22
已采纳

提交表单作为cron作业的一部分

I have a url that I need to visit as part of a wider process on a project, I know that it works when I am logged in but obviously as part of the cron job it wouldn't be. If it were htaccess I would simply either use curl or wget and pass the username and password parameters accepted.

I have tried this already on this particular cron but it didn't seem to perform the task that the url is associated with. See example below:

curl -u username:password http://www.example.com (I would usually have the dev/null 2>&1 as part of the cron but I wish to see the output for now)

The problem is however that this page sits behind a form login and I am unsure of how to pass parameters to that form using a cron job.

Any help or advice would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douzi2778 2015-10-08 11:02
    关注

    Using Curl:

    You will need to pass the form login parameters, probably using the POST method. Check the form's HTML to be sure.

    To do a POST request with curl, see https://superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request.

    This might not work for some forms, which implement CSRF. To work-around this, you would need to parse the HTML, find the CSRF token, and pass it as one of the POST request's data parameters.

    Next, the login most likely returns a cookie. Your browser normally saves this, and gives the cookie back to the website on each page request. You will need to specify a cookie file. See Send cookies with curl.

    There may be some investigation to work-around any more complicated login schemes, depending on the website.

    Using an automated web-browser

    The much easier alternative, is to use an automated browser, like Selenium webdriver. There are scripting interfaces you can use, like Capybara (a ruby gem). Using Capybara and Selenium to control a browser, you can avoid any techniques that websites might have which makes using CURL difficult (eg. if they detect and block bots).

    The disadvantage is that you need to install it. However, once you do, you can use simple commands to do stuff, eg visit('http://www.google.com'), click_link('Link Text'), ...

    Also, see:

    require 'capybara'
    
    session = Capybara::Session.new(:webkit, my_rack_app)
    session.within("//form[@id='session']") do
      session.fill_in 'Email', :with => 'user@example.com'
      session.fill_in 'Password', :with => 'password'
    end
    session.click_button 'Sign in'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路