انجمن‌های فارسی اوبونتو

کمک و پشتیبانی => برنامه‌سازی => نویسنده: AmirHossein WP در 13 فروردین 1396، 04:27 ق‌ظ

عنوان: بررسی اتصال به اینترنت bash
ارسال شده توسط: AmirHossein WP در 13 فروردین 1396، 04:27 ق‌ظ
سلام دوستان
ساعت 04:25 :P
چجوری تو bash اتصال به اینترنت رو بررسی کنم؟
مثلا بگم اگه وصل بود فلان کارو بکن؟  :oops:
تشکر از معین عزیز
عنوان: پاسخ : بررسی اتصال به اینترنت bash
ارسال شده توسط: AmirHossein WP در 13 فروردین 1396، 04:30 ق‌ظ
یکی از عزیزان جواب رو رسوند
نقل‌قول

('connected=$(ping -c1 8.8.8.8 2> /dev/null | grep '1 received
if [ "$connected" ]; then
    ...
fi
عنوان: پاسخ : بررسی اتصال به اینترنت bash
ارسال شده توسط: فاروق در 13 فروردین 1396، 12:05 ب‌ظ
میتونید از exit code خود ping استفاده بکنید.
عنوان: پاسخ : بررسی اتصال به اینترنت bash
ارسال شده توسط: فاروق در 13 فروردین 1396، 06:29 ب‌ظ
نقل قول از man ping
If  ping  does not receive any reply packets at all it will exit with code
       1. If a packet count and deadline are both specified, and fewer than count
       packets  are  received  by the time the deadline has arrived, it will also
       exit with code 1.  On other error it exits with code 2. Otherwise it exits
       with  code 0. This makes it possible to use the exit code to see if a host
       is alive or not.

       This program is intended for use in network testing, measurement and  man‐
       agement.   Because  of the load it can impose on the network, it is unwise
       to use ping during normal operations or from automated scripts.
unwise یعنی «غیر عاقلانه»