Skip to content

Instantly share code, notes, and snippets.

@bmcbm
Created November 22, 2022 11:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bmcbm/5402b223694b850f4e9167a0a9a7e084 to your computer and use it in GitHub Desktop.
Aliases to defang and "refang" IP-addresses and domains in Linux bash
#!/bin/bash
#
# Aliases to defang IP-addresses/domains and remove the defanging again - "refang" for lack of a better word.
#
# Add the aliases to ~/.bash_aliases
# Then source the file: source ~/.bash_aliases
#
# Takes arguments from command line or STDIN (one per line)
#
# Examples:
#
# $ defang 1.2.3.4 2.2.3.3 www.google.com
# 1.2.3[.]4
# 2.2.3[.]3
# www.google[.]com
#
# $ defang 1.2.3.4 2.2.3.3 | refang
# 1.2.3.4
# 2.2.3.3
# www.google.com
alias defang="perl -e 'foreach(@ARGV) {s/\.(\w+)$/\[.\]\$1/; print \"\$_\n\"} unless (-t) { while(<STDIN>){s/\.(\w+)$/\[.\]\$1/; print}}'"
alias refang="perl -e 'foreach(@ARGV) {s/[\[\]]//g; print \"\$_\n\"} unless (-t) { while(<STDIN>){s/[\[\]]//g; print}}'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment