3

I want to get the User Agent HTTP request header string from a new Google Chrome browser session (just opened) from bash and put it in a variable.

Here is the pseudo-code:

USER_AGENT="$(google-chrome --user-agent)"
echo "$USER_AGENT"

Output example:

Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36

If it is not possible to do this with the google-chrome command, what is an equivalent workaround?

Mario Palumbo
  • 693
  • 8
  • 32
  • Ok, I changed the question, now it should be more explanatory. It took me an hour to make it as understandable as possible. – Mario Palumbo Sep 05 '22 at 14:01

3 Answers3

4

Chrome in headless mode with the --repl command line option evaluates Javascript expressions. You could try something like the following to get the user agent string:

echo navigator.userAgent | /opt/google/chrome/chrome --headless --repl 2> /dev/null | sed 's/^>>> //' | jq -r .result.value

Note: Do run the chrome binary (e.g. /opt/google/chrome/chrome) directly rather than the google-chrome shell script, because it seems that the shell script does not support passing the standard input through.

The sed command removes the prompt from the output and the jq command extracts the value from the JSON string that Chrome prints.

Note however that the user agent string when running Chrome with the --headless command line flag is not identical to the user agent string when running it regularly. At least Chrome 105 seems to have "HeadlessChrome" instead of "Chrome".

Jukka Matilainen
  • 9,608
  • 1
  • 25
  • 19
  • With `man google-chrome` and `google-chrome --help` I couldn't find the `--headless` and `--repl` parameters, yet they work. What is the manual where they are reported? – Mario Palumbo Sep 06 '22 at 08:56
  • You can answer to my comment please? – Mario Palumbo Sep 07 '22 at 14:03
  • 1
    The documentation about the headless mode that I know of is in [this post on Chrome Developers blog](https://developer.chrome.com/blog/headless-chrome/) and in [this README file](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/headless/README.md) file in the chromium source repository. – Jukka Matilainen Sep 07 '22 at 16:55
  • This no longer works in Chrome 113. Is there an alternative? – Mario Palumbo Jun 07 '23 at 15:20
3

Here is an alternative approach which is not specific to Chrome and will work with many other browsers and http clients too. It involves setting up a HTTP server with netcat and capturing the User-Agent header value:

echo -e 'HTTP/1.1 200 OK\r\n\r\n' | nc -q 0 -l localhost 8080 | sed -n '/^User-Agent: /Is/.*: //p' & google-chrome --headless http://localhost:8080 2>/dev/null

You could replace the google-chrome invocation at the end with another browser or http client.

Jukka Matilainen
  • 9,608
  • 1
  • 25
  • 19
1

Another one based on Jukka Matilainen's answer:

USER_AGENT=$(
    while read request; do
        [[ $request =~ ^User-Agent:\ (.*)$ ]] && { echo "${BASH_REMATCH[1]}"; break; }
    done < <(nc -q0 -l -p 8080) &
    curl http://localhost:8080  2> /dev/null # change curl to whatever cli browser
)
echo "$USER_AGENT"
Ivan
  • 6,188
  • 1
  • 16
  • 23