Solved

automate UNIX commands

Posted on 2014-04-26
9
446 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
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 28

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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 61

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bad ownership or modes for chroot directory 6 68
Automating a script for user accounts LINUX 14 70
nagios 4 php error after installation 6 78
VMware Workstation 12 Player 16 41
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now