【问题标题】:Write log for curl on windows在 Windows 上为 curl 编写日志
【发布时间】:2015-02-12 03:30:03
【问题描述】:

我目前有一个运行curl 函数以从另一台服务器获取文件的脚本,我正在尝试调试此脚本的一些问题,并希望为此curl 函数启用日志记录。

我已经读到这可以使用--trace 文件开关来实现。

有人可以帮我在下面的代码中添加这个功能吗?

每个curl 调用的日志必须是唯一的,也许使用日期/时间调用?

脚本定义了以下变量:

strCodeBase = "E:\WebApps\LogHarvest\curl-7.40.0-win32\bin\"
strScriptSource = "LogHarvest.vbs"
strServerDir = "\\server.contoso.com\WebLogs\test\"

'  Determine current date, then calculate date-1 and separate Day, Month, and Year.
strDate = Date
strDate = DateAdd("d",-1,strDate)
strDay = Day(strDate)
strMonth = Month(strDate)
strYear = Year(strDate)

'  Get the two digit representation of the year.
strShortYear = Right(strYear,2)

'  For day numbers less than 10, append a leading 0.
If strDay < 10 Then
    strDay = "0" & strDay
End If

'  For month numbers less than 10, append a leading 0.
If strMonth < 10 Then
    strMonth = "0" & strMonth
End If

测试文件夹包含具有服务器名称的文件夹,例如 server2.contoso.com 和 server3.contoso.com

我已经根据下面提供的建议更新了代码,我现在收到一个错误

E:\WebApps\LogHarvest\basic.vbs(94, 3) (null):系统找不到文件s 指定。

第 94,3 行是:objShell.Run strcURL,,true

'  Build the cURL command line.
strcURL = chr(34) & strCodeBase & "curl" & chr(34) & " -s -f --trace " & strTraceFile & " -o "
strcURL = strcURL & chr(34) & strServerDir & strServer & "\gr" & strShortYear & strMonth & strDay & ".zip" & chr(34) & " "
strcURL = strcURL & "http://" & strServer & "/weblogs/gr" & strShortYear & strMonth & strDay & ".zip"

'Echo to see strcURL
Wscript.Echo strcURL    

回声返回

"E:\WebApps\LogHarvest\curl-7.40.0-win32\bin\curl" -s -f --trace trace20150211191943.log -o "\\server.contoso.com\WebLogs\test\server2.contoso.com\gr150210.zip" http://server2.contoso.com/weblogs/gr150210.zip

从cmd运行上述命令运行正常,提示脚本在别处失败。

Private Function FunDateTime( byVal dtmDateTime)
' input: any value of (or convertible to) 'date' TypeName
' returns YYYYmmddHHmmss string (14 chars)
FunDateTime = YEAR( dtmDateTime) & _
  Right( "00" & Month( dtmDateTime), 2) & _
  Right( "00" & Day( dtmDateTime), 2) & _
  Right( "00" & Hour( dtmDateTime), 2) & _
  Right( "00" & Minute( dtmDateTime), 2) & _
  Right( "00" & Second( dtmDateTime), 2)
End Function
'
' Examples:
' FunDateTime(  Now) returns YYYYmmddHHmmss 
' FunDateTime( Date) returns YYYYmmdd000000, i.e. HHmmss   == 000000
' FunDateTime( Time) returns 18991230HHmmss, i.e. YYYYmmdd == 18991230
' FunDateTime(  365) returns 19001230000000, i.e. 365 days since above date
' FunDateTime( "xy") results to 'Type mismatch' runtime error

【问题讨论】:

  • 您的问题主要是关于curlhow-to,还是vbscripthow-to
  • 我会说一个 vbscript 操作方法
  • wscript.echo strcURL 的输出呢?拜托add examples to your question:看起来怎么样,应该怎么样。
  • 这将运行 curl.exe contoso1.domain.com\w3svc01\gr150204.zip,它将文件 gr150204.zip 拉到当前文件夹中。一个 --trace 开关会给我类似 logfile.log 的东西,我希望 curl 在完成后将此函数写入日志文件,然后使用当前时间戳重命名该日志文件,以便在下次运行时不会被覆盖跨度>
  • 对于不言自明的The system cannot find the file specified 消息,无需多言。 指定的文件很可能是curl.exe"E:\WebApps\LogHarvest\curl-7.40.0-win32\bin\"目录下...Run期望提供的字符串与在命令行窗口中写入的字符串相同。

标签: curl vbscript


【解决方案1】:

假设您的代码 sn-p 的正确性(因为我不知道 strCodeBasestrServerDirstrServer、... 变量的内容):

'  Build the cURL command line.
strTraceFile = "trace" & FunDateTime( Now) & ".txt"
strcURL = chr(34) & strCodeBase & "curl" & chr(34) _
    & " -s -f --trace " & strTraceFile & " -o "
strcURL = strcURL & chr(34) & strServerDir & strServer & "\gr" _
    & strShortYear & strMonth & strDay & ".zip" & chr(34) & " "
'Echo to see strcURL
Wscript.Echo strcURL 

Private Function FunDateTime( byVal dtmDateTime)
' input: any value of (or convertible to) 'date' TypeName
' returns YYYYmmddHHmmss string (14 chars)
FunDateTime = YEAR( dtmDateTime) & _
  Right( "00" & Month( dtmDateTime), 2) & _
  Right( "00" & Day( dtmDateTime), 2) & _
  Right( "00" & Hour( dtmDateTime), 2) & _
  Right( "00" & Minute( dtmDateTime), 2) & _
  Right( "00" & Second( dtmDateTime), 2)
End Function
'
' Examples:
' FunDateTime(  Now) returns YYYYmmddHHmmss 
' FunDateTime( Date) returns YYYYmmdd000000, i.e. HHmmss   == 000000
' FunDateTime( Time) returns 18991230HHmmss, i.e. YYYYmmdd == 18991230
' FunDateTime(  365) returns 19001230000000, i.e. 365 days since above date
' FunDateTime( "xy") results to 'Type mismatch' runtime error

Resource:要获取有关 curl 功能的更多详细信息和信息,请尝试使用带有给定文件名的 --trace--trace-ascii 选项进行登录,如下所示:

    curl --trace trace.txt www.haxx.se

【讨论】:

  • 代码中有重要下划线_结束了一些行
  • JosefZ,感谢您的回复。我尝试将您的代码添加到我的脚本中,它会引发以下错误:codelogharvest.vbs(206, 39) Microsoft VBScript 编译错误:Syntaxerror code 这是 FunDateTime = YEAR(dtmDateTime) &
  • 1. 不要在 cmets 中发布代码 sn-ps。它变得有点难以阅读,因为你可以看到自己......Update your question instead。 2.没有人可以帮助你不知道所有使用的变量内容(见我的回答)。如需帮助,请阅读How to create a Minimal, Complete, and Verifiable example。 3.Wscript.Echo strcURL发布结果
  • Josef,我已经用丢失的变量更新了我的问题,我希望它现在更有意义,但是我无法获得 Echo 的输出,因为脚本在 echo 之前失败.
      - 私有函数是否应该出现在 curl 代码之前? - 我相信,curl 语法也应该是“curl domain --trace tracefile”。
  • 所有Function .. End Function 定义代码可以放在脚本中的任何位置,但在下一个重要关键条件下:您不能在任何其他过程中定义Function 过程(例如SubProperty Get)。否则你会得到 Microsoft VBScript 编译错误:语法错误。 HTH
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 2010-09-11
  • 1970-01-01
  • 2012-06-18
  • 2021-11-12
  • 2013-11-15
相关资源
最近更新 更多