Skip to content

Instantly share code, notes, and snippets.

@TalalMash
Created August 30, 2023 23:10
Show Gist options
  • Save TalalMash/c0e02f4d3639236f09cb91e6a571cd78 to your computer and use it in GitHub Desktop.
Save TalalMash/c0e02f4d3639236f09cb91e6a571cd78 to your computer and use it in GitHub Desktop.
HTML generate last seen network ping for cron
#!/bin/bash
ip_address="1.1.1.1"
output_file="index.html"
generate_html() {
local utc_time="$1"
cat << EOF > "$output_file"
<!DOCTYPE html>
<html>
<head>
<script>
function convertUTCToLocal(utcTime) {
var localTime = new Date(utcTime);
if (isNaN(localTime)) {
return "Invalid Date";
}
return localTime.toLocaleString();
}
</script>
</head>
<body>
<style>
h1 {font-size: 500%;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;}
</style>
<h1>Last seen<span id="localTime"></span></h1>
<script>
var utcTime = "$utc_time";
document.getElementById("localTime").innerText = convertUTCToLocal(utcTime);
</script>
</body>
</html>
EOF
}
ping_result=$(ping -c 1 "$ip_address")
if [ $? -eq 0 ]; then
timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
generate_html "$timestamp"
exit 0
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment