Python checks whether the server is normal

Source: Internet
Author: User
This article describes how to use python to check whether the server is normal and the port of the program is normal. For more information about how to use python to check whether the server can be pinged, whether the program runs properly (check whether the corresponding port is normal)

Previously, shell scripts were written as follows:

The code is as follows:


PINGRET = $ (ping www.baidu.com-c 2 | grep "icmp _"); if [-z $ PINGRET]; then echo "ping fail"; else echo "ping OK"; fi


Or

The code is as follows:


Ping-c 2 www.baidu.com | grep "icmp _" & echo 'ping OK '| echo 'ping fail'


Sample code:

The code is as follows:


#! /Usr/bin/python
# Encoding = UTF-8
# Filename: net_is_normal.py
Import OS
Import socket
Import subprocess


# Determine whether the network is normal
Server = 'www .baidu.com'
# Check whether the server can be pinged. when the program runs, the command running information is displayed in the standard output.
Def pingServer (server ):
Result = OS. system ('ping' + server + '-c 2 ')
If result:
Print 'server % s ping fail '% server
Else:
Print 'server % s ping OK '% server
Print result

# Locate the program output to/dev/null. Otherwise, the running information of the command will be displayed in the standard output when the program is running.
Def pingServerCall (server ):
Fnull = open (OS. devnull, 'w ')
Result = subprocess. call ('ping' + server + '-c 2', shell = True, stdout = fnull, stderr = fnull)
If result:
Print 'server % s ping fail '% server
Else:
Print 'server % s ping OK '% server
Fnull. close ()

# It can be used to check whether the program is normal. if it detects whether redis is normal, it checks whether Port 6379 of redis is normal.
# Check whether ssh is normal, that is, check whether ssh port 22 is normal
Def check_aliveness (ip, port ):
Sk = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
Sk. settimeout (1)
Try:
Sk. connect (ip, port ))
Print 'server % s % d service is OK! '% (Ip, port)
Return True
Failed T Exception:
Print 'server % s % d service is not OK! '% (Ip, port)
Return False
Finally:
Sk. close ()
Return False

If _ name __= = '_ main __':
PingServerCall (server)
PingServer (server)
Check_aliveness ('192. 168.230.128 ', 192)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.