Back to Geneos FAQ

Internal documentation only

This page has been marked as draft.

Gateway Commands - Example script for test connection (Telnet script)

This will let you enter a hostname and port, then checks if there’s a service running and listening on that address.

Example script uses expect, an extension the to TCL language and spawn function:

#!/usr/bin/expect -f

## Get arguments from command line
set host [lindex $argv 0]
set port [lindex $argv 1]

## Set timeout for the connection attempt
set timeout 5

## Start Telnet session
spawn telnet $host $port

## Look for connection result
expect {
    "Connected to" {
        puts "Connection successful to $host:$port"
        exit 0
    }
    "Connection refused" {
        puts "Connection refused to $host:$port"
        exit 1
    }
    timeout {
        puts "Connection timed out to $host:$port"
        exit 1
    }
    eof {
        puts "Connection failed to $host:$port"
        exit 1
    }
}

Lastly, it can be incorporated as a right click command via the Active Console as below:

Geneos - Gateway Commands - Example script for test connection (Telnet script) Geneos - Gateway Commands - Example script for test connection (Telnet script)

XML Copied

<command name="Test Connection">
    <targets>
        <target>/geneos/gateway/directory/probe/managedEntity</target>
    </targets>
    <userCommand>
        <type>script</type>
        <runLocation>gateway</runLocation>
        <args>
            <arg>
                <static>/home/MNL/jdavid/mantel2.sh</static>
            </arg>
            <arg>
                <userInput>
                    <description>Hostname:</description>
                    <singleLineString></singleLineString>
                    <requiredArgument>true</requiredArgument>
                </userInput>
            </arg>
            <arg>
                <userInput>
                    <description>Port:</description>
                    <singleLineString></singleLineString>
                </userInput>
            </arg>
        </args>
        <omitBlankArguments>true</omitBlankArguments>
    </userCommand>
</command>

Output:

Geneos - Gateway Commands - Example script for test connection (Telnet script)

If you prefer using standard Unix functions, the script below provides the same functionality with less complexity:

#!/bin/bash

hostname=$1
port=$2

## Try to connect using telnet with a short timeout
timeout 5 bash -c ">/dev/tcp/$hostname/$port" 2>/dev/null

if [[ $? -eq 0 ]]; then
    echo "Connection successful to $hostname:$port"
else
    echo "Connection failed to $hostname:$port"
fi

Output:

Geneos - Gateway Commands - Example script for test connection (Telnet script)

["Geneos"] ["Geneos > Gateway"] ["FAQ"]

Was this topic helpful?