ssh to remote machine with 2nd user and password

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers ssh to remote machine with 2nd user and password
# 1  
ssh to remote machine with 2nd user and password

Hi all,

Really hope someone can help me, i have been trying lots of things and just cant seem to nail it - and for something that seems straight forward....

Anyway, scenario is I need to log onto a second machine (remote server) from main workstation. Once logged in I need to run a batch job, then log out and return to main workstation. I am scripting to do this automatically, therefore any passwords need to be automatically entered.

I have set up key pairs for the initial user, so I do not get prompted for the auth password into second machine, problem is I need to log into the second machine as another user so I am doing ssh -l user@host2.

The connection is being made but I am then prompted for a second password which has to be entered to allow access to relevant user directory.....I can not seem to get my script to pass through a password to the 'Password:' prompt.....the second machine then timesout and the password then echos to screen afterwards.....annoying.

Has anyone got any ideas? Experienced something similar?

Thanks a million
# 2  
some options:

ssh actively prevents that. If the originating process is a script (a job without a controlling terminal), then you can use the -b [batchscriptfile] option and have a password in it.

You can also use expect.

You can also put YOUR public key in the .ssh directory for the other user on the second remote box. This public key has to be one generated by you on the first remote box, placed on the second, not the key from the very first machine (where you started this whole mess from).

Last edited by jim mcnamara; 10-31-2010 at 12:31 PM..
# 3  
As Jim suggested, the keys are always the best option - it rules out the need for storing passwords in plain text. If you are OK with that, however, you can go for expect, personally I have written at least a dozen examples here on how to achieve this, for instance :

set timeout 60    # the login timeout - if the machine is busy, consider increasing it.

if { $argc != 1 } {
    puts "Usage $argv0 host"
    exit 1

set host [lindex $argv 0]
set pass THEPASSWORD_IN_PLAIN_TEXT # use with caution !

spawn ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=/dev/null root@$host    # replace root with user name
expect assword:

send "$pass\r"
expect *#     // change to respective shell output/prompt or just leave wildcard.
send "/full/path/to/command\r"
expect *#    // prompt again or respective output
send "exit\r"
expect eof


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #864
Difficulty: Medium
In 2019, the UserCP was rewritten in Vue.js by Neo.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Input to while loop in remote machine using ssh

Hello I am writing a script in a local machine, i am using ssh, here i am not able to using back ticks & input file to while loop. here is my script ssh -q server1 a=`ps -ef | grep ccms | awk {print $1}` b=`ps -ef | grep mss | awk {print $1}` # above lines are not working, so i redirected... (12 Replies)
Discussion started by: nanz143
12 Replies

2. Linux

Executing a script in remote machine through ssh

How to execute a script in remote machine through ssh I have a script which does some backup activity in remote machine. Wanted to keep backup also in remote machine. ssh -l username <remote machine> "commands to be exceuted as ; separted" but how to put the script in the place of... (5 Replies)
Discussion started by: sanvel
5 Replies

3. Red Hat

iptables applied in local machine, can't ssh remote machine after chain changed to DROP

I want to SSH to Server from my machine, my ip was Source Destination was UP, with IP This is LAN Network there are 30 Machine's Connected to the network and working fine, I'm Playing around the local machine's because I need to apply the same rules in... (2 Replies)
Discussion started by: babinlonston
2 Replies

4. Shell Programming and Scripting

Cannot create variables via ssh on remote machine

Hello to all Background info: Local machine : Linux, /bin/bash Remote machine (for the user used for ssh) : SunOs, /bin/ksh (so we have different OS, different Shells) My problem : From the local host i execute $ var=bla $ result=$(ssh -q user@remote-machine " > echo \"this is... (12 Replies)
Discussion started by: black_fender
12 Replies

5. Solaris

how to login with ssh to remote system with out applying the remote root/usr password

how to login with ssh to remote system with out applying the remote root/user password with rlogin we can ujse .rhosts file but with ssh howits possible plz guide (2 Replies)
Discussion started by: tv.praveenkumar
2 Replies

6. Shell Programming and Scripting

Password change logic for remote machine using shell and expect

Day before yesterday,I got the success creating a shell script using expect tool and now it is running successfully on the server. Now I want to make some changes in as you can see it can handle only one user i.e dbaguest and not other user.So I am thinking the logic how I can... (0 Replies)
Discussion started by: manish_1678
0 Replies

7. Shell Programming and Scripting

ssh connection from remote machine in solaris

Hi! I have two solaris 10 machines(say, i have installed rsync on, Sun Microsystems Inc. SunOS 5.10 Generic January 2005 -bash-3.00$ ssh "echo $PATH" Password:... (4 Replies)
Discussion started by: dddkiran
4 Replies

8. Shell Programming and Scripting

[SSH] Need to connect to remote server as different user and without password

I have a task requiring that USER_A run a script, which connects to HOST_B as USER_B and does not ask for a password. If I am logged in on HOST_A as USER_B, I can connect to HOST_B without a password, no problem. However, if I try running ssh with the command line "ssh USER_B@HOST_B" while... (3 Replies)
Discussion started by: Totengraber
3 Replies

9. Shell Programming and Scripting

without password to login into remote machine- in the script ??

HI, I need to write a script .. when I run this script , will directly goto that remote machine without asking password.. Once it is entered, I needs to transfer some of the log files... how can I proceed ? (7 Replies)
Discussion started by: hegdeshashi
7 Replies

10. Shell Programming and Scripting

executng program on remote machine using ssh

I am trying to search and remove files from a list of servers. I want to find every occurence of this file on each machine and then remove it. If I execute the find command on the remote machine I would like to be able to pipe the output to xargs and remove the file. Does anyone know hat would be... (1 Reply)
Discussion started by: sewood
1 Replies