ssh to another server with different username ssh to another server with different username unix unix

ssh to another server with different username


You want to use key-authentication http://ornellas.apanela.com/dokuwiki/pub:ssh_key_auth

  1. Generate your keys ssh-keygen
  2. Copy the keys to your new box ssh-copy-id -i ~/.ssh/id_rsa.pub me@otherhost.com
  3. ssh to other host without password ssh me@otherhost.com


You can use expect to wrap ssh, but it's pretty hectic, and fails easily when there are network errors, so test it well or use a script specifically designed for wrapping ssh passwords. Key based authentication is better.

You can prevent interactive sessions by redirecting standard input from the null device, ie.

ssh me@destination destination-command < /dev/null

About placing the script in the source server, if the script you are running is local, rather than remote, then you can pass the script on standard input, rather than the command line:

cat bashscript.sh | ssh me@destination 


You can install the sshpass program, which lets you write a script like

#!/bin/bashsshpass -p bob123 ssh UserName@xyz.efc.com