1

I want to write a shell script to connect multiple linux hosts and collect server side information / monitoring details.

As I need to ssh username@hostname for the given list of servers, I am just wondering I can enter / hardcode the password onetime and prevent ssh command to prompt for password for each ssh command.

I use the same password for across all servers.

I wrote one shell script, buts its keep on prompting me to enter password for ssh command in the loop.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Rao
  • 11
  • 2
  • 1
    That's why they have `ssh-agent` for such use cases. Depending on which OS platform you are using, `ssh-agent` might already be installed. – Emacs User Jun 01 '16 at 00:58
  • A "one time password" is a term of the art, and it's something very much unrelated to your question. You might want to use a different term. – Charles Duffy Jun 01 '16 at 01:12
  • BTW, asking for an answer "ASAP" or flagging a question as urgent is considered rude. See also http://www.catb.org/esr/faqs/smart-questions.html#urgent – Charles Duffy Jun 01 '16 at 01:14

2 Answers2

1

Try to use sshpass.

sshpass -p "password" ssh -o StrictHostKeyChecking=no user@server

Or use public key authentication (you will establish ssh connect by pair of keys without password promt):

ssh-keygen -t rsa 
ssh-copy-id user@server
Serenity
  • 35,289
  • 20
  • 120
  • 115
  • Passing a password on the command line is **extremely** insecure -- command lines are visible to every other process on the system. Even if `sshpass` overwrites the command line after it's started up, there's still a window of opportunity. – Charles Duffy Jun 01 '16 at 01:15
  • ...even `sshpass -f <(printf '%s\n' "$password")` would be better (since `printf` is a shell builtin, it doesn't run an external command with a password on its argv). – Charles Duffy Jun 01 '16 at 01:16
1

Stanley R's answers are good.

Also, consider use of ControlMaster, which should make your script run a lot faster, because you avoid the cost of connecting every time. As a side-effect, it should mean that you only have to sign in once, so it'll solve your problem by fluke.

https://en.wikibooks.org/wiki/OpenSSH/Cookbook/Multiplexing

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39
  • That makes sense for multiple connections to one host, but this is one connection per host for a number of remote hosts. – Charles Duffy Jun 01 '16 at 01:17