Home > Enterprise >  Get the "user agent" string from a new Google Chrome browser session from bash
Get the "user agent" string from a new Google Chrome browser session from bash

Time:09-06

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?

CodePudding user response:

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".

CodePudding user response:

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.

CodePudding user response:

Another one based on Jukka Matilainen's answer:

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