[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 475
  • Last Modified:

automate UNIX commands

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
toooki
Asked:
toooki
1 Solution
 
omarfaridCommented:
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
 
serialbandCommented:
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
 
toookiAuthor Commented:
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
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!

 
omarfaridCommented:
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
 
toookiAuthor Commented:
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
 
omarfaridCommented:
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
 
gheistCommented:
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
 
toookiAuthor Commented:
The following line
expect "$" { send ssh [lindex $argv 3] \r }

in place of

spawn ssh [lindex $argv 3]

did not work.
0
 
omarfaridCommented:
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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now