how to initiate an ssh command from a MID server

haldroid
Tera Contributor

Sometimes I want a MID server to start a task on a remote system without doing a discovery probe or a remote data import. Of course this could be run manually or as a cron job on the MID server, but I want to consolidate control on the Service Now instance. The Wiki entry for MIDServerWorkerCommandLine says this can be done. Here's a complete example, shown as a scheduled job. This capability requires the SNC Discovery plugin.

You have MID server MYMID01 and you want to run the "uptime" command on server s01.sample.com. SSH credentials are in place (e.g. the MID server can run discovery probes on the target server).

  1. Create a Scheduled Job Entry

    Scheduled Jobs > New > Automatically run a script of your choosing >
    Fill in form as follows.

    Name: My SSHCommand Job
    Active: (checked)
    Run: On Demand
    Time: 00 00 00
    Conditional: (not checked)

    In the textarea for "Run this script", enter the following:


    var probe = new Packages.com.snc.discovery.Probe();
    probe.setName("uptime");
    probe.setTopic("SSHCommand");
    probe.setAgent("mid.server.MYMID01");
    probe.setSource("s01.sample.com");
    probe.addParameter("port", 22);
    probe.addParameter("skip_sensor", "true");
    probe.create();

    and click Submit.
  2. Run the job:
    Scheduled Jobs > My SSHCommand Job > Execute Now

  3. Check results:
    ECC > Queue
    Filter: All > Created on Today > Topic = SSHCommand
    There should be two ECC entries for the job run, one with Queue value of "output", and another with Queue value of "input". You may need to refresh the view a few times before both entries are there. Click on the record for the ECC "input" queue and view command result in the XML payload under ....

Thanks to Chris at ServiceNow and Clark at Fruition for helping fill in the details.

1 REPLY 1

tony_fugere
Mega Guru

Great write-up! You can do the same thing with Topic = "Command" for Windows, too.