python端口检测和ping检测脚本

简介:

线上用lvs做负载均衡,最近发现几台机器有些时间段经常被踢出,写了个python脚本用来做简单的端口和ping检测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import  os,socket,time
import  threading
def  check_port(host,port):
     =  None
     for  res  in  socket.getaddrinfo(host, port, socket.AF_UNSPEC,socket.SOCK_STREAM):
         af, socktype, proto, canonname, sa  =  res
         try :
             =  socket.socket(af, socktype, proto)
         except  socket.error, msg:
             =  None
             print  str (msg)
             continue
         try :
             s.settimeout( 2 )
             s.connect(sa)
         except  socket.error, msg:
             print  str (msg)
             s.close()
             =  None
             continue
         break
     if  is  None :
         return  0
     s.close()
     return  1
def  ping_server(ip):
     cmd  =  """
     ping  -c 50 %s|egrep "received|mdev" | sed "s,^,%s : ," >> %s/%s_ping.log
     """  %  (ip,time.ctime(time.time()),logs_dir,ip)
     print  cmd
     os.popen(cmd)
def  check_network():
     check_ip_list  =  get_hosts()
     thread_pool  =  []
     for  in  range ( len (check_ip_list)):
         ip  =  check_ip_list[i]
         print  ip
         th  =  threading.Thread(target = ping_server,args = (ip,) ) ;
         thread_pool.append(th)
     for  in  range ( len (check_ip_list)):
         thread_pool[i].deamon  =  True
         thread_pool[i].start()
def  get_hosts():
         hosts_list  =  []
         =  open ( '/etc/hosts' )
         for  in  f.readlines():
                 if  'hadoop-'  in  i:
                         host  =  i.strip().split()[ 0 ]
                         hosts_list.append(host)
                 else :
                         continue
         f.close()
         print  hosts_list
         return  hosts_list
def  get_hosts_ports():
         hosts_ports_list  =  []
         =  open ( '/etc/hosts' )
         for  in  f.readlines():
                 if  'hadoop-'  in  i:
                         host  =  i.strip().split()[ 0 ]
                         hosts_ports_list.append(host + ':10050' )
                 else :
                         continue
         f.close()
         print  hosts_ports_list
         return  hosts_ports_list
def  check_server_port():
     port_file  =  open ( "%s/check_port.log" % (logs_dir), 'a' )
     dt  =  time.ctime(time.time())
     checklist  =  get_hosts_ports()
     for  checkitem  in  checklist:
         host, port  =  checkitem.split( ':' )
         if  check_port(host, port):
             port_file.write( '%s connect to %s  success\n' % (dt,checkitem))
         else :
             port_file.write( '%s connect to %s  fail' % (dt,checkitem))
def  check_health():
     while  True :
         check_server_port()
         check_network()
         time.sleep( 60 )
if  __name__  = =  '__main__' :
     logs_dir  =  '/apps/logs/ping'
     if  not  (os.path.exists(logs_dir)):
         os.makedirs(logs_dir)
     check_health()



本文转自菜菜光 51CTO博客,原文链接:http://blog.51cto.com/caiguangguang/1352307,如需转载请自行联系原作者
相关文章
|
3月前
|
Python
自动化微信朋友圈:Python脚本实现自动发布动态
本文介绍如何使用Python脚本自动化发布微信朋友圈动态,节省手动输入的时间。主要依赖`pyautogui`、`time`、`pyperclip`等库,通过模拟鼠标和键盘操作实现自动发布。代码涵盖打开微信、定位朋友圈、准备输入框、模拟打字等功能。虽然该方法能提高效率,但需注意可能违反微信使用条款,存在风险。定期更新脚本以适应微信界面变化也很重要。
291 61
|
11天前
|
SQL 关系型数据库 数据库连接
|
1月前
|
监控 Java 计算机视觉
Python图像处理中的内存泄漏问题:原因、检测与解决方案
在Python图像处理中,内存泄漏是常见问题,尤其在处理大图像时。本文探讨了内存泄漏的原因(如大图像数据、循环引用、外部库使用等),并介绍了检测工具(如memory_profiler、objgraph、tracemalloc)和解决方法(如显式释放资源、避免循环引用、选择良好内存管理的库)。通过具体代码示例,帮助开发者有效应对内存泄漏挑战。
49 1
|
2月前
|
监控 网络安全 开发者
Python中的Paramiko与FTP文件夹及文件检测技巧
通过使用 Paramiko 和 FTP 库,开发者可以方便地检测远程服务器上的文件和文件夹是否存在。Paramiko 提供了通过 SSH 协议进行远程文件管理的能力,而 `ftplib` 则提供了通过 FTP 协议进行文件传输和管理的功能。通过理解和应用这些工具,您可以更加高效地管理和监控远程服务器上的文件系统。
65 20
|
2月前
|
安全 Linux 网络安全
利用Python脚本自动备份网络设备配置
通过本文的介绍,我们了解了如何利用Python脚本自动备份网络设备配置。该脚本使用 `paramiko`库通过SSH连接到设备,获取并保存配置文件。通过定时任务调度,可以实现定期自动备份,确保网络设备配置的安全和可用。希望这些内容能够帮助你在实际工作中实现网络设备的自动化备份。
87 14
|
2月前
|
XML 机器学习/深度学习 人工智能
使用 OpenCV 和 Python 轻松实现人脸检测
本文介绍如何使用OpenCV和Python实现人脸检测。首先,确保安装了OpenCV库并加载预训练的Haar特征模型。接着,通过读取图像或视频帧,将其转换为灰度图并使用`detectMultiScale`方法进行人脸检测。检测到的人脸用矩形框标出并显示。优化方法包括调整参数、多尺度检测及使用更先进模型。人脸检测是计算机视觉的基础技术,具有广泛应用前景。
90 10
|
3月前
|
数据采集 存储 监控
21个Python脚本自动执行日常任务(2)
21个Python脚本自动执行日常任务(2)
159 7
21个Python脚本自动执行日常任务(2)
|
3月前
|
数据挖掘 vr&ar C++
让UE自动运行Python脚本:实现与实例解析
本文介绍如何配置Unreal Engine(UE)以自动运行Python脚本,提高开发效率。通过安装Python、配置UE环境及使用第三方插件,实现Python与UE的集成。结合蓝图和C++示例,展示自动化任务处理、关卡生成及数据分析等应用场景。
271 5
|
3月前
|
网络协议 API
检测指定TCP端口开放状态免费API接口教程
此API用于检测指定TCP端口是否开放,支持POST/GET请求。需提供用户ID、KEY、目标主机,可选指定端口(默认80)和地区(默认国内)。返回状态码、信息提示、检测主机、端口及状态(开放或关闭)。示例中ID和KEY为公共测试用,建议使用个人ID和KEY以享受更高调用频率。
96 14
|
26天前
|
SQL 关系型数据库 MySQL
云服务器常用端口作用
了解云服务器常用端口的作用有助于高效管理资源、快速定位问题及更好地使用云服务。常见端口包括:21(FTP,文件传输)、22(SSH,远程连接Linux)、25(SMTP,发送邮件)、80(HTTP,网页服务)、110/143(POP3/IMAP,接收邮件)、443(HTTPS,加密网页)、1433(SQL Server)、3306(MySQL)、3389(RDP,远程访问Windows桌面)和8080(代理服务)。
47 2