0

Before this question gets deleted, I already read this, this, this, and this; and most of these links don't answer my question; because:

  1. I do need a key AND password
  2. I run multiple servers (50+), and I can't afford changing them all to no password.
  3. The same is true for public and private keys, would take up too much time for every server.

I tried sshpass, but it somehow messed up my SSH login with some of my units (I had to reset the known_hosts of the server). If I could modify my script to work with sshpass properly, it would be a possible solution.

I'm hoping to find, is some sort of option on ssh, that will automatically do the password; something like:

ssh -o ConnectTimeout=5 -p 'USER_PASSWORD' USER@192.168.0.*** sensors | grep Core >> sensors.txt

Or something with a small script that can sense and auto-fill (like a macro or so?) a password (half the servers have the same PWD anyway).

My second issue is that I noticed that the script sometimes hangs (Ping connects, but SSH doesn't), which requires me to wait for a timeout, or restart the script. Not sure if it's a network issue, or what, but if I restart it, sometimes it does go through.

ConnectTimeout=5 doesn't seem to work like expected. The script only stops, when the server is offline (no IP link established). When the server is online, but openssh doesn't handshake, the script hangs...

3
  • You need a configuration management system. Jul 14, 2020 at 7:09
  • And your second issue should be a separate question. Preferably after you solved the first, if it still persists then. Jul 14, 2020 at 7:12
  • 1
    If you "can't afford" to reconfigure your servers for the sake of monitoring, then how do you update them? How is installing keys "take up too much time", and keeping the servers up-to-date isn't?
    – Lacek
    Jul 14, 2020 at 10:00

1 Answer 1

1

I used expect to automate keyless logins. Here is my code. Shell wrapper:

#!/bin/bash
    HOSTNAME=$1
    case `echo ${HOSTNAME:7:1}` in
            d|q)
              ENV='sit'
              PW='pw1'
              ;;
            u)
              ENV='uat'
              PW='pw2'
              ;;
            p)
              ENV='prod'
              PW='pw3';
              ;;
    esac
    # Pass hostname and password to expect script
    ./go.exp $HOSTNAME $PW

Expect script:

#!/usr/bin/expect -f
set hostname [lindex $argv 0];
set password [lindex $argv 1];
spawn ssh your_username@$hostname
expect "*:"
send "$password\n"
send "bash\n"
expect "bash-*"
# The next two lines are prompt customization, it is not necessary
send "export PROMPT_COMMAND='echo -ne \"\\033k\$HOSTNAME\\033\\\\\"'\n"
send "export PS1='\\e\[0;33m\\u\\e\[0;36m@\\e\[0;33m\\h \\e\[0;32m\\w> \\e\[m'\n"
# If you don't need interactive session then you may replace the below with whatever you need
interact

Note: It is assumed that username and prompt are the same everywhere, moreover this is not secure because password is passed as arg in clear text and therefore is visible (for example in ps output) by other users

Not the answer you're looking for? Browse other questions tagged or ask your own question.