Passing password for ssh in Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing password for ssh in Script
# 1  
Old 12-15-2011
Passing password for ssh in Script

I want to do following 2 commands via script:

1) eval `ssh-agent`2) ssh-add /export/home/sufuser/.ssh/id_rsa When asked for passphrase enter "passwordpassword1234

but whenever I run the script it stucks after "ssh-add /export/home/sufuser/.ssh/id_rsa" command and asks fro password
although I have given password in script its not taking it and asking to input [assword mannually , but if I am inputtting the password manually its not working .....
If i do thses steps manually 1 bye 1 ... everything works very fine . . .

Need your help in prepatation of such a script which will take ssh password via script
# 2  
Old 12-15-2011
Hi,

explore 'expect' utility to manage non-interactive ssh session.

A barebone script in expect may look something like this:

Code:
#!/usr/bin/expect

#   get parameters from command arguments
set host [lindex $argv 0]
set user [lindex $argv 1]
set password [lindex $argv 2]

#   set remote host shell prompt to correct value
set shell_prompt "~]# "

#   spawn ssh session to host
spawn ssh $user@$host

expect {
          "assword:" {
             send -s "$password\r"
             #the expect command will now return
          }
}

expect {
        "$shell_prompt"
}

sleep 1
send -s -- "YOUR COMMAND HERE\r"

expect {
        "$shell_prompt"
}

#   close connection to remote host
send -s -- "exit\r"
send -s -- "\r"

#   end of expect script
exit 0

invoke the script as usual:
Code:
$ ./myscript.expect remotehost remoteuser remotepassword

see ya
fra

Last edited by frappa; 12-15-2011 at 06:50 AM.. Reason: errors corrections
# 3  
Old 12-15-2011
Hi fra, thanks for your prompt reply.
can you please give me exact script with following requirment
1)eval `ssh-agent`
2) ssh-add /export/home/sufuser/.ssh/id_rsa When asked for passphrase enter "passwordpassword1234"
3) dumpsubscribers user root outputDir /ISS/dump/

All above three stpes should be executed in sequence

Thanks,
Yogesh
# 4  
Old 12-15-2011
Hi,

may you please clarify what you're actually trying to do? I mean, explain the objective of the actions you'd like to perform on the remote server (i.e: first transfer of files, second execution of remote commands etc.)

see ya
fra
# 5  
Old 12-16-2011
Hi Fra.

Actually I am trying to ssh into my database by follwoing 2 commands:
1)eval `ssh-agent`
2) ssh-add /export/home/sufuser/.ssh/id_rsa


and then running following comamnd to dump data to drirectory /ISS/dump/ by command

dumpsubscribers user root outputDir /ISS/dump/

This command will dump all data in *.zip file format to /ISS/dump/ .

I want to automize this by preparing 1 script and adding it to crontab.

-Yogesh


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Expect script not passing password / commands ??

Newbie here. My goal is to have the expect script log into the Ubuntu 18.04 server and run two commands (lsb_release -a and ip addr) and eventually pipe the output/results to a file. For now, I would be happy to get this one command or two to run successfully. How to fix this? #!/usr/bin/expect ... (3 Replies)
Discussion started by: jacob600
3 Replies

2. Shell Programming and Scripting

Passing Username & password through shell script to java code

Hi, I have a shell script (script.sh) in which we are calling java code which asks for Username: Password: for authentication purpose currently we are passing the credential manually and run the script. but I am trying echo -e "user_id\npassword" | script.sh but its not... (1 Reply)
Discussion started by: rakeshtomar82
1 Replies

3. Shell Programming and Scripting

Passing password with SSH command

Hi Experts, I have specific requirement where I want to pass the password with the ssh username@hostname command . I dont want to use RSA public and private keys also. Because that will be on production server and no one wants to give access like that. Second thing it is production... (14 Replies)
Discussion started by: sharsour
14 Replies

4. Shell Programming and Scripting

Passing Password to SSH without using expect in a Script

How can I pass password in SSH command without using expect in a shell program. I don't have expect installed on my Solaris server. #!/bin/bash ssh user@hotname (how to supply pass in script?:wall:) Experts please help its very urgent. Shrawan Kumar Sahu (4 Replies)
Discussion started by: ss135r
4 Replies

5. Shell Programming and Scripting

Passing password in script for ssh connection - no except

Used the script posted on forum - unix.com/shell-programming-scripting/21597-script-change-passwords-same-user-multiple-servers.html but the last question posted on this seems to be still unanswered, tried different things with no success, can someone help giving an way to pass the password via... (5 Replies)
Discussion started by: sapadmin
5 Replies

6. Shell Programming and Scripting

passing database password to isql command in shell script

Hi, I need to connect to DB through my shell script. but I dont want to hardcode my db password in the script. Is there a way to do it? Thanks ---------- Post updated at 07:42 PM ---------- Previous update was at 04:54 PM ---------- :(Guys..please help me with this:( (1 Reply)
Discussion started by: agrawal.prachi
1 Replies

7. Shell Programming and Scripting

Passing a MySql password from bash script

Hi all, I am running this script on Mandrakelinux release 10.1, 2.6.8.1-12mdksmp #1 SMP I have also installed 'expect' separately. I have created an Rsync script, but before any Rsync command does run, a MySql dump must be done first, and I am battling a bit to pass the MySql password from... (2 Replies)
Discussion started by: codenjanod
2 Replies

8. Shell Programming and Scripting

SSH Login by passing password.

ssh/sftp login by passing password , is it possible.Don't want to expect. (1 Reply)
Discussion started by: dinjo_jo
1 Replies

9. Shell Programming and Scripting

ssh - passing password in shell script

I have a requirement, I need to run a command at remote system using a ssh. Is there any way we can pass the username and password in shell script to the ssh command as we did it in one of the shell script for FTP. ftp -n $i <<!EOF >> user Username $PASSWD cd /home/scripts ... (5 Replies)
Discussion started by: Muktesh
5 Replies

10. Shell Programming and Scripting

passing password in shell script

Hi, Anybody help to write a shell script as below requirement. script stops some costomized application services. 1. first it will stop apache as root 2. then it will switch to my application user and stop app service. Note: passing password is required to stop app service as app... (1 Reply)
Discussion started by: manojbarot1
1 Replies
Login or Register to Ask a Question