Solved

automate UNIX commands

Posted on 2014-04-26
9
455 Views
Last Modified: 2014-05-21
I need to execute the following UNIX commands in sequence quite often. Is there any way to save the commands and execute it via some alias or so?
I need to execute:
$ssh server1
password: server1_password
$ssh server2
password: server2_password
$sudo su - user1
$sudo su - user2

The ssh commands above are not password less. How can I embed the passwords in a file and execute the file instead of the above commands? The servers are UNIX servers where I ssh to. And my PC is a Mac OS.

Many thanks for the help.
0
Comment
Question by:toooki
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 40

Expert Comment

by:omarfarid
ID: 40025279
You need to use expect to automatically response to prompts. Below link is an example of how to use expect and ssh:

http://www.journaldev.com/1405/expect-script-example-for-ssh-and-su-login-and-running-commands
0
 
LVL 29

Expert Comment

by:serialband
ID: 40025371
Create an ssh key pair, put the public key in the remote account ~/.ssh/authorized_keys and load your private key before connecting

http://www.linuxproblem.org/art_9.html
0
 

Author Comment

by:toooki
ID: 40025441
Thank you all.
I followed the instruction at http://www.linuxproblem.org/art_9.html and it did not work (ssh user2@serverB) asked for the password.

I followed http://www.journaldev.com/1405/expect-script-example-for-ssh-and-su-login-and-running-commands and it did not work either.

This is my code:

spawn ssh [lindex $argv 0]

expect "yes/no" {
    send "yes\r"
    expect "*?assword" { send "[lindex $argv 2]\r" }
    } "*?assword" { send "[lindex $argv 2]\r" }

spawn ssh [lindex $argv 3]

expect "yes/no" {
    send "yes\r"
    expect "*?assword" { send "[lindex $argv 2]\r" }
    } "*?assword" { send "[lindex $argv 2]\r" }

I execute the above and the first ssh succeeds. Next one stalls (apparently the 2nd ssh command executes as if is executing on the first server). Also I do not understand why the first ssh takes so long compared to my command line ssh command.

The reason I need to do two ssh in sequence is because the direct ssh to the 2nd server does not work (stalls).

Thank you
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 40

Expert Comment

by:omarfarid
ID: 40025474
can you elaborate more on the requirements? Are you trying to ssh to server 1 then ssh to server 2 and then execute commands?

Try to add expect prompt before running commands
0
 

Author Comment

by:toooki
ID: 40025499
Thank you. Yes I ssh to serverA and the ssh from serverA to serverB.
So this is how I usually run the commands:
me@MacPc$ssh serverA
password: somePassword
me@serverA$ssh serverB
password: somePassword
me@serverB: sudo su - user1
user1@serverB$
user1@serverB$sudo su - user2
user2@serverB$

Sorry I did not understand: "Try to add expect prompt before running commands".

Also in serverA there is no "expect".
me@serverA$which expect
The above returns ...no expect in ....

Thanks.
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 40025511
Ok, I think you need to add one line like


expect "$" { send ssh [lindex $argv 3] \r }

in place of

spawn ssh [lindex $argv 3]

I don't have linux box to test script.

You may also try to use autoexpect to generate the expect script from a real session. Please see below:

http://expect.sourceforge.net/example/autoexpect.man.html
http://www.admin-magazine.com/Articles/Automating-with-Expect-Scripts
0
 
LVL 62

Expert Comment

by:gheist
ID: 40027040
In theory you should use ssh public key authentication for users you need to log in as.
su is very bad even for expect.
0
 

Author Comment

by:toooki
ID: 40035170
The following line
expect "$" { send ssh [lindex $argv 3] \r }

in place of

spawn ssh [lindex $argv 3]

did not work.
0
 
LVL 40

Accepted Solution

by:
omarfarid earned 500 total points
ID: 40036147
Did you try to capture your session with autoexpect ?

Also the $ should be replaced with actual prompt of the remote server
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
The better OS Architecture 13 92
parallel rsync issues with Ubuntu 1 41
Shell script issue 4 55
IMAP copying tool 14 49
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question