I am working to make a status page of sorts. Essentially, it will be a basic HTML page with a little CSS that displays a list of servers and a green bubble for online, red for offline. I have the page looking the way I want and my script to ping all the servers works. I achieved the bubbles by using a very simple span tag, then setting background-color to limegreen.
The Python script iterates through a list of servers and pings each one four times. Afterwards, it checks the result and prints a message to show whether or not the server is online.
The problem I have is that I can't get the two to work together. How can I use Python and BeautifulSoup to go into my HTML file and modify the style of an element based on the result of a ping?
Example: Server 1 is online and the circle is green. Server 2 goes down, the script notices a bad ping. Script then navigates to HTML and changes the span background-color for server 2 to red.
Here is the code for the python script:
# import dependencies
import os
import pandas
from bs4 import BeautifulSoup
import re
soup = BeautifulSoup(open('index.html'), 'html.parser')
# import servernames from csv, convert to list
colnames = ['SERVERNAME']
data = pandas.read_csv('Server_List.csv', names=colnames)
hostnames = data.SERVERNAME[1:].tolist()
# ping each server
for hostname in hostnames:
# pings each server four times
response = os.system("ping -n 4 " + hostname)
head, sep, tail = hostname.partition('.')
# check the response
if response == 0:
print('\n', flush=True)
print(head.upper(), 'is up!\n', flush=True)
else:
print('\n', flush=True)
print(head.upper(), 'is down!\n', flush=True)
Here is one of my spans:
<span class="status" style="background-color: limegreen;">
And here is my class:
.status {
padding: 2px 11px;
border-radius: 100%;
}
ifandelse, as you're already doing). Instead of printing the status, you could also append the correct html (depending on your condition) to a string, then putting that into a file$$replace_this$$. Then your script reads theindex.htmlfile into a variabletemplateat the beginning. You also define a string at the beginning to which you append your html that will be put at the position of$$replace_this$$. This html is appended for each server, aka infor hostname in hostnames:after differenciating the result, then at the end you call replace on yourtemplatestring and store its result. That is the html you want.