xiaohuamao
xiaohuamao

Reputation: 190

How to automatically open prompts, ssh, and do things using a script?

I have to connect to a linux server from my own Ubuntu machine and operate directly on the server. A dozen of folder names are listed in a LIST file. How to write sth. (like a bash script?) to carry out the following procedures?

for fold_name in LIST {  
/******on my own Ubuntu*******/  
 -- open 2 new tabs of prompt terminal  
 -- run an ssh command in both  
 -- then input passwd and log in automatically in both  
/******on the linux server*******/  
 -- cd to directory xxx/fold_name in both  
 -- run aaa.exe in 1st tab  
 -- vim sth in the 2nd tab  
}  

Once the loop of open-tab-login is solved, I guess the second part is routine as simple bash script except that I don't know how to specify between 2 tabs, either.
The point is I want all tabs in the same terminal. To this end, manually, I often Ctrl+Alt+T to create a prompt and Ctrl+Shift+T to open many tabs within it. And ssh...cd...... in each one. You see how annoying and cumbersome it is!

Upvotes: 0

Views: 1997

Answers (2)

Sam Varshavchik
Sam Varshavchik

Reputation: 118435

-- open 2 new tabs of prompt terminal

This depends on which desktop you're using. For gnome, gnome-terminal takes the -e option to specify the script to execute in the new terminal window. So, for something like this, you would execute gnome-terminal -e $script &, placing each instance of gnome-terminal in the background.

If you're using a different desktop, other terminal applications typically have a similar option. So, you'd point the terminal application to a script that's going to run in the terminal, and complete the rest of your task for you.

-- run an ssh command in both
-- then input passwd and log in automatically in both

This is going to be more complicated. The classical solution is the expect utility. There might be other similar tools that do similar things, but expect is pretty much the usual way these kinds of things have been done in the past. When it comes to trying to automate an interactive application, expect is really the only way to go. Unfortunately, expect uses a somewhat arkane syntax, that first-time users typically find confusing, and hard to understand. I grumble, every time I see no other alternative but to use expect to automate something, but this is pretty much the only option that's usually available.

Upvotes: 1

AMADANON Inc.
AMADANON Inc.

Reputation: 5929

There are a few things you might like to research, which will get you a little closer.

You can run an ssh without a password, if you use an ssh key. Check out ssh-keygen, and the -i option in ssh.

Opening up tabs in gnome-terminal can be done via the method described here: Open a new tab in gnome-terminal using command line

You can run specific commands (e.g. aaa.exe) on a remote box over ssh, by including the command after the ssh: ssh user@remotehost aaa.exe.

If you want multiple commands, try enclosing them in quotes: ssh user@remotehost "cd /xxx; aaa.exe". Vim does not need to be in the directory in question in most cases: ssh user@remotehost vim /xxx/filename"

If you want to do something interactive (like vim), include the -t flag in ssh.

The tabs will be independent of each other - I'd probably run half of the command in one window, the other (e.g. runnning aaa.exe in one window, using one command, and the vim in another window, using another command, that I just happen to run at the same time. This way I can resize the windows, and arrange them relative to each other, and see both at once.

Upvotes: 2

Related Questions