Get output of a command on an ssh session using AppleScript
-
02-06-2021 - |
Вопрос
I'm connecting to an ssh
session with AppleScript as follows:
tell application "iTerm2"
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 "ssh username@server"
write text "ls" -- 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 "ls"
Решение
Adding, e.g.,:
delay 1
set foo to contents
After:
write text "ls" -- I need to get the output of command
In the code of the OP, e.g.,:
tell application "iTerm"
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 "ssh username@server"
write text "ls" -- 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 "ssh username@server"
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.
Другие советы
A better way to do what you want:
-- this ssh assumes that public-key authentication is set up to the server
set the_output to do shell script "ssh name_or_ip_of_server ls"
-- the_output contans the result of the ls
Doing what you were trying to do would depend on the terminal capturing the entire ls
in the scrollback buffer and you being able to retrieve it back from the buffer. You don't need the GUI terminal (whether Terminal/iTerm) in order to do what you asked.