89

Everyday I am connecting to a server through ssh. I go through this routine:

IC001:Desktop user$ ssh user@my.server.com
user@my.server.com's password: 

Last login: Tue Jun  4 10:09:01 2013 from 0.0.0.0
$

I would like to automate this process and create a bash script to do it for me. I don't care about security and okay to store my password openly in the script. I am also okay for it to get typed openly on the screen while the script gets executed. So I've created this:

#!/bin/bash          
ssh user@my.server.com
echo mypassword

But it doesn't work. I've also tried send instead of echo, but it also didn't work. Please advise if it is possible to do.

Prostak
  • 3,565
  • 7
  • 35
  • 46
  • https://help.ubuntu.com/community/SSH/OpenSSH/Keys – Kevin Jun 04 '13 at 21:46
  • 4
    @Kevin, I am not in charge of the servers. I am just a user which connects to them. I am not allowed to modify ANYTHING on the server. – Prostak Jun 04 '13 at 21:49
  • 1
    also: http://stackoverflow.com/questions/4780893/use-expect-in-bash-script-to-provide-password-to-ssh-command?rq=1 http://stackoverflow.com/questions/13298487/bash-script-to-ssh-into-a-machine-without-prompting-password-and-without-using-k?rq=1 http://stackoverflow.com/questions/3796345/can-i-use-a-heredoc-to-enter-a-password-in-bash?rq=1 – clt60 Jun 04 '13 at 21:51
  • 1
    All you need to do on the server is add a single line to a configuration file in your home directory. If you can't do that, what can you do? – Kevin Jun 04 '13 at 22:44
  • 2
    @Kevin, read logs... but that's irrelevant. – Prostak Jun 05 '13 at 00:01

2 Answers2

185

Double check if you are not able to use keys.

Otherwise use expect:

#!/usr/bin/expect -f
spawn ssh user@my.server.com
expect "assword:"
send "mypassword\r"
interact
michas
  • 25,361
  • 15
  • 76
  • 121
95

Create a new keypair: (go with the defaults)

ssh-keygen

Copy the public key to the server: (password for the last time)

ssh-copy-id user@my.server.com

From now on the server should recognize your key and not ask you for the password anymore:

ssh user@my.server.com
michas
  • 25,361
  • 15
  • 76
  • 121
  • I am not allowed to do anything on the server. Is it possible to do it the other way? – Prostak Jun 04 '13 at 21:57
  • What is the point of logging in, if you are not allowed to do anything there? – michas Jun 04 '13 at 22:17
  • to read the logs only... but that's not the point. – Prostak Jun 04 '13 at 22:18
  • 5
    So the point is that you have very limited access to a server whose admin has locked down security and you want to leave your password in plaintext on your machine because the server owner's security concerns are bothersome to you. Did I get that right? Why don't you ask that sysadmin how he feels about your script? – msw Jun 04 '13 at 22:55
  • 1
    Needed to: ssh-agent bash && ssh-add as shown here http://www.solomonson.com/content/setting-ssh-agent-ask-passphrase-only-once to get this to work. – Gary Thomann Nov 08 '13 at 11:59
  • 5
    Oh snap! lmao @Prostak. I go to great lengths to distill my questions down to the bare minimum so that answerers/commenters don't get their panties in a bunch about things that have nothing to do with the actual question. – CatDadCode Jan 30 '14 at 21:02
  • 7
    If you're on OSX or another OS that doesn't have `ssh-copy-id` then there are one-line alternatives [here](http://www.commandlinefu.com/commands/view/188/copy-your-ssh-public-key-to-a-server-from-a-machine-that-doesnt-have-ssh-copy-id). – CatDadCode Jan 30 '14 at 21:10
  • Will not work in all type of devices. Some devices are router and not Linux – Ahmed Apr 29 '15 at 18:16
  • You can do [key based authentication](http://www.cisco.com/c/en/us/td/docs/ios/sec_user_services/configuration/guide/15_1/sec_user_services_15_1_book/sec_secure_shell_v2.html#wp1082784) for routers, too. But this would go beyond the scope of that question. – michas Apr 29 '15 at 20:23
  • 4
    @Chev Also, `ssh-copy-id` is available from Homebrew. – robert Dec 21 '15 at 23:12
  • @robert so simple ... so easy ! +1 – Cyryl1972 Jan 12 '16 at 09:50
  • If I sore my ssh key not in is_rsa which command I should use instead ssh-copy-id? Or it is the same so it will use the last generated key? – fdrv Feb 01 '17 at 14:47
  • @fdrv Please read `man ssh-copy-id`. It will tell you about the `-i` option. – michas Feb 01 '17 at 14:53
  • 1
    @Chev ssh-copy-id is on macOS (tested on Catalina) – TimD Feb 23 '20 at 07:34