Get output of a command on an ssh session using AppleScript I'm connecting to an ssh session with AppleScript as follows:
tell application &quot;iTerm2&quot;
  tell current window
   create tab with default profile
  end tell

  tell first session of current tab of current window
    split horizontally with default profile
    split vertically with default profile
    write text &quot;ssh username@server&quot;
    write text &quot;ls&quot; -- I need to get the output of command
  end tell
end tell    

How can I get the output of the command on the remote machine?
The following didn't work : set x to  write text &quot;ls&quot;
Adding, e.g.,:
delay 1
set foo to contents

After:
write text &quot;ls&quot; -- I need to get the output of command

In the code of the OP, e.g.,:
tell application &quot;iTerm&quot;
    tell current window
        create tab with default profile
    end tell
    
    tell first session of current tab of current window
        split horizontally with default profile
        split vertically with default profile
        -- write text &quot;ssh username@server&quot;
        write text &quot;ls&quot; -- I need to get the output of command
        delay 1
        set foo to contents
    end tell
end tell

Gets the contents of the target in which one could then parse foo to get the information sought.

Note that while -- write text &quot;ssh username@server&quot; is commented out, nonetheless, I did test it with a valid ssh login and it did return the expected results in the contents.
Also a delay was absolutely necessary and the value of the delay may need to be adjusted as the condition warrants.