pmset -g assertions will point out that IS is guilty:/System/Library/LaunchDaemons/com.apple.InternetSharing.plist sudo launchctl load -F /System/Library/LaunchDaemons/com.apple.InternetSharing.plist Disabled <true/> flag Disabled <true/> ; sudo launchctl unload /System/Library/LaunchDaemons/com.apple.InternetSharing.plist . shell_exec('launchctl load -F /System/Library/LaunchDaemons/com.apple.InternetSharing.plist');  shell_exec('launchctl unload /System/Library/LaunchDaemons/com.apple.InternetSharing.plist'); com.username.InternetSharing.plist file in the /Library/LaunchDaemons/ folder. sudo launchctl load -F /Library/LaunchDaemons/com.username.InternetSharing.plist Scenario program. Let's write for it on AppleScripts two creaks that will call the client program (full code below) with the parameter start or stop.do shell script "/usr/bin/php /Users/username/CheckMac/client.php start"do shell script "/usr/bin/php /Users/username/CheckMac/client.php stop""~/Library/Scenario/Wake Scripts" and "~/Library/Scenario/Sleep Scripts" .Wake Scripts " folder, which launches the Client program with the start parameter. The client program is associated with our service, which can start the IS service, as it has sufficient rights.Sleep Scripts " folder and with the stop parameter. <?php // PHP 5.3.10 //   error_reporting(E_ALL ^ E_WARNING); set_time_limit(0); ob_implicit_flush(); $port = 10001; $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_bind($sock, "127.0.0.1", $port) or die('Port listened'."\n"); socket_set_option($sock, SOL_SOCKET, SO_KEEPALIVE, 1); socket_set_nonblock($sock); socket_listen($sock, 1000); $clients = array($sock); $tarr = array(); $iparr = array(); $current = 0; while (true) { $read = $clients; if (count($tarr) > 0) foreach ($tarr as $ind => $tim) { //      10  -  if ((time() - $tim) > 10) { socket_close($read[$ind + 1]); unset($clients[$ind+1]); unset($iparr[$ind]); unset($tarr[$ind]); echo "Disconnect client.\n"; continue; } } //    -  if (socket_select($read, $write = NULL, $except = NULL, 1) < 1) continue; //   if (in_array($sock, $read)) { $current ++; $clients[$current] = $newsock = socket_accept($sock); socket_write($newsock, "<OK>\n"); socket_getpeername($newsock, $ip); echo "New connection from ip: {$ip}\n"; $key = array_search($sock, $read); $iparr[$current-1] = $ip; $tarr[$current-1] = time(); unset($read[$key]); $read[$current] = $newsock; continue; } //   foreach ($read as $index => $read_sock) { $data = socket_read($read_sock, 1024); if ($data === false) { $key = array_search($read_sock, $clients); unset($clients[$key]); unset($iparr[$key - 1]); echo "Disconnect client.\n"; unset($tarr[$key - 1]); continue; } $data = trim($data); //    if (!empty($data)) { echo $iparr[$index - 1] . "[$index] - $data\n"; $tarr[$index - 1] = time(); switch ($data) { case "quit": socket_close($read_sock); $key = array_search($read_sock, $clients); unset($clients[$key]); unset($iparr[$key - 1]); echo "Disconnect client.\n"; unset($tarr[$key - 1]); break; case "ping": socket_write($read_sock, "<PONG> " . time() . "\n"); break; //     IS case 'start': $s = shell_exec('launchctl list'); if(strpos($s, 'com.apple.InternetSharing') === false){ shell_exec('launchctl load -F /System/Library/LaunchDaemons/com.apple.InternetSharing.plist'); } break; //     IS case 'stop': $s = shell_exec('launchctl list'); if(strpos($s, 'com.apple.InternetSharing') !== false){ shell_exec('launchctl unload /System/Library/LaunchDaemons/com.apple.InternetSharing.plist'); } break; } if ($data === "close") { socket_close($sock); break(2); } } } } socket_close($sock);  <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.username.InternetSharing</string> <key>ProgramArguments</key> <array> <string>/usr/bin/php</string> <string>/Users/username/CheckMac/listen.php</string> </array> <key>RunAtLoad</key> <true/> </dict> </plist>  <?php $fp = fsockopen("127.0.0.1", 10001, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)<br />\n"; } else { $out = @$argv[1]."\r\n"; fwrite($fp, $out); fgets($fp, 128); sleep(1); //       $out = "quit\r\n"; fwrite($fp, $out); fgets($fp, 128); fclose($fp); } do shell script "/usr/bin/php /Users/username/CheckMac/client.php start"do shell script "/usr/bin/php /Users/username/CheckMac/client.php stop"Source: https://habr.com/ru/post/150617/
All Articles