Forum Discussion

Sharath's avatar
Sharath
Icon for Neophyte rankNeophyte
2 months ago

Monitoring Gateway SIP Trunk Status via SSH through Command line

Hi,

I am trying to monitor the status of the Gateway SIP trunk using the command "show sip-ua register status" via SSH. However, I am encountering issues while running a custom script provide by the support team. Initially, when I ran the script a few times, I received an error: "Unable to establish the SSH Connection". After some time, the error changed to "No Signature Method."

I have verified the SSH connection from the collector server to the end device, and it works fine when I manually establish the SSH session. I also tested my own script , but it returned a similar error: "Error: Unable to establish SSH session/channel."

I have attached the error snapshot and the script I am using for reference. Please guide me on how to monitor the SIP status via the command line using SSH.

When running the command "show sip-ua register status", the output is either Yes (registered) or No (unregistered) from the device end. In LogicMonitor, the expected output should be:

  • Yes = 0
  • No = 1


I tried to attach the Script but it will not support Txt Format, Sorry for the inconvenience

Below is the script Provided by the support team

import com.santaba.agent.groovyapi.expect.Expect;

// Get the hostname and credentials from the device property table
hostname = hostProps.get("system.hostname");
userid = hostProps.get("ssh.user");
passwd = hostProps.get("ssh.pass");

// Ensure required properties are not null
if (!hostname || !userid || !passwd) {
    throw new Exception("Missing required device properties: hostname, ssh.user, or ssh.pass.");
}

// Initiate an SSH connection to the host using the provided credentials
def ssh_connection = Expect.open(hostname, userid, passwd);

// Wait for the CLI prompt to confirm connection
ssh_connection.expect("# "); // Adjust prompt based on your device

// Send the `show sip-ua register status` command
ssh_connection.send("show sip-ua register status\n");

// Wait for the response to complete
ssh_connection.expect("#"); // Replace with your device's prompt

// Print the output of the command
println "Command Output: " + ssh_connection.getLastOutput();

// Logout
ssh_connection.send("exit\n");

// Close the SSH connection handle
ssh_connection.expectClose();

return 0;

This is another script Created by Me

import com.santaba.agent.groovyapi.expect.Expect

// Device credentials and details
def deviceIP = hostProps.get("system.ip")      // IP address of the device
def username = hostProps.get("ssh.user")       // SSH username from LogicMonitor
def password = hostProps.get("ssh.pass")       // SSH password from LogicMonitor

// Command to execute
def command = "show sip-ua register status"

// Initialize result
def resultCode = 1  // Default to 1 (Unregistered)

// Start SSH session
try {
    def session = Expect.open(deviceIP, username, password)
    
    // Send the command
    session.send(command + "\n")
    
    // Wait for the output
    def output = session.expect(10, ".*")  // Adjust timeout (10 seconds) as needed
    println("Command output: ${output}")
    
    // Check the output for 'Yes' or 'No'
    if (output.toLowerCase().contains("yes")) {
        resultCode = 0  // Registered
    } else if (output.toLowerCase().contains("no")) {
        resultCode = 1  // Unregistered
    } else {
        throw new Exception("Unexpected output: ${output}")
    }
    
    // Close the session
    session.close()
} catch (Exception e) {
    println("Error: ${e.message}")
    // Optionally log additional details for debugging
}

// Print the result for LogicMonitor
println(resultCode)
return resultCode

  • What kinda device are you attempting to SSH into?

    There was an older forum post at https://community.logicmonitor.com/discussions/product-discussions/lm-expect-packages-and-ed25519/14285 that mentions issues with Expect and Fortigate. That LM's Expect might not support some of the newer SSH encryption systems.

    • Sharath's avatar
      Sharath
      Icon for Neophyte rankNeophyte

      Model - ISR4431/K9 Router
      firmware_version    17.6.1
      hardware_version    V07
      Vendor - Cisco

       

  • I have attached the snapshot for reference , please let me know any other info

    • Mike_Rodrigues's avatar
      Mike_Rodrigues
      Icon for Product Manager rankProduct Manager

      You can also do this at the collector level with the following directive in agent.conf:

      ssh.preferredlibrary=jsch

       

      • Shack's avatar
        Shack
        Icon for Advisor rankAdvisor

        Try this one for Active Discovery.  My applies to is hasCategory("CiscoVoiceDialControl")  It was created for telnet but you should be able to tweak it.  It is multi instance.

        // Import the LogicMonitor expect helper class
        import com.santaba.agent.groovyapi.expect.Expect
        
        // Get the hostname and credentials from the device property table
        def hostname = hostProps.get("system.hostname")
        def userid = hostProps.get("ssh.user")
        def passwd = hostProps.get("ssh.pass")
        def port = 23  // Default Telnet port
        def timeout = 5000  // Timeout in milliseconds
        
        // Open a Telnet connection
        def telnet_connection = Expect.open(hostname, port, timeout)
        telnet_connection.expect("Username:")
        telnet_connection.send("${userid}\n")
        telnet_connection.expect("Password:")
        telnet_connection.send("${passwd}\n")
        telnet_connection.expect("#")
        
        // Disable pagination and set terminal width
        telnet_connection.send("terminal width 0\n")
        telnet_connection.expect("#")
        telnet_connection.send("terminal pager 0\n")
        telnet_connection.expect("#")
        
        // Send command to display the SIP UA register status
        telnet_connection.send("show sip-ua register status\n")
        telnet_connection.expect("#")
        
        // Collect the command output
        def config = telnet_connection.before()
        
        // Close the Telnet connection
        telnet_connection.send("exit\n")
        telnet_connection.expectClose()
        
        // Print the raw output for debugging
        //println config
        
        // Initialize a list to hold parsed entries
        def entries = []
        
        // Use regex to match each line of data
        def matcher = config =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\w+)\s+(\w+)\s+(\S+)/
        
        while (matcher.find()) {
            def line = matcher.group(1)
            def peer = matcher.group(2)
            def expires = matcher.group(3).toInteger()
            def reg = matcher.group(4)
            def survival = matcher.group(5)
            def p_associ_uri = matcher.group(6)
        
            // Map Reg field: 0 for "yes" and 1 for "no"
            def regStatus = (reg == "yes") ? 0 : 1
        
            // Create an entry map with the mapped Reg value
            def entry = [
                'Line': line,
                'Peer': peer,
                'Expires': expires,
                'Reg': regStatus,  // Mapped value
                'Survival': survival,
                'P-Associ-URI': p_associ_uri
            ]
            
            // Add entry to entries list
            entries << entry
        }
        
        // Output the parsed entries for LogicMonitor to pick up as individual datapoints
        entries.each { entry ->
            // println "Line: ${entry['Line']}, Peer: ${entry['Peer']}, Expires: ${entry['Expires']}, Reg: ${entry['Reg']}, Survival: ${entry['Survival']}, P-Associ-URI: ${entry['P-Associ-URI']}"
            println "${entry.Line}##${entry.Line}"
        }
        
        // Return a success code
        return 0

        Below is for data collection.

        // Import the LogicMonitor expect helper class
        import com.santaba.agent.groovyapi.expect.Expect
        
        // Get the hostname and credentials from the device property table
        def hostname = hostProps.get("system.hostname")
        def userid = hostProps.get("ssh.user")
        def passwd = hostProps.get("ssh.pass")
        def port = 23  // Default Telnet port
        def timeout = 5000  // Timeout in milliseconds
        
        // Open a Telnet connection
        def telnet_connection = Expect.open(hostname, port, timeout)
        telnet_connection.expect("Username:")
        telnet_connection.send("${userid}\n")
        telnet_connection.expect("Password:")
        telnet_connection.send("${passwd}\n")
        telnet_connection.expect("#")
        
        // Disable pagination and set terminal width
        telnet_connection.send("terminal width 0\n")
        telnet_connection.expect("#")
        telnet_connection.send("terminal pager 0\n")
        telnet_connection.expect("#")
        
        // Send command to display the SIP UA register status
        telnet_connection.send("show sip-ua register status\n")
        telnet_connection.expect("#")
        
        // Collect the command output
        def config = telnet_connection.before()
        
        // Close the Telnet connection
        telnet_connection.send("exit\n")
        telnet_connection.expectClose()
        
        // Print the raw output for debugging
        //println config
        
        // Initialize a list to hold parsed entries
        def entries = []
        
        // Use regex to match each line of data
        def matcher = config =~ /(\d+)\s+(\d+)\s+(\d+)\s+(\w+)\s+(\w+)\s+(\S+)/
        
        while (matcher.find()) {
            def line = matcher.group(1)
            def peer = matcher.group(2)
            def expires = matcher.group(3).toInteger()
            def reg = matcher.group(4)
            def survival = matcher.group(5)
            def p_associ_uri = matcher.group(6)
        
            // Map Reg field: 0 for "yes" and 1 for "no"
            def regStatus = (reg == "yes") ? 0 : 1
        
            // Create an entry map with the mapped Reg value
            def entry = [
                'Line': line,
                'Peer': peer,
                'Expires': expires,
                'Reg': regStatus,  // Mapped value
                'Survival': survival,
                'P-Associ-URI': p_associ_uri
            ]
            
            // Add entry to entries list
            entries << entry
        }
        
        // Output the parsed entries for LogicMonitor to pick up as individual datapoints
        entries.each { entry ->
            println "${entry.Line}.Reg:${entry.Reg}"
        }
        
        // Return a success code
        return 0

        I have one datapoint named Status and it looks like this.

         

  • I tried this, I am getting an error " Connection Refused Connect" 

    • Shack's avatar
      Shack
      Icon for Advisor rankAdvisor

      Did you change line #8 to ssh port 22?  You should also be able to uncomment line #37, assuming all the expect lines are accurate and work.

  • After of changing this i am getting IO Exceptional Java Error