Trouble with Fortigate PropertySource script
I am trying to write a Groovy PropertySource script, which will SSH into Fortigate devices and return the list of available VDOMs.
In Putty, I would run the command
config vdom -> Enter
Then I would type "edit ?" and without hitting enter, the list of available VDOMs would appear (where each VDOM entry beginning with "<vdom> Virtual Domain Name"):
I have the following Groovy script (modified from a ConfigSource in the portal):
import com.santaba.agent.util.Settings import net.schmizz.sshj.SSHClient import net.schmizz.sshj.connection.channel.direct.Session import net.schmizz.sshj.transport.verification.PromiscuousVerifier import java.util.concurrent.TimeUnit import java.security.MessageDigest //def host = hostProps.get("system.hostname") def host = "<ip address>" //def user = hostProps.get("ssh.user", hostProps.get("config.user")) def user = "<username>" //def pass = hostProps.get("ssh.pass", hostProps.get("config.pass")) def pass = "<password>" //def port = hostProps.get("auto.config.port", "22") def port = "22" def time = (Settings.getSettingInt("configcollector.script.timeout", 120) - 1) * 1000 def startTime = System.currentTimeMillis() def client = new SSHClient() client.setTimeout(time) client.setConnectTimeout(time) client.addHostKeyVerifier(new PromiscuousVerifier()) try { client.loadKnownHosts() } catch (ex) { /* No Known Hosts File */ } def session try { client.connect(host, port.toInteger()) if(pKey && new File(pKey).exists()) { client.authPublickey(user, client.loadKeys(pKey)) } else { client.authPassword(user, pass) } session = client.startSession() session.allocateDefaultPTY() def cmd = session.exec("config vdom\n edit ?") cmd.join(time.toInteger(), TimeUnit.MILLISECONDS) cmd.close() def rawOut = cmd.getInputStream().text print "rawout: " + rawOut def out = rawOut.trim() if(out) { print out } } catch (Exception ex) { println ex } finally { session?.close() client?.disconnect() }
The result is only "true" (from !groovy).
I'm not super great with Groovy, what am I missing here? Thanks.
- Anonymous4 years ago
Hm, so the issue is that, when executing these commands manually, the question mark itself prompts a response from the target device. I'm not familiar with the library used to perform the SSH session here, but the target device should receive the question mark and spit out the output the same. Do you need to get the output stream instead of the input stream?
def rawOut = cmd.getInputStream().text
Like i said, not familiar with that particular method of connecting via SSH. I use JSch or Expect.