LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Enterprise (https://www.linuxquestions.org/questions/linux-enterprise-47/)
-   -   Automate Logins Thru Shell Script (https://www.linuxquestions.org/questions/linux-enterprise-47/automate-logins-thru-shell-script-942399/)

rhadmn 04-29-2012 08:57 AM

Automate Logins Thru Shell Script
 
Hi All,

I have automated the logins to a number of servers through the shell script.
But I am left with few since they are not connected directly in the sense I need to connect to a particular intermediate host and from there I need to login to the intender server.

All these servers are unix only. But I am not aware how to invoke a shell from intermediate host and connect the intended destination server.

Kind request to please provide assistance on the same asap.

Note:- As of now the script prompt for the passwords during login.
Please let me know in case if there is any way to encrypt the password and put the same in the script.

Thanks In Advance.

flamelord 04-30-2012 02:28 AM

have you tried writing an expect script (rather than a straight shell script)?

rhadmn 04-30-2012 02:35 AM

Hi Flamelord,

Thanks for your response.
I have not tried expect since the password would be visible. Is there any way in shell to do the same ?? If not, could you please provide a good link for teh expect script ?
I have browsed thru a few and was not able to find a good one where in the password could be encrypted.

flamelord 04-30-2012 07:41 PM

how is your password encrypted?

expect lets you run arbitrary bash commands, so whatever method you are using to handle encrypted passwords in bash should be able to be extended to expect.


For prompting the user, take a look at this: http://stackoverflow.com/questions/6...for-a-password

rhadmn 05-01-2012 07:37 AM

Hi Flamelord,

I would like to rather opt for prompting for the password than encrypting.
I went thru the URL you have provided and got a fair idea.

I got the expect script like below:-

set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "]0;jerry@station1:~\[jerry@station1 ~\]\$ "
send -- "ssh tom@station1"
expect -exact "station1"
send -- "\r"
expect -exact "\r
tom@station1's password: "
stty -echo
expect -exact "\r
]0;tom@station1:~\[tom@station1 ~\]\$ "
send -- "ssh jerry@station2"
expect -exact "jerry@station2"
send -- "\r"
expect -exact "\r
jerry@station2's password: "
stty -echo
expect -exact "\r
]0;jerry@station2:~\[jerry@station2 ~\]\$ "
expect eof


It is working fine. But once I get the password prompt I am not able to type anything there and the script is throwing me back.
When I tried to give the passwords and I was able to login to the destination machine through the intermediate server but again was not able to execute any commands.

Request to please check the above expect script and advice in case if I am missing somthing.

Thanks In Advance.

Reuti 05-01-2012 04:35 PM

I would suggest to use an ssh-key to log in. Running an ssh agent in the background would even allow to connect to several machines after you entered the passphrase once.

Then an ssh tunnel created by the option -L could give you access to a secondary machine behind a gateway and by using agent forwarding the same already enabled ssh-key (something like: “ssh -L 1234:server2:22 server1 &” and then “ssh -p 1234 localhost”).

A good Howto about this.


All times are GMT -5. The time now is 06:17 PM.