Solved

automate UNIX commands

Posted on 2014-04-26
9
439 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
Comment Utility
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 27

Expert Comment

by:serialband
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:toooki
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Did you try to capture your session with autoexpect ?

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

Featured Post

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.

Join & Write a Comment

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now