88

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

IC001:Desktop user$ ssh [email protected]
[email protected]'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 [email protected]
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.

5

2 Answers 2

187

Double check if you are not able to use keys.

Otherwise use expect:

#!/usr/bin/expect -f
spawn ssh [email protected]
expect "assword:"
send "mypassword\r"
interact
Sign up to request clarification or add additional context in comments.

7 Comments

uh...Is "assword" a typo or intended?
It is intended: It matches Password and password.
I tried this in TextExpander, but I get spawn: command not found. I tried it without the keyword, but nothing happens.
@jowie you should install expect with sudo apt-get install expect for example and then you'll have the spawn command
lol at the "uh..." comment
|
96

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 [email protected]

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

ssh [email protected]

14 Comments

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?
Needed to: ssh-agent bash && ssh-add as shown here solomonson.com/content/… to get this to work.
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.
If you're on OSX or another OS that doesn't have ssh-copy-id then there are one-line alternatives here.
@Chev Also, ssh-copy-id is available from Homebrew.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.