AWS dynamic IP ranges

Need to know the dynamic IP ranges used by AWS in a specific region? AWS publishes this as a JSON file.

curl -s https://ip-ranges.amazonaws.com/ip-ranges.json | jq '.prefixes | .[] | select((.region == "eu-west-1") and (.service == "EC2")) | .ip_prefix' | sed 's/"//g'

This filters for EC2 in eu-west-1 (Ireland) and outputs CIDR blocks.

To check another service:

curl -s https://ip-ranges.amazonaws.com/ip-ranges.json | jq '.prefixes | .[] | select(.service == "CLOUDFRONT") | .ip_prefix' | sed 's/"//g'

Useful for firewall rules.

Enjoy!

Find sdx device/ATAx mappings

When dealing with hardware, you often see log messages like:

ata9: hard resetting link
ata9: SATA link up 1.5 Gbps (SStatus 113 SControl 310)

To map an ata number to the actual sdX device:

ls -l /sys/class/ata_device/ata9/device/host*/target*/*/block

This shows which sdX corresponds to ata9. Check dmesg for the initial discovery mapping too.

Enjoy!

Convert Squid/timestamps to a human readable format

Somebody had the brilliant idea of using Unix timestamps in Squid log files. It makes parsing easy but reading them is not so human-friendly.

This is an elegant way of solving the problem:

cat /var/log/squid/access.log | perl -p -e 's/^([0-9]*)/"[" . localtime($1) . "]"/e'

The Perl one-liner captures the timestamp at the beginning of each line, converts it to a human-readable date with localtime(), and replaces it inline. The /e flag tells Perl to evaluate the replacement as code rather than a string.

Hope it helps!

Remove all non-ascii characters with perl

Sometimes when creating one-liners, the source contains non-ASCII characters that make parsing harder. To strip them all out, use Perl:

cat dirty-source.txt | perl -pe 's/[^[:ascii:]]//g' > clean-output.txt

This replaces every character that is not in the ASCII set with nothing, leaving only clean ASCII text. The same approach works for Windows line endings or other invisible characters by adjusting the character class.

If you prefer a sed alternative for the same task:

sed 's/[^[:print:]]//g' dirty-source.txt > clean-output.txt

Hope it helps!

How to update all docker base images with one command

To pull the latest version of all your Docker images at once:

for docker in $(docker images | awk '{print $1,$2}' | sed 's/ /:/g' | grep -v REPOSITORY); do docker pull $docker; done

This lists all images, extracts repository and tag, formats as repository:tag, and pulls each one.

After updating, old image versions stay on disk. To clean them up:

docker image prune -a

This removes all unused images and frees up space.

Enjoy!

How to update all pip python packages with one command

To update all your pip-installed Python packages at once:

sudo pip install -U `pip list -o | cut -d ' ' -f1 | tr '\n' ' '`

This lists outdated packages with pip list -o, extracts their names, joins them with spaces and passes them to pip install -U. A one-liner that saves you from updating each package individually.

NB: One-liners like this are potentially destructive. Read what they do before running them, especially with sudo.

For a safer alternative on modern systems, use pip-review:

pip install pip-review
pip-review --auto

This prompts you before each update.

Enjoy!