I try to make a simple script that pings a network connection:
#!/usr/bin/env bash
# Xdebug ip detector for docker
# Copyright (C) 2023 Dimitrios Desyllas
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
SCRIPT=$(cat << 'EOM'
<?php
define('ERROR',"\033[0;31m");
define('OK',"\033[0;32m");
define('HIGILIGHT',"\033[0;96m");
define('HIGILIGHT2',"\033[1;33;4;44m");
define('HIGILIGHT_ERROR',"\033[0;95m");
define('NC',"\033[0m");
if(count($argv) < 2){
echo HIGILIGHT_ERROR." No IP and port provided".NC.PHP_EOL;
exit(-1);
}
$host = $argv[1];
$host_parts = explode(":",$host);
$host = $host_parts[0];
if(isset($host_parts[1])){
$port=$host_parts[1];
} elseif(isset($argv[2])){
$port = $argv[2];
} else {
echo HIGILIGHT_ERROR." No port provided".NC.PHP_EOL;
exit(-1);
}
$port = intval($port);
$timeout = 30;
echo PHP_EOL."##############".PHP_EOL.PHP_EOL."TESTING ".HIGILIGHT2.$host.':'.$port.NC.PHP_EOL;
//check if connection established via is_resource
$sk = @fsockopen($host, $port, $errnum, $errstr, $timeout);
if (!is_resource($sk)) {
echo ERROR."connection fail: ".HIGILIGHT_ERROR. $errnum . " " . $errstr.NC.PHP_EOL;
exit(1);
} else {
echo OK."Connected".NC.PHP_EOL;
exit(0);
}
EOM
)
echo "$SCRIPT" | php -d xdebug.mode=off $@
exit $?
But once I execute it:
bash check_connect 172.162.5.3:3306
I get:
Could not open input file: 172.162.5.3:3306
How I could also use pipe and command line arguments in my script?
#!/usr/bin/env phpshebang line instead of using bash, and then don't write any bash code but just plain PHP#!/usr/bin/env -S php -d xdebug.mode=offwould do exactly that. I'm not sure what this has to do with netcat?