Home > OS >  linux how to measure ssh time until login to remote machine
linux how to measure ssh time until login to remote machine

Time:07-25

we want to measure the time that is needed until ssh successfully login to remote machine

is it possible to measure ssh time until login to remote machine?

note - all machine are RHEL machines

for example

lets say we want to verify how much time is needed to login machine_X from machine_Y

example from machine_Y

while machine_X is machine_[1..100]

ssh time until login to machine1 was 2 sec
ssh time until login to machine2 was 6 sec
ssh time until login to machine3 was 12 sec
ssh time until login to machine4 was 3 sec
.
.
.

CodePudding user response:

#!/bin/bash

# using bash variable SECONDS 
for srv in machine_{1..100}
     START=$SECONDS;        
     ssh "$srv" /usr/bin/true; 
     printf "ssh time until login to %s was %s sec\n" "$srv" "$((SECONDS-START))"; 
done

ssh time until login to machine1 was 1 sec
ssh time until login to machine2 was 0 sec
ssh time until login to machine3 was 0 sec
ssh time until login to ...      

CodePudding user response:

For each ssh print the date in epoch secs locally, then print the epoch secs remotely, then do a subtraction:

user='whatever'
for machine_X in machine_{1..100}; do
    { date  %s; ssh "${user}@${machine_X}" 'date  %s'; } |
    awk -v mc="$machine_X" '
        NR%2 { begSecs=$1; next }
        { printf "ssh time until login to %s was %d sec\n", mc, $1 - begSecs }
    '
done

We're doing the second call to date remotely so we only consider the time to log IN using ssh and not also the time to log OUT again as would be included if we did both calls to date locally.

We're using command grouping to call date and ssh to minimize overhead (and so added irrelevant time) during and between those calls. That makes the awk code very slightly more complicated than if we used other approaches that would output both timestamps on 1 line but that's a good tradeoff.

CodePudding user response:

If the time on the machines is accurate, and you have a recent version of GNU date, this will measure the time difference at nano-second level :

#!/bin/bash

for srv in machine_{1..100}; do
    start=$(date  %s.%N)
    end=$(ssh "$srv" date  %s.%N)
    echo "ssh time until login to $srv was $(bc <<< "$end - $start") seconds."
done
  • Related