Guest User

Untitled

a guest
Apr 5th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. """
  2. im_online.py (0.1.1)
  3.  
  4. $ pyhon3 um_online.py to@email.com
  5.  
  6. Send email with machine status to to@email.com
  7.  
  8. example:
  9. {"User": "use_name", "Hostname": "host_name",
  10. "IP": "192.168.1.5 / 61:5D:33:A7:FC:6D", "OS": "Mac OSX 10.13.2 x86_64",
  11. "Kernel": "17.3.0", "Uptime": "0 days 3 hrs 30 mins", "Shell": "/bin/bash",
  12. "Processes": "343 running", "Packages": 11, "CPU": "Intel Core i7-3520M @ 2.90GHz",
  13. "CPU Usage": "[21.0, 6.0, 21.8, 7.0]", "RAM": "8 GB/ 8 GB", "Disk": "256 / 489 GB"}
  14.  
  15. """
  16.  
  17. import sys
  18. import socket
  19. import subprocess
  20. import time
  21. import pip
  22.  
  23. USER = 'from@gmail.com'
  24. PW = 'pass'
  25.  
  26.  
  27. def internet_is_on():
  28. """
  29. Host: 8.8.8.8 (google-public-dns-a.google.com)
  30. OpenPort: 53/tcp
  31. Service: domain (DNS/TCP)
  32. """
  33. try:
  34. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  35. s.connect(("8.8.8.8", 53))
  36. return True
  37. except:
  38. return False
  39. finally:
  40. s.close()
  41.  
  42.  
  43. def get_host_name():
  44. try:
  45. host_name = socket.gethostname()
  46. except:
  47. host_name = None
  48. finally:
  49. return host_name
  50.  
  51.  
  52. def get_ip_addr():
  53. try:
  54. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  55. s.connect(("8.8.8.8", 53))
  56. ip = s.getsockname()[0]
  57. except:
  58. ip = None
  59. finally:
  60. s.close()
  61. return ip
  62.  
  63.  
  64. def import_or_install(package):
  65. try:
  66. __import__(package)
  67. return True
  68. except ImportError:
  69. pip.main(['install', package])
  70. return False
  71.  
  72.  
  73. def get_machine_info():
  74. while not import_or_install('pyarchey'):
  75. None
  76.  
  77. command = "pyarchey -zj"
  78. p = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True)
  79. (output, err) = p.communicate()
  80. # This makes the wait possible
  81. p_status = p.wait()
  82. return output
  83.  
  84.  
  85. def send_email(user, pwd, recipient, subject, body):
  86. import smtplib
  87.  
  88. FROM = user
  89. TO = recipient if type(recipient) is list else [recipient]
  90. SUBJECT = subject
  91. TEXT = body
  92.  
  93. # Prepare actual message
  94. message = """From: %s\nTo: %s\nSubject: %s\n\n%s
  95. """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
  96. try:
  97. server = smtplib.SMTP("smtp.gmail.com", 587)
  98. server.ehlo()
  99. server.starttls()
  100. server.login(user, pwd)
  101. server.sendmail(FROM, TO, message)
  102. status = True # "successfully sent the mail"
  103. except:
  104. status = False # "failed to send mail"
  105. finally:
  106. server.close()
  107. return status
  108.  
  109.  
  110. if __name__ == "__main__":
  111. if len(sys.argv) < 2:
  112. print("ERROR: arguments number. Syntax: python3 im_online.py to@email.com")
  113. exit(0)
  114.  
  115. while not internet_is_on():
  116. time.sleep(5)
  117.  
  118. info = get_machine_info().decode("utf-8")
  119.  
  120. status = send_email(USER, PW, str(sys.argv[1]), "I'M ONLINE", str(info))
  121. if status:
  122. exit(0)
  123. else:
  124. exit(1)
Add Comment
Please, Sign In to add comment