Log date and external ip address with curl and batch (Windows)

0

I am having weird issues where my external IP is changed VERY often as in every 2, 3 minutes.

I have a dynamic dns service in place that I update via task scheduler with curl called in a batch file.

I'm in the process of debugging that and to do so I'd need to log(append) the date and time and each response from CURL to a file.

I am able to echo to a file, but have no clue how to append date/time and curl response.

The environment is ws2012.

Thanks in advance! Here's the snippet I have in at the moment

curl -k http://freedns.afraid.org/dynamic/update.php?xdfSDASDQW232FFD
ECHO I ran! I really did! >c:\TestBat.log

grumpygamer

Posted 2015-03-19T14:23:39.160

Reputation: 3

Could you post a snippet of your current script? – Josh – 2015-03-19T14:30:40.650

added it at the top! – grumpygamer – 2015-03-19T14:33:22.487

Answers

0

Batch solution, appends to log file. Change >> to > to overwrite:

@echo off
set logfile=.\curl.log

echo [ %date% %time% ]>>%logfile%
curl -s -k http://freedns.afraid.org/dynamic/update.php?xdfSDASDQW232FFD>>%logfile%

beatcracker

Posted 2015-03-19T14:23:39.160

Reputation: 2 334

it should write the curl response too, but thanks – grumpygamer – 2015-03-19T14:54:37.387

@grumpygamer Can be done, see update. – beatcracker – 2015-03-19T15:02:26.187

1

I recommend skipping curl all together and using Powershell instead since that is native WS technology and can be used to send a HTTP POST as well. Here's a simple two-liner that logs your external IP and the current time to the file C:\temp\debuglog.txt:

$response = Invoke-WebRequest -UseBasicParsing -Uri http://icanhazip.com -Method get
"{0};{1}" -f $response.content.trim(), (get-date -format u) | out-file -filepath 'C:\temp\debuglog.txt' -append

Put this in a file that ends in .ps1 and run it via task scheduler:

Program: powershell
Arguments: -executionpolicy bypass C:\temp\debugscript.ps1

megamorf

Posted 2015-03-19T14:23:39.160

Reputation: 1 494

This is awesome but it should also update the DNS at afraid.org; maybe this should be

$response = Invoke-WebRequest -UseBasicParsing -Uri http://freedns.afraid.org/dynamic/update.php?xdfSDASDQW232FFD -Method get "{0};{1}" -f $response.content.trim(), (get-date -format u) | out-file -filepath 'C:\temp\debuglog.txt' -append – grumpygamer – 2015-03-19T15:02:25.720

Yes, I believe I posted this before you had added your command to your question :-) Feel free to accept beatcracker's or my answer if you think that solves your problem. – megamorf – 2015-03-19T15:04:42.610