Last active
April 12, 2025 00:26
-
-
Save scentoni/9be03d94581b97c11b7fd7a2bee381a1 to your computer and use it in GitHub Desktop.
On AWS EC2 instance, use data from [Instance Metadata Service version 2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html) in bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python3 -c "from datetime import datetime; pt=lambda s: datetime.strptime('2001,1'+s, '%Y,%j+%H:%M:%S'); t0=pt('0+20:28:33'); t1=pt('1+01:09:07'); print(t0); print(t1); dt=t1-t0; print(dt)" | |
deltatime() { python3 -c "from datetime import datetime; pt=lambda s: datetime.strptime('2001,1'+s, '%Y,%j+%H:%M:%S'); t0=pt('$1'); t1=pt('$2'); print(t0); print(t1); dt=t1-t0; print(dt)" } | |
$ deltatime '0+20:28:33' '1+01:09:07' | |
2001-01-10 20:28:33 | |
2001-01-11 01:09:07 | |
4:40:34 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
IMDS2TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") | |
metadata() { curl -s -H "X-aws-ec2-metadata-token: $IMDS2TOKEN" "http://169.254.169.254/latest/meta-data/$1"; } | |
metahtml() { | |
cat <<EOF | |
<!DOCTYPE html> | |
<html> | |
<center> | |
<body bgcolor="black" text="#39ff14" style="font-family: Arial"> | |
<h1>Metadata Demo</h1> | |
<h3>Availability Zone: $(metadata placement/availability-zone)</h3> | |
<h3>Instance Id: $(metadata instance-id)</h3> | |
<h3>Public IP: $(metadata public-ipv4)</h3> | |
<h3>Local IP: $(metadata local-ipv4)</h3> | |
</body> | |
</center> | |
</html> | |
EOF | |
} | |
metahtml | |
metahtml >/var/www/html/index.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# AWS records Tags data using a structure that's a bit harder to work with. | |
# Adapted from https://stackoverflow.com/a/70433794/2327973 | |
aws ec2 describe-instances >instances.json | |
jq <instances.json -r '.Reservations[].Instances[] | ((.Tags // empty) | from_entries) as $tags | [($tags.Name), ($tags.UserId), .ImageId, .InstanceId, .PublicIpAddress] | @csv' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Calculate the difference between two dates | |
# Usage: subdate FIRST_DATE SECOND_DATE | |
# Examples: | |
# subdate 2024-10-06 today | |
# subdate "Apr 06 04:00:00" "Apr 06 22:42:06" | |
case $(uname -s) in | |
Linux) | |
date=date | |
;; | |
Darwin) | |
date=gdate | |
;; | |
CYGWIN*|MINGW32*|MSYS*) | |
date=gnudate | |
;; | |
*) | |
echo "Unknown OS" | |
exit 1 | |
;; | |
esac | |
d1=$($date +%s -d "$1") | |
d2=$($date +%s -d "$2") | |
diff=$(( d2 - d1 )) | |
printf '%02ds=%02dd%02dh%02dm%02ds\n' $diff $((diff/86400)) $((diff%86400/3600)) $((diff%3600/60)) $((diff%60)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment