Board logo

标题: CentOS6使用Ping [打印本页]

作者: admin    时间: 2019-10-10 09:13     标题: CentOS6使用Ping

操作系统的版本信息: [root@centos6 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@centos6 ~]# [root@centos6 ~]# uname -r 2.6.32-696.el6.x86_64 Python的版本信息: [root@centos6 ~]# python3 --version Python 3.6.8 脚本内容:(以下方法无需安装第三方库) [root@centos6 ~]# cat xx.py #coding=utf-8 import os import datetime,time def func1(): dt = datetime.datetime.now() tt = dt.strftime('%Y%m%d_%H%M%S') Key_t ="\n\n检测时间:" + tt + "\n" Path_1 = "/root/1.txt" f_name = open(Path_1,'a') #写入(追加)具体检测时间到文件 f_name.write(Key_t) f_name.close() #调用Linux命令 os.system("ping 47.75.39.177 -c 10 >> /root/1.txt") def func_main(func,second): while True: #进程会无限次地自动执行下去 func() time.sleep(second) if __name__ == '__main__': func_main(func1,60) #每隔60秒后执行一次函数func1() 设置脚本权限: [root@centos6 ~]# chmod a+x xx.py 脚本运行中的效果:( 程序会无限次地执行下去;组合键Ctrl+c可结束程序的执行 ) 图片1.png 文件/root/1.txt自动记录的数据:(以下只是最后三次的结果) 检测时间:20190208_121024 PING 47.75.39.177 (47.75.39.177) 56(84) bytes of data. 64 bytes from 47.75.39.177: icmp_seq=1 ttl=128 time=71.0 ms 64 bytes from 47.75.39.177: icmp_seq=2 ttl=128 time=65.7 ms 64 bytes from 47.75.39.177: icmp_seq=3 ttl=128 time=66.2 ms 64 bytes from 47.75.39.177: icmp_seq=4 ttl=128 time=106 ms 64 bytes from 47.75.39.177: icmp_seq=5 ttl=128 time=107 ms 64 bytes from 47.75.39.177: icmp_seq=6 ttl=128 time=202 ms 64 bytes from 47.75.39.177: icmp_seq=7 ttl=128 time=53.7 ms 64 bytes from 47.75.39.177: icmp_seq=8 ttl=128 time=92.2 ms 64 bytes from 47.75.39.177: icmp_seq=9 ttl=128 time=78.4 ms 64 bytes from 47.75.39.177: icmp_seq=10 ttl=128 time=84.8 ms --- 47.75.39.177 ping statistics --- 10 packets transmitted, 10 received, 0% packet loss, time 9115ms rtt min/avg/max/mdev = 53.764/92.819/202.683/40.194 ms 检测时间:20190208_121133 PING 47.75.39.177 (47.75.39.177) 56(84) bytes of data. 64 bytes from 47.75.39.177: icmp_seq=1 ttl=128 time=80.1 ms 64 bytes from 47.75.39.177: icmp_seq=2 ttl=128 time=52.5 ms 64 bytes from 47.75.39.177: icmp_seq=3 ttl=128 time=78.5 ms 64 bytes from 47.75.39.177: icmp_seq=4 ttl=128 time=63.9 ms 64 bytes from 47.75.39.177: icmp_seq=5 ttl=128 time=67.9 ms 64 bytes from 47.75.39.177: icmp_seq=6 ttl=128 time=57.3 ms 64 bytes from 47.75.39.177: icmp_seq=7 ttl=128 time=74.3 ms 64 bytes from 47.75.39.177: icmp_seq=8 ttl=128 time=54.3 ms 64 bytes from 47.75.39.177: icmp_seq=9 ttl=128 time=73.5 ms 64 bytes from 47.75.39.177: icmp_seq=10 ttl=128 time=77.9 ms --- 47.75.39.177 ping statistics --- 10 packets transmitted, 10 received, 0% packet loss, time 9121ms rtt min/avg/max/mdev = 52.576/68.084/80.128/9.926 ms 检测时间:20190208_121242 PING 47.75.39.177 (47.75.39.177) 56(84) bytes of data. 64 bytes from 47.75.39.177: icmp_seq=1 ttl=128 time=87.0 ms 64 bytes from 47.75.39.177: icmp_seq=2 ttl=128 time=74.6 ms 64 bytes from 47.75.39.177: icmp_seq=3 ttl=128 time=71.5 ms 64 bytes from 47.75.39.177: icmp_seq=4 ttl=128 time=69.6 ms 64 bytes from 47.75.39.177: icmp_seq=5 ttl=128 time=57.1 ms 64 bytes from 47.75.39.177: icmp_seq=6 ttl=128 time=105 ms 64 bytes from 47.75.39.177: icmp_seq=7 ttl=128 time=75.0 ms 64 bytes from 47.75.39.177: icmp_seq=8 ttl=128 time=90.5 ms 64 bytes from 47.75.39.177: icmp_seq=9 ttl=128 time=67.3 ms 64 bytes from 47.75.39.177: icmp_seq=10 ttl=128 time=82.4 ms --- 47.75.39.177 ping statistics --- 10 packets transmitted, 10 received, 0% packet loss, time 9115ms rtt min/avg/max/mdev = 57.143/78.125/105.789/13.075 ms ############ 把脚本放在后台运行: [root@centos6 ~]# nohup python3 /root/xx.py & [1] 17068 [root@centos6 ~]# nohup: 忽略输入并把输出追加到"nohup.out" [root@centos6 ~]# 查看脚本“xx.py”的进程号: [root@centos6 ~]# ps -ef |grep xx.py |grep -v grep root 17068 16478 0 12:19 pts/2 00:00:00 python3 /root/xx.py [root@centos6 ~]# 结束脚本“xx.py”的进程:(等于结束脚本的运行) [root@centos6 ~]# kill -9 17068 [root@centos6 ~]# [1]+ 已杀死 nohup python3 /root/xx.py [root@centos6 ~]# ############ 开机自动在后台运行脚本: [root@centos6 ~]# echo 'nohup python3 /root/xx.py &' >> /etc/rc.local [root@centos6 ~]# [root@centos6 ~]# tail -1 !$ tail -1 /etc/rc.local nohup python3 /root/xx.py & [root@centos6 ~]# 相关文章: Timer/sleep 文件操作(创建、读取、写入、追加) CentOS8使用多线程+Ping Windows使用Ping rsync远程同步目录树

图片附件: 图片1.png (2022-2-24 12:00, 2.31 KB) / 下载次数 90
http://blog.zhuohua.store/attachment.php?aid=19131&k=759ba8dd789ca0ea364bbb4631d4ba42&t=1714055067&sid=ZiZuVY






欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2