automatic ssh connection without password

CC(ChenChih)
Chen-Chih’s Portfolio Page
6 min readAug 28, 2022

--

I have been using SSH connection often, but typing passwords just makes me exhausted especially when typing the wrong password. So I have found some solutions to remote SSH without a password.

Most often using SSH without a password will use the public and private keys method, but in this post, I am not going to use this method. I have written a post on SSH public and private tutorials in this post . In this article I will cover bleow topic:

Part 1 SSH without password sshpass

Part 2 Understand Gnome-terminal and how to use with sshpass

Part3 Automation bash script

Part 1: SSH without Password

I am going to introduce sshpass this package, but before it I want to mention another command to ignore or skip fingerprint option.

Skip fingerprint (StrictHostKeyChecking=no)

Command to do ssh connection: ssh username@host or IP , if you make ssh connection it will occur fingerprint, you can press yes, or ignore it. I am going to teach you how to skip the fingerprint option.

In order to skip displaying this option we can use this command it will not show : ssh -o StrictHostKeyChecking=no <username>@<host or IP address>

sshpass command

The sshpass the command is pretty powerful, it allows us to connect ssh without enter a password. So we need to install the package first.

Ubuntu/debian: sudo apt-get install sshpass

RHEL/Fedora/CentOS: yum install -y sshpass

There are two options:

-f , add your password in a file, much secure.

-p , add password in command

Let's give some examples:

  • Example for -p parameter:

syntax: sshpass -p <password> ssh username@<host or IP>

  • Example for -f parameter:

syntax: sshpass -f <filename> ssh username@<host or IP>

Please create a filename (sshpasswd). So you don’t have to show your password in the command line, which is much more secure.

combine StrictHostKeyCheckingand sshpass

We can combine both sshpassand StrictHostKeyChecking together using the below command

We can use both commands to remote ssh and we can also add some commands:

  • Ex1: remote ssh command

Syntax: sshpass -p <password> ssh <username>@<IP or host >

Example: sshpass -p ‘123456’ ssh -o StrictHostKeyChecking=no test@localhost

  • Ex2: remote ssh with other commands

Syntax: sshpass -p <password> ssh <username>@<IP or host > <command>

Example: sshpass -p ‘123456’ ssh -T -o StrictHostKeyChecking=no test@localhost ‘ls’

Example: sshpass -f sshpasswd ssh -T -o StrictHostKeyChecking=no test@localhost ‘ls’

SCP with sshpass

sshpass -p ‘123456’ scp root@host_ip:<source> <destionation>

Part 2: Gnome Terminal

You will be asking why am I introducing gnome-terminal. Well, this is a useful tool that will automate opening a new terminal or new tab terminal. This is a pretty useful tool when doing automation.

In this section,I will explain the basic command for gnome-terminal.

gnome-terminal command to open a new terminal

Under Ubuntu gnome-terminal default is already installed, so in order to open a new terminal just type in this command: gnome-terminal it will pop a new terminal or use gnome-terminal --tab to open a new tab terminal

gnome command with adding command

Syntax: gnome-terminal — bash -c “<command; exec bash>”

gnome-terminal — bash -c “ip a; exec bash”

Note: we need to add exec bash in the end, else will not pop new terminal

gnome command with ssh

gnome command with ssh commands:

gnome-terminal --bash -c ‘sshpass -p 123456 ssh -t ‘$hostIP’ “command;exec bash”’

Part3 Automation

Now we have a fundamental of using ssspass, and gnome-terminal these two commands, we now are able to use automation to implement them. I am using bash automation as an example.

Automation Different example

  • multiply command, and multiply variable
IFS=$'\n'
VARS=`ssh test@192.168.50.83 'echo 1 2; echo 3'`
for A_VAR in $VARS; do
#echo "Out: $A_VAR"
newvar=$A_VAR

done
echo "exit"
echo $newvar
  • using EOF multiply command
sshpass -p $password ssh -T -o StrictHostKeyChecking=no test@localhost -- bash <<'EOF'
var1=$(ls)
var2=$(pwd)
echo -e $var1'\n'$var2
echo "===========Exit CDU server=================="
EOF
  • EOF return variable
eval $(
ssh -T test@localhost 'bash -s' <<'EOF'
RESULTS1=$(pwd)
RESULTS2=$(ls)
#xtest="x *"
#echo "xtest='$xtest'"
echo "RESULTS1='$RESULTS1'"
echo "RESULTS2='$RESULTS2'"
EOF
)
echo "$RESULTS1"
echo "========="
echo "$RESULTS2"
  • the single command return variable
variable1=$(ssh -T user@hostname "command one")
echo $variable1

Iperf automation example

This is an example of running iperf3 server with 7 different IP addresses with a new terminal.

#!/bin/bashport=1000
ip_=11
for i in {1..7}
do
command="iperf3 -s -i1 -B 192.168.$ip_.1"
gnome-terminal --tab -- bash -c "sleep 1s; echo \"command: $command\"; $command; exec bash -i"
((ip_=ip_+11))
done

Iperf3 SSH automation example

I use localhost or 127.0.0.1 as an example, which is remote to my local PC.

resultFile='/home/test/Desktop/script/testresult.txt'# run iperf server and logged file
function iperf_serverLocal(){
echo "open iperf server ok"
gnome-terminal -- bash -c "iperf3 -s 2>&1 | tee iperf3.txt"
}
function iperfclient(){
iperfservercmd='iperf3 -c 127.0.0.1'
sshpass -p $password ssh -T -o StrictHostKeyChecking=no test@localhost "$iperfservercmd" &>/dev/null
echo "run iperf client ok"
}
#get iperf server log
function result_merge(){
sshpass -p $password ssh -T -o StrictHostKeyChecking=no test@localhost "cat /home/test/Desktop/script/iperf3.txt" >> $resultFile
}
function testiperf(){
echo -e "\n####SetUP Iperf####"
echo "####SetUP Iperf####" >> resultFile
iperf_serverLocal
echo "Run Iperf Server ok" >> $resultFile
#run iperf client
iperfclient
echo "Run Iperf Client ok" >> $resultFile
#read -p "please enter to continue ... "
#merge server's log
result_merge
}
###########################################
clear
echo "============RegressionTest-Report=============">$resultFile
echo "Date: "$(date) >>$resultFile
testiperf

Summary

So in this post, I have mentioned usingsshpass method to ssh connection without a password, but you can use another method public and private key, which I mention in another post. I also mention how to use gnome-terminal which is pretty useful.

I have been writing automation on it, looking at many tutorials, and wishing to share it with people. Hope this blog might help people if you have many ssh to connect and hate to enter long complicated passwords.

Lastly, If you're interested in more commands, I have listed many related references on both Iperf and gnome-terminal.

Reference

SSH and gnome information:

Iperf Related script

Bash Shell Script ping IP

--

--

CC(ChenChih)
Chen-Chih’s Portfolio Page

I self study technology, and likes to exchange knowledge with people. I try writing complex tech blog into simple and various ways for people to understand.