Iterm2: Automate sending text to shell upon start

iterm2

Using profiles you can send text to the shell at the start of a session, under "send text at start". I'd like to do this 3 times in succession, but it looks like there's only one string allowed. Does anyone know of any other way to do this using iterm2? I believe I can use applescript but I'd rather be able to start a session using iterm2 directly.

Ex:

<terminal session starts>
ssh server
send "asdf"
send "qwer"
send "zxcv"
(done...)

The reason why I can't simply set a profile to do this is that my work has a special environment that usually asks 3 questions. I want to automate answering these 3 questions and set them to the iterm2 profile config under "send text at start".

Best Answer

There's a tool called Expect that does generalized handshaking and communication that you could use, however, iTerm2 can do some of what Expect does straight from the Advanced tab.

Note: Expect is written in TcL, an awesome language to know in its own right. This particular question is probably spelled out with a nicely commented answer in an Expect FAQ somewhere. Also, if you chose to implement a solution with Expect over iTerm2, you can use that solution on more than your OSX host. So think about that trade up front. In the short-term, the iTerm2 Triggers option will do the trick for you.

So, to provide some of what Expect provides straight from iTerm2, goto the Advanced tab. There you will find the Triggers capability. From there you can add "+" a specific trigger rule. In a nutshell, what these triggers (and Expect) allow you to do is filter all text to and from the Terminal, match that text against the user specified regular expressions (ReEx) trigger, and when that ReEx is matched in the Terminal's text stream, iTerm2 will perform a user specified task.

Here's one trigger rule that will match a line containing exactly one word, Password, and then Send Text: My Password.

enter image description here

Keep in mind that the Terminal Text stream contains unseen characters, and specifying the exact ReEx to match can be a challenge, especially, for instance, because different servers implement different line separators, and if a ReEx specifically uses one, it can not match for no apparent reason.

One thing: I'm thinking about why you may be asking this question? Not sure of what you are familiar with, but if you want to auto-login via ssh, add a comment, and setting that up is a piece of cake. If you are invested in this, search for keywords "ssh-keygen" and "generate rsa public keys for ssh."

I know this is a comment, but I thought I'd throw this out there since I already have the space - for what it is worth:

I think one would use the Send text at start: to set environment variables which are relevant to the remote host session. For instance, I used this method to pass parameters to my remote processes as a means of passing parameters through environment variables. I'm not certain, but that would be a use of that Command field to tailor what happens when a particular ssh userid@hostname is run.

See the second answer at Is it possible to get SSH to forward empty arguments? for the full example, but it all boils down to the following syntax:

$ ssh me@localhost 'VAR1="var1 text" /tmp/try.pl'

I hypothesize that one potential use of the Send text at start: option is to provide the VAR1="var1 text" /tmp/try.pl, or simply, the VAR="var1 text" to the remote process's environment.

Here's a more concrete example from scratch using a Dual-G5 Mac host named, r2d2:

Here's how I set up r2d2's profile:

enter image description here

Now when I kick of an r2d2 iTerm2, and echo $FRED, is see that any command I would have invoked a la the ssh could examine the environment and determine a context for execution:

enter image description here

Of course, this is speculation given what I've experienced, so it is just one possibility. I now know that I'll be able to exploit this control path, so thanks for the question.

Related Question