I've grown tired of typing my password back and forth to all the hosts you connect to, i want to be able to jump to every single user on all the hosts with ease. 

So i've made this script with a quite messy oneliner at the end of it.

The script first reads user input where to connect to.

**remote\_user, remote\_host and remote\_port**.

```
echo "Enter the user you want to connect with (sudo needs to be enabled and installed)"
read -e rUser

echo "Enter the host you want to connect to"
read -e rHost

echo "Enter the SSH-port"
read -e rPort

read -s -p "Enter Password: " password 

```

Checks if ssh-key is generated, if not proceeds with ssh-keygen for the user to generate a strong key.

```
if [ ! -d "$HOME/.ssh" ] && [ ! -f "$HOME/.ssh/id_rsa" ] && [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
    echo -e "Private / Public keys not generated"
    echo -e "Generating..."
    ssh-keygen -b 4096
fi

```

After that we proceed with establishing ssh-connection and creating the authorized\_keys file on the remote host.

(Annoying 'bug' here you have to enter the password twice, since it doesnt seem possible to read ssh-password from stdin.)

```
cat "$HOME/.ssh/id_rsa.pub" | ssh "$rUser@$rHost" -p $rPort "cat >> ~/.ssh/authorized_keys"

```

Since the script will only be run once per remote host, its acceptable.

We continue with the quite messy oneliner.

We're establishing an SSH connection, to solve the problem with password over SSH we pass the -S parameter to SUDO that lets us read the password from STDIN. Now our subsequent SUDO calls will work. We proceed and create the .ssh directories for all the users on the system if they dont exist.

We create the authorized\_keys file for all users and set correct owner, then we append authorized\_keys file of the user we connected with.

```
remoteUsers=($(ssh "$rUser@$rHost" -p $remotePort  
'echo '"$password"' | sudo -S ls /home/ && for localUser 
in $(ls /home | grep -v $USER); do sudo mkdir -p /home/$localUser/.ssh && 
sudo touch /home/$localUser/.ssh/authorized_keys 
&& sudo chown "$localUser:$localUser" /home/$localUser/.ssh/authorized_keys && 
cat /home/$USER/.ssh/authorized_keys | sudo tee 
 --append /home/$localUser/.ssh/authorized_keys > /dev/null; done'))

```

Not that pretty. But i don't know how to improve this without considering another approach for the sudo over SSH problem.

And finally we write our .ssh/config with all the users in it.

```
for remoteUser in "${remoteUsers[@]}"
do
   cat <<< "Host ${remoteUser}_${rHost}
            HostName $rHost
            Port $rPort
            User $remoteUser" >> /home/$USER/.ssh/config
   done

```

Below is the script in its entirety.

```
#!/bin/bash

echo "Enter the user you want to connect with (sudo needs to be enabled and installed)"
read -e rUser # -e Identifier bash feature

echo "Enter the host you want to connect to"
read -e rHost # -e Identifier bash feature

echo "Enter the SSH-port"
read -e rPort

read -s -p "Enter Password: " password 

# Generate private public ssh-keys 
if [ ! -d "$HOME/.ssh" ] && [ ! -f "$HOME/.ssh/id_rsa" ] && [ ! -f "$HOME/.ssh/id_rsa.pub" ]; then
    echo -e "Private / Public keys not generated"
    echo -e "Generating..."
    ssh-keygen -b 4096
fi

# Send public key to remote host
echo "Sending public key to remote host..."
cat "$HOME/.ssh/id_rsa.pub" | ssh "$rUser@$rHost" -p $rPort "cat >> ~/.ssh/authorized_keys"
remoteUsers=($(ssh "$rUser@$rHost" -p $rPort 'echo '"$password"' | sudo -S ls /home/ && for localUser in $(ls /home | grep -v $USER); do sudo mkdir -p /home/$localUser/.ssh && sudo touch /home/$localUser/.ssh/authorized_keys && sudo chown "$localUser:$localUser" /home/$localUser/.ssh/authorized_keys && cat /home/$USER/.ssh/authorized_keys | sudo tee --append /home/$localUser/.ssh/authorized_keys > /dev/null; done'))

for remoteUser in "${remoteUsers[@]}"
do
    cat <<< "Host ${remoteUser}_${rHost}
            HostName $rHost
            Port $rPort
            User $remoteUser" >> /home/$USER/.ssh/config
done

```