Created
October 18, 2020 17:37
-
-
Save Tbruno25/df95610152892441e898748afb903dd2 to your computer and use it in GitHub Desktop.
Visualize cpu temperature from a remote linux computer.
This file contains 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
import streamlit as st | |
import paramiko | |
import time | |
from streamlit.report_thread import add_report_ctx | |
from getpass import getpass | |
ip = input("Please enter IP address: ") | |
user = input("Username: ") | |
pw = getpass() | |
cpu_script = "date +%H:%M:%S ; sensors | grep 'id 0' | awk '{print$4}'" | |
ssh = paramiko.SSHClient() | |
add_report_ctx(ssh) | |
try: | |
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) | |
ssh.connect(hostname=ip, username=user, password=pw) | |
print(f"Successfully connected to {ip}") | |
except Exception: | |
print("Connection Failed.") | |
def command(script, wait=True, sudo=False): | |
stdin, stdout, stderr = ssh.exec_command(script, get_pty=wait) | |
if sudo: | |
stdin.write(f"{pw}\n") | |
stdin.flush() | |
return stdout | |
def stdout_to_str(stdout) -> list: | |
"""Converts from binary to string""" | |
return stdout.read().decode().split() | |
def timestamp(stdout): | |
return stdout[0] | |
def temperature(stdout): | |
return stdout[-1][1:-4] | |
if __name__ == "__main__": | |
st.title("CPU Live Feed") | |
delay_time = st.number_input("Delay", value=1, min_value=1) | |
stop = st.checkbox( | |
"Stop", | |
) | |
chart = st.line_chart(data=None) | |
while not stop: | |
stdout = stdout_to_str(command(cpu_script)) | |
new_row = dict(CPU={timestamp(stdout): temperature(stdout)}) | |
chart.add_rows(new_row) | |
time.sleep(delay_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment