[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

automate UNIX commands

Posted on 2014-04-26
9
Medium Priority
?
470 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 30

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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 2000 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

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses

656 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