Сохранение переменных cURL -w в переменную bash - PullRequest
0 голосов
/ 16 января 2020

У меня есть команда типа

curl -kso /dev/null -w "dns lookup:%{time_namelookup}, tcp:%{time_connect}, ssl:%{time_appconnect} , total:%{time_starttransfer} )\n" https://www.google.com

Я хочу захватить переменные типа time_namelookup, time_connect, time_starttransfer, которые предоставляются cURL (как показано в фрагменте справочной страницы ниже), в переменной bash, чтобы я мог использовать ее дальше.

Я пытался использовать команду read, но это не помогает. Я пробовал что-то вроде этого

curl -kso /dev/null -w "dns lookup:%{time_namelookup}, tcp:%{time_connect}, ssl:%{time_appconnect} , total:%{time_starttransfer} $(something=%{time_starttransfer})\n" https://www.google.com

, но переменная something пуста.

Пожалуйста, предложите, как это должно продолжаться.

   -w, --write-out <format>
          Defines what to display on stdout after a completed  and  suc‐
          cessful  operation.  The  format  is a string that may contain
          plain text mixed with any number of variables. The string  can
          be  specified  as "string", to get read from a particular file
          you specify it "@filename" and to tell curl to read the format
          from stdin you write "@-".

          The variables present in the output format will be substituted
          by the value or text that curl thinks fit, as described below.
          All  variables are specified as %{variable_name} and to output
          a normal % you just write them as %%. You can output a newline
          by  using  \n,  a carriage return with \r and a tab space with
          \t.

          NOTE: The %-symbol is a special symbol in  the  win32-environ‐
          ment,  where  all  occurrences of % must be doubled when using
          this option.

          The variables available are:

          content_type   The Content-Type of the requested document,  if
                         there was any.

          filename_effective
                         The  ultimate filename that curl writes out to.
                         This is only meaningful  if  curl  is  told  to
                         write  to  a  file  with  the  --remote-name or
                         --output option. It's most useful  in  combina‐
                         tion   with  the  --remote-header-name  option.
                         (Added in 7.25.1)

          ftp_entry_path The initial path curl ended up in when  logging
                         on to the remote FTP server. (Added in 7.15.4)

          http_code      The  numerical  response code that was found in
                         the last retrieved HTTP(S) or FTP(s)  transfer.
                         In  7.18.2 the alias response_code was added to
                         show the same info.

          http_connect   The numerical code that was found in  the  last
                         response  (from  a  proxy)  to  a  curl CONNECT
                         request. (Added in 7.12.4)

          local_ip       The IP address of the local  end  of  the  most
                         recently  done  connection - can be either IPv4
                         or IPv6 (Added in 7.29.0)

          local_port     The local port number of the most recently done
                         connection (Added in 7.29.0)

          num_connects   Number  of  new  connects  made  in  the recent
                         transfer. (Added in 7.12.3)

          num_redirects  Number of redirects that were followed  in  the
                         request. (Added in 7.12.3)

          redirect_url   When  an  HTTP  request  was made without -L to
                         follow redirects, this variable will  show  the
                         actual URL a redirect would take you to. (Added
                         in 7.18.2)

          remote_ip      The remote IP address of the most recently done
                         connection  - can be either IPv4 or IPv6 (Added
                         in 7.29.0)

          remote_port    The remote port number  of  the  most  recently
                         done connection (Added in 7.29.0)

          size_download  The total amount of bytes that were downloaded.

          size_header    The  total  amount  of  bytes of the downloaded
                         headers.

          size_request   The total amount of bytes that were sent in the
                         HTTP request.

          size_upload    The total amount of bytes that were uploaded.
 [...]

Редактировать: я вижу различные методы для захвата переменных, но при захвате выходные данные данной команды не должны быть изменены. Короче говоря, я пытаюсь перехватить переменные, не меняя вывод данной команды.

Ответы [ 3 ]

1 голос
/ 16 января 2020

Это невозможно, так как каждая команда запускается в своей собственной оболочке, создает собственные переменные и так далее. Попробуйте этот код:

export somevar=test; curl -kso /dev/null -w "$(export somevar=%{time_connect}; env | grep somevar)\n" https://www.google.com; env | grep somevar

Выход (в моем случае) будет:

somevar=0,004898
somevar=test

Таким образом, способ сделать это - вывести напрямую в var.

somevar=$(curl -kso /dev/null -w "%{time_connect}" https://www.google.com)

$ echo $somevar
0,005093

Или в массив, если вам нужно несколько элементов.

somearray=( $(curl -kso /dev/null -w "%{time_connect} %{time_namelookup} %{time_appconnect}" https://www.google.com) )

$ echo ${somearray[@]}
0,005095 0,004317 0,165347
0 голосов
/ 16 января 2020

Вы можете использовать файл формата и использовать его в curl.

curl-format.txt:


        time_namelookup:  %{time_namelookup}\n
           time_connect:  %{time_connect}\n
        time_appconnect:  %{time_appconnect}\n
       time_pretransfer:  %{time_pretransfer}\n
          time_redirect:  %{time_redirect}\n
     time_starttransfer:  %{time_starttransfer}\n
                        ----------\n
             time_total:  %{time_total}\n
              http_code: %{http_code}\n

check_stats. sh:


    CURL='/usr/bin/curl'
    CURL_OPTS="-w "@curl-format.txt" -sS"
    URL="https://www.google.com"
    STATS=$(${CURL}  ${CURL_OPTS} ${URL})
    STATS_ARRAY=($( echo $STATS | ${SED} ':a;N;$!ba;s/\r\n/ /g' ))
    time_namelookup=${STATS_ARRAY[1]}
    time_connect=${STATS_ARRAY[3]}
    time_appconnect=${STATS_ARRAY[5]}
    time_pretransfer=${STATS_ARRAY[7]}
    time_redirect=${STATS_ARRAY[9]}
    time_starttransfer=${STATS_ARRAY[11]}
    time_total=${STATS_ARRAY[14]}
    http_code=${STATS_ARRAY[16]}

0 голосов
/ 16 января 2020

Вот способ сделать это на основе Linux bash: присвоение нескольких переменных

read -r time_namelookup time_connect time_appconnect time_starttransfer<<<$(curl -kso /dev/null -w "%{time_namelookup} %{time_connect} %{time_appconnect} %{time_starttransfer}" https://www.google.com)

Переменные:

echo dns lookup:$time_namelookup, tcp:$time_connect, ssl:$time_appconnect, total:$time_starttransfer

Вывод:

dns lookup:0.004143, tcp:0.044270, ssl:0.112656, total:0.195420
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...