BASH – generate a pseudo-random user agent

Posted in Programming by Julian Kessel - Oct 01, 2009

The following script will give you a user agent randomly out of a big list of all user agents that exist.

1
2
3
4
5
6
7
8
9
#!/bin/bash
 
userAgents=`curl -s http://www.user-agents.org/allagents.xml | grep -iB 5 '<Type>B</Type>' | grep -i '<String>' | cut -c 9- | sed 's/..........$//'`
maxLines=`echo "$userAgents" | wc -l | tr -d ' '`
randomUserAgent=$(echo "$userAgents" | sed -n $[ ( $RANDOM % ( $[ $maxLines - 1 ] + 1 ) ) + 1 ]p)
 
echo $randomUserAgent
 
</string>

The script searches for a type “B” in the xml document, which stands for Browser, if you want to include other types, write the letter in place of, or with one space seperated from “B”. It’s possible to specify more than 2 types.

B = Browser
C = Link-, bookmark-, server- checking D = Downloading tool
P = Proxy server, web filtering
R = Robot, crawler, spider
S = Spam or bad bot

Tags: COMMENTS

3 Responses to “BASH – generate a pseudo-random user agent”

  1. Sami says:

    Could you write an extention for firefox?
    We need a “random user agent switcher” which changes every page load.

  2. http://www.user-agents.org/ seems to have changed its schema. Please replace “B’ | grep ‘” with B’ | grep ”

  3. Thanks, I updated the script and also added a “-i” to the grep evocations, so we’re prepared for future case changes ;)

Leave a Reply