check_mysql_server_version/check_mysql_server_version

59 lines
1.3 KiB
Plaintext
Raw Normal View History

2022-12-30 15:04:50 +01:00
#!/usr/bin/perl
# Copyright 2022 XiCoN-FJS- @ fjs@xicon.de
#
# Version: 0.1 (2022-12-29)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
use IO::Socket;
use IO::Socket::IP;
sub usage_die()
{
die "Usage: ".$0." <host> <port>\n";
}
my ($host, $port) = @ARGV;
if (not defined $host)
{
usage_die();
}
if (not defined $port)
{
usage_die();
}
# prepare the TCP connection
my $mysql_sock = IO::Socket::IP->new(
Proto => 'tcp',
PeerAddr => $host,
PeerPort => $port,
Timeout => 5,
) or die "Could not create socket: $!";
# waiting for an answer
my ($datagram,$flags);
$mysql_sock->recv($datagram,32,$flags);
# extract the version string
my ($line1,$line2) = split("\n",$datagram);
die("Couldn't extract version of mysql from ".$host.":".$port) if !defined $line2;
my ($mysql_version) = $line2 =~/^(\d+\.\d+\.\d+)/;
print $mysql_version."\n";