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:


XML:
<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:
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"
fiOutput:
If you need further help:
- Please contact our support team via the chat service box on any of our websites or raise a support request.
- Make sure you provide us with:
- Background of the issue or request.
- Use cases, requirements, business impact, etc.
- Encountered error messages.
- Log files or diagnostic files.
- Screenshots.
- And other important information relevant to your inquiry.
Comments
0 comments
Please sign in to leave a comment.