Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- im_online.py (0.1.1)
- $ pyhon3 um_online.py to@email.com
- Send email with machine status to to@email.com
- example:
- {"User": "use_name", "Hostname": "host_name",
- "IP": "192.168.1.5 / 61:5D:33:A7:FC:6D", "OS": "Mac OSX 10.13.2 x86_64",
- "Kernel": "17.3.0", "Uptime": "0 days 3 hrs 30 mins", "Shell": "/bin/bash",
- "Processes": "343 running", "Packages": 11, "CPU": "Intel Core i7-3520M @ 2.90GHz",
- "CPU Usage": "[21.0, 6.0, 21.8, 7.0]", "RAM": "8 GB/ 8 GB", "Disk": "256 / 489 GB"}
- """
- import sys
- import socket
- import subprocess
- import time
- import pip
- USER = 'from@gmail.com'
- PW = 'pass'
- def internet_is_on():
- """
- Host: 8.8.8.8 (google-public-dns-a.google.com)
- OpenPort: 53/tcp
- Service: domain (DNS/TCP)
- """
- try:
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.connect(("8.8.8.8", 53))
- return True
- except:
- return False
- finally:
- s.close()
- def get_host_name():
- try:
- host_name = socket.gethostname()
- except:
- host_name = None
- finally:
- return host_name
- def get_ip_addr():
- try:
- s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
- s.connect(("8.8.8.8", 53))
- ip = s.getsockname()[0]
- except:
- ip = None
- finally:
- s.close()
- return ip
- def import_or_install(package):
- try:
- __import__(package)
- return True
- except ImportError:
- pip.main(['install', package])
- return False
- def get_machine_info():
- while not import_or_install('pyarchey'):
- None
- command = "pyarchey -zj"
- p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
- (output, err) = p.communicate()
- # This makes the wait possible
- p_status = p.wait()
- return output
- def send_email(user, pwd, recipient, subject, body):
- import smtplib
- FROM = user
- TO = recipient if type(recipient) is list else [recipient]
- SUBJECT = subject
- TEXT = body
- # Prepare actual message
- message = """From: %s\nTo: %s\nSubject: %s\n\n%s
- """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
- try:
- server = smtplib.SMTP("smtp.gmail.com", 587)
- server.ehlo()
- server.starttls()
- server.login(user, pwd)
- server.sendmail(FROM, TO, message)
- status = True # "successfully sent the mail"
- except:
- status = False # "failed to send mail"
- finally:
- server.close()
- return status
- if __name__ == "__main__":
- if len(sys.argv) < 2:
- print("ERROR: arguments number. Syntax: python3 im_online.py to@email.com")
- exit(0)
- while not internet_is_on():
- time.sleep(5)
- info = get_machine_info().decode("utf-8")
- status = send_email(USER, PW, str(sys.argv[1]), "I'M ONLINE", str(info))
- if status:
- exit(0)
- else:
- exit(1)
Add Comment
Please, Sign In to add comment