SHARE
TWEET

Linux Email on Login.

MartyniP Sep 18th, 2011 170 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. //exit();
  4. $email = "your@email.tld";
  5. $fromEmail = "from@email.tld";
  6.  
  7. $filename = "/tmp/login.".time().".temp";
  8.  
  9. exec("cat /var/log/auth.log | grep 'Accepted password for ' > ".$filename);
  10. $file = file_get_contents($filename);
  11. $last = trim(file_get_contents('/tmp/lastlog.log'));
  12. $file = explode("\n",$file);
  13.  
  14. $i = 0;
  15. $proccess = 0;
  16. foreach ($file as $k => $v) {
  17.  
  18. if ($proccess == 1) {
  19.         $v2 = explode($findme, $v);
  20.         $ex = explode(" ",$v2[1]);
  21.         $login[$i]['ip'] = $ex[2];
  22.         $login[$i]['user'] = $ex[0];
  23.         $login[$i]['line'] = $v;
  24. $i++;
  25. }
  26. if (trim($v) == $last) {
  27.         $proccess = 1;
  28. }
  29. }
  30. //var_dump($blline);
  31. $line = $login[($i-1)]['line'];
  32. if ($line) {
  33. file_put_contents("/tmp/lastlog.log", $line);
  34. }
  35.  
  36. foreach ($login as $k => $v) {
  37. /*foreach ($v['ip'] as $k2 => $v2) {
  38. $emailtext .= "Login from $v2 <br />\n";
  39. }*/
  40. $emailtext .= "Login:
  41. User: ".$login[$k]['user']."
  42. IP: ".$login[$k]['ip']."
  43. Hostname: ".gethostbyaddr($login[$k]['ip'])."
  44. Line: ".$login[$k]['line']."
  45. -----------------------------
  46.  
  47. ";
  48. }
  49.  
  50. //echo $emailtext;
  51.  
  52. if ($i != 0) {
  53. mail($email, "SHH Login - ".date('G:i:s j/m/Y'), str_replace("\n", "<br />\n", $emailtext), "From: ".$fromEmail."\r\nContent-type: text/html; charset=iso-8859-1\r\n");
  54. }
  55. unlink($filename);
  56. ?>
RAW Paste Data
Want to get better at PHP?
Learn to code PHP in 2017
Top