Last active
December 25, 2018 04:14
-
-
Save KEINOS/c960b5344bfbb9c14553c06c26201f20 to your computer and use it in GitHub Desktop.
macOS の PHP で空きポートを取得する(ランダムな未使用ポート検索) ref: https://qiita.com/KEINOS/items/be7b769f64d5f154388f
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
lsof -i -P | grep -i 'tcp' | sed 's/\[.*\]/IP/' \ | |
| sed 's/:/ /' | sed 's/->/ /'| awk -F' ' '{print $10}' \ | |
| awk '!a[$0]++' |
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
lsof -i -P | grep -i 'listen' 2>&1 |
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
#!/usr/bin/env php | |
<?php | |
$browser_default = 'safari'; | |
$browser = isset($argv[1]) ? strtolower($argv[1]) : $browser_default; | |
switch($browser){ | |
case 'chrome': | |
$browser = 'Google Chrome'; | |
break; | |
case 'firefox': | |
$browser = 'Firefox'; | |
break; | |
case 'opera': | |
$browser = 'Opera'; | |
break; | |
default: | |
$browser = 'Safari'; | |
break; | |
} | |
$port = getPortRandom(); | |
$cmd = "open -a \"{$browser}\" \"http://localhost:{$port}\" "; | |
$cmd .= "&& php -S localhost:{$port}"; | |
`$cmd`; | |
/* [Functions] -------------------------------------------------------------- */ | |
function getPortRandom($port_min = 50000, $port_max = 65535) | |
{ | |
$cmd = 'lsof -i -P | grep -i "listen" 2>&1'; | |
$rows = array_filter(explode(PHP_EOL, `$cmd`)); | |
$ports = array_map( | |
function ($row) { | |
$row = str_replace(':', ' ', $row); | |
$cols = explode(' ', $row); | |
$cols = array_values(array_filter($cols)); | |
return (int) $cols[9]; | |
}, | |
$rows | |
); | |
while (true) { | |
$port = rand($port_min, $port_max); | |
if (false === array_search($port, $ports)) { | |
break; | |
} | |
} | |
return $port; | |
} |
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
$ cd /path/to/your/dev/dir | |
$ ls | |
index.php | |
$ dev opera |
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
$ cd /path/to/your/dev/dir | |
$ ls | |
index.php | |
$ dev opera |
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
$ cd /path/to/your/dev/dir | |
$ ls | |
index.php | |
$ dev opera |
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
function getPortRandom($port_min = 50000, $port_max = 65535) | |
{ | |
$cmd = "lsof -i -P | grep -i 'tcp' | sed 's/\[.*\]/IP/' \ | |
| sed 's/:/ /' | sed 's/->/ /'| awk -F' ' '{print $10}' \ | |
| awk '!a[$0]++'"; | |
$ports = array_filter(explode(PHP_EOL, `$cmd`)); | |
while (true) { | |
$port = rand($port_min, $port_max); | |
if (false === array_search($port, $ports)) { | |
break; | |
} | |
} | |
return $port; | |
} |
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
function getPortRandom($port_min = 50000, $port_max = 65535) | |
{ | |
$cmd = "lsof -i -P | grep -i 'tcp' | sed 's/\[.*\]/IP/' \ | |
| sed 's/:/ /' | sed 's/->/ /'| awk -F' ' '{print $10}' \ | |
| awk '!a[$0]++'"; | |
$ports = array_filter(explode(PHP_EOL, `$cmd`)); | |
while (true) { | |
$port = rand($port_min, $port_max); | |
if (false === array_search($port, $ports)) { | |
break; | |
} | |
} | |
return $port; | |
} |
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
function getPortRandom() | |
{ | |
$socket = socket_create_listen(0); //ランダムな空きポートの割り当て | |
socket_getsockname($socket, $addr, $port); //ポート情報の取得 | |
socket_close($socket); //ソケットを解放 | |
return (int) $port; //解放されたソケットのポート番号を横取り | |
} |
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 -a | |
Interactive shell | |
php > $sock = socket_create_listen(0); | |
php > socket_getsockname($sock, $addr, $port); | |
php > socket_close($sock); | |
php > echo $port; | |
58487 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment