2014-11-07 7 views
0

Я пытаюсь написать версию SSH из ICMP Ping, следующим образом:Лучший способ выбора времени?

TARGET_IP="" 
count=0 
time_start=0 
time_end=0 
time_taken=0 
TARGET_IP=$1 
while [ $count -lt 5 ] 
do 
    ((count=count+1)) 
    time_start=$(date +%s%N) 
    temp_target_key=$(ssh-keyscan $TARGET_IP) > /dev/null 2>&1 
    time_end=$(date +%s%N) 
    time_taken=$((($time_end - $time_start)/1000000)) 
    echo "Time taken=$time_taken ms." 
    target_key=$(echo $temp_target_key | awk '{print $3}') > /dev/null 2>&1 
    echo $temp_target_key > target_key.txt 
    ssh-keygen -l -f target_key.txt > /dev/null 2>&1 
    test=$? 
    if [ $test -ne 0 ] 
    then 
     echo "Device returned invalid RSA Public Key" 
     echo -e "\n" 
     echo -e "\n" 
    else 
     echo "Device responding correctly." 
     echo -e "\n" 
    fi 
done 
exit 

ICMP Ping сообщает звона времена 5мс по сравнению с этим сценарием 300 мс для того же самого устройства отчетности. Я понимаю, что время отклика на скрипт И программирование устройства/время отклика прошивки тоже, но я делаю это наилучшим образом, пожалуйста?
Спасибо

ответ

0

Я не вижу лучшего метода для имитации ssh ping. Тем не менее я попытался улучшить ваш сценарий в более чистом стиле bash:

#! /bin/bash 

if (($# != 1)); then 
    echo "Usage: ${0##*/} host" >&2 
    exit 1 
fi 
TARGET_IP="$1" 

target_key_file="target_key.txt" 

for ((count=0; count < 5; count++)) 
do 
    time_start=$(date +%s%N) 
    temp_target_key=$(ssh-keyscan "$TARGET_IP") > /dev/null 2>&1 
    time_end=$(date +%s%N) 
    time_taken=$(((time_end - time_start)/1000000)) 
    echo "Time taken=$time_taken ms." 

    read _ _ target_key _ <<< "$temp_target_key" 
    echo "$target_key" 

    echo "$temp_target_key" >| "$target_key_file" 
    if ! ssh-keygen -l -f "$target_key_file" > /dev/null 2>&1 
    then echo -e "Device returned invalid RSA Public Key.\n\n" 
    else echo -e "Device responding correctly.\n\n" 
    fi 
done 
+0

Спасибо. Мои сценарии в лучшем случае являются лучшими, поскольку это не моя основная работа. Приветствуем все улучшения/образование. –

Смежные вопросы