by xfguunbb · 3y ago · 0 comments
Meanwhile here is one. Insert stdin or a src file at the top of a dst file.
test ! -h $1||exec echo $0: error: symlink
case $# in :)
;;1) x=$(sed -n '$=' $1)
test $x -gt 1||exec echo usage: $0 dst src
sed -i -e1r/dev/stdin -e1N $1
;;2) printf '0r '$2'\nwq\n'|ed -s $1
esac
For example, 1.sh 1.c < 1.h
echo 93.184.216.34 example.com|1.sh /etc/hosts
or 1.sh 1.c 1.h
1.sh /etc/hosts map-ip-host.txt
How is this done in Python. test ! -h $1||exec echo $0: error: symlink
case $# in :)
;;1) x=$(sed -n '$=' $1)
test $x -gt 1||exec echo usage: $0 dst src
od -tx1 -An|sed 's/^/w /'|ired -n $1 /dev/stdin
;;2) printf '0r '$2'\nwq\n'|ed -s $1
esacTask 1: Transform (a) the BIND format of stub resolver output, i.e., the format used by "dig" and many other stub resolvers, to (b) some other format, like HOSTS file, BIND/tinydns zone file, haproxy map file, etc. The input would typically be catenated stub resolver output for hundreds to thousands of domains. The epoch program is three lines of C.
NB. For large input on Linux, dash is significantly faster than bash. I wrote a C program that does the job of this script, faster than dash, and much faster than python. However I still prefer the shell for testing ideas, quickly.
EPOCH=$(epoch);
tr -d '\12'|tr ';' '\12' \
|sed -n '/ANSWER SECTION/{s/ ANSWER SECTION://;
s/\.[^-0-9a-zA-Z].*IN[^-0-9a-zA-Z]*A[^-0-9a-zA-Z]/ 1 IN A /;
# list domains to exclude
/www.google.com/d;
/^$/d;/^.$/d;p;}' \
|{
exec 2>/dev/null 3>&3 4>&4 5>&5 6>&6 7>&7 8>&8 9>&9;
while read NAME TTL CLASS TYPE IPADDR ;do
echo $NAME $TTL $CLASS $TYPE $IPADDR;
echo $NAME 1 $CLASS $TYPE $IPADDR >&3;
echo $NAME $IPADDR >&4;
echo $NAME $IPADDR \# $EPOCH >&5;
echo =$NAME:$IPADDR:1 >&6;
#_IP_()
# {
# echo $IPADDR|grepcidr -f $1-ips.txt >/dev/null;
# }
#if _IP_ cloudflare ...
#if _IP_ aws ...
#if _IP_ cloudfront ...
#if _IP_ fastly ...
#if _IP_ akamai ...
# etc.
done;
}
For example, drill example.com|1.sh 3>>1.zone
kdig example.com|1.sh 4>>/etc/hosts
drill example.com|1.sh 5>>1.map
kdig example.com|1.sh 6>>data
Task 2: Extract and transform CloudFront domains and their CNAMEs from BIND format stub resolver output to haproxy map format. grep -A1 ANSWER.SEC|tr '\11' '\40'|sed -n '/cloudfront.net\.$/{s/\. .* / /;s/\.$//p;}'
For example, drill blogs.aws.amazon.com|2.sh >>2.map