Created
July 16, 2014 15:32
-
-
Save mcuyar/a8e7dcaab1e1a4630e27 to your computer and use it in GitHub Desktop.
Send messages to logged in terminal user from your laravel application
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
<?php | |
if(! function_exists('tlog')) { | |
function tlog($logged, $message = '', $user = 'vagrant') | |
{ | |
// Only available in test environments | |
if (app()->environment() === 'production') { | |
return; | |
} | |
$log = 'echo "\n'; | |
if($message !== '') { | |
$log .= 'Message: ' . (string) $message . '\n\n'; | |
} | |
$log .= 'Start------------------------------ \n\n'; | |
if(is_string($logged) || is_int($logged)) { | |
$log .= (string) $logged; | |
} | |
else if(is_array($logged)) { | |
$log .= print_r($logged, true); | |
} | |
else if(is_object($logged)) { | |
if(method_exists($logged, 'toArray')) { | |
$log .= print_r($logged->toArray(), true); | |
} | |
else { | |
try { | |
// Use var export to catch bad objects | |
$log .= var_export($logged, true); | |
} | |
catch(Exception $e) { | |
$log .= $e->getMessage(); | |
} | |
} | |
} | |
$log .= '\n\nEnd------------------------------ \n'; | |
$log .= '" | write '. tlog_find_user($user); | |
exec($log); | |
} | |
} | |
if(! function_exists('tlog_find_user')) { | |
function tlog_find_user($user) | |
{ | |
$exec = shell_exec('who -us'); | |
$array = explode(' ', $exec); | |
$array = array_filter($array); | |
$array = array_chunk ($array , 6); | |
$users = []; | |
foreach($array as $item) { | |
if(count($item) >= 2 && !isset($users[$item[0]])) { | |
$users[$item[0]] = $item[0].' '.$item[1]; | |
} | |
} | |
if(isset($users[$user])) { | |
return $users[$user]; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can send colors through if you send the output via
> /dev/ttysXXX
.Example
echo "\e[31mAlert\e[0m" > /dev/ttys000
sends a message of "Alert" colored in red text.