Python测试远程端口连接时间
问题
最近自己服务器访问别人的服务器,有时候会报超时错误,有时候又能够正常访问别人服务器。
思路
最开始猜测是网络不稳定造成的,但是自己没有收集什么时候超时,什么时候能正常访问别人服务器的日志,搞网络运维的同学根本不鸟我(其实,这活本来就是运维的事,有点小心塞,不过想起蜘蛛侠的名言)。
能力越大,责任就越大
写个python脚本,然后,在python脚本里面使用telnet去连接别人服务器对应的端口,然后,计算连接前后的时间长短。
解决
import os
import csv
import time
import argparse
import telnetlib
from datetime import datetime
# 测试远程服务端口连接耗时
# python3 windows_telnet.py 192.168.10.21 80
parser = argparse.ArgumentParser()
parser.add_argument("ip", type=str, help="ip")
parser.add_argument("port", type=str, help="port")
args = parser.parse_args()
timeFormat = "%Y-%m-%d %H:%M:%S.%f"
starTimeTitle = "开始连接时间"
endTimeTitle = "结束连接时间"
differenceTimeTitle = "连接总耗时"
while True:
starTime = datetime.now()
starTimeView = starTime.strftime(timeFormat)
print("开始连接:{0}".format(starTimeView))
tn = telnetlib.Telnet(args.ip, args.port)
endTime = datetime.now()
endTimeView = endTime.strftime(timeFormat)
print("连接完成:{0}".format(endTimeView))
tn.close()
print("连接结束")
differenceTime = endTime - starTime
print("连接消耗:{0}".format(differenceTime))
nowTime = datetime.now()
csvFileName = "{0}.csv".format(nowTime.strftime("%Y-%m-%d"))
if os.path.exists(csvFileName) is not True:
with open(csvFileName, "w", newline="") as csvfile:
fieldnames = [starTimeTitle, endTimeTitle, differenceTimeTitle]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
with open(csvFileName, "a", newline="") as csvfile:
fieldnames = [starTimeTitle, endTimeTitle, differenceTimeTitle]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({starTimeTitle : starTimeView, endTimeTitle : endTimeView, differenceTimeTitle : differenceTime})
time.sleep(0.2)
这里涉及到几个Python的知识点:
- 获取当前时间,计算时间差以及时间格式化
telnetlib的使用- 生成csv文件以及对文件读写
- 在
while True这个死循环里面需要避免cpu飙到100%问题,则需要在最后一行添加time.sleep(0.2)
接下来一个一个谈这些点:
Python3获取当前时间
from datetime import datetime starTime = datetime.now() endTime = datetime.now()
这样获取出来的时间,我们一般需要在进行格式化处理才能够展现给用户看。
Python3时间格式化
在上面的基础上,我们可以,这样做
timeFormat = "%Y-%m-%d %H:%M:%S.%f" starTimeView = starTime.strftime(timeFormat)
使用 strftime 方法处理,具体可以查看Python3文档的 date.strftime(format) 部分。
Python3计算时间差
differenceTime = endTime - starTime
对,就这样相减,就完事了。
telnetlib的使用
import telnetlib
tn = telnetlib.Telnet(")
csv文件创建
import os
import csv
csvFileName = "{0}.csv".format(nowTime.strftime("%Y-%m-%d"))
if os.path.exists(csvFileName) is not True:
with open(csvFileName, "w", newline="") as csvfile:
fieldnames = [starTimeTitle, endTimeTitle, differenceTimeTitle]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
这里是先判断文件是否存在,如果不存在,就创建一个csv文件,并且写好表头。
csv文件追加
with open(csvFileName, "a", newline="") as csvfile:
fieldnames = [starTimeTitle, endTimeTitle, differenceTimeTitle]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({starTimeTitle : starTimeView, endTimeTitle : endTimeView, differenceTime
死循环避免CPU飚高
循环里面最后添加一行:
import time time.sleep(0.2)
让线程休眠一段时间,这样就避免死循环占用cpu太高。
使用脚本
python3 windows_telnet.py 192.168.10.21 80
以后就可以通过这个脚本监测远程端口连接问题,并每天生成一个日志文件。
python学习交流群:125240963
原文:https://www.jianshu.com/p/e2e88cbb7572?utm_source=tuicool&utm_medium=referral
Python测试远程端口连接时间的更多相关文章
- python检测远程udp端口是否打开的代码
研发过程,把开发过程较好的代码收藏起来,如下的代码内容是关于python检测远程udp端口是否打开的代码,希望对各朋友有较大帮助. import socketimport threadingimpor ...
- Python测试进阶——(2)配置PyCharm远程调试环境
新建一个Python项目 配置Deployment,用于本地文件和远程文件的同步,在pycharm的菜单栏依次找到:Tools > Deployment > Configuration 点 ...
- ssh远程端口转发&&windows系统提权之信息收集&&网安工具分享(部分)
一.ssh远程端口转发 背景:当我们在渗透过程中,获取到内网的一台仅有内网IP的服务器后,我们可以通过ssh隧道,将内网某个主机的端口进行远程转发 1.网络拓扑图 假设获取的服务器为web服务器,we ...
- Python测试 ——开发工具库
Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. mechanize- Python中有状态的程序化Web浏 ...
- linux 检测远程端口是否打开
linux 检测远程端口是否打开 检测远程端口是否打开 常用telnet 110.101.101.101 80方式测试远程主机端口是否打开. 除此之外还可以使用: 方法1.nmap i ...
- 【译】SSH隧道:本地和远程端口转发
本文是:SSH Tunnel - Local and Remote Port Forwarding Explained With Examples 的译文 有两种方法可以创建SSH隧道,本地和远程端口 ...
- 测试Linux端口的连通性的四种方法
Linux系统有时候需要测试某个端口的连通性,用户可以参考如下方法来测试. 方法一.telnet法 telnet为用户提供了在本地计算机上完成远程主机工作的能力,因此可以通过telnet来测试端口 ...
- Appium环境的安装与配置,Python测试脚本测试
Appium自动化测试系列1 - Appium环境的安装与配置 发表于4个月前(2015-01-27 14:34) 阅读(803) | 评论(0) 0人收藏此文章, 我要收藏 赞0 寻找 会’偷懒 ...
- linux 测试远程主机端口
检测远程端口是否打开 常用telnet 110.101.101.101 80方式测试远程主机端口是否打开. 除此之外还可以使用: 方法1.nmap ip -p port 测试端口 nmap ip 显示 ...
随机推荐
- go-mysql: database/sql 接口适配
go-mysql已经支持golang database/sql接口,并通过https://github.com/bradfitz/go-sql-test测试用例. 现在go-mysql可以直接通过go ...
- Touch Handling in Cocos2D 3.x(四)
创建触摸生命周期 让我们改善我们的应用程序.如果玩家可以触摸屏幕并且拖放英雄到指定位置不是更好吗? 为了完成这个功能我们必须使用Cocos2d 3.0提供的所有的触摸事件: touchBegan:在用 ...
- 海量数据挖掘MMDS week2: Association Rules关联规则与频繁项集挖掘
http://blog.csdn.net/pipisorry/article/details/48894977 海量数据挖掘Mining Massive Datasets(MMDs) -Jure Le ...
- 简约才是王道? CardView 的使用
发现个好看的东东 CardView,他在support v7包中~~ 顾名思义就是卡片view,可以设置阴影,圆角,等等.. 样子是这样的: 或者你还可以放到listview里 是这样的: http: ...
- [加密]C#实现维吉尼亚加密与解密(解密前提为已知密匙)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 《java入门第一季》之面向对象面试题(代码块一网打尽)
<pre name="code" class="java">/* 代码块:在Java中,使用{}括起来的代码被称为代码块. 根据其位置和声明的不同, ...
- 【面试笔试算法】Program 5 : 推箱子 (网易游戏笔试题)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 推箱子是一款经典游戏.如图所示,灰色格子代表不能通过区域,蓝色方格是箱子,黑色圆形代表玩家,含有圆点的格子代表目标点. 规 ...
- 让App中加入LruCache缓存,轻松解决图片过多造成的OOM
上次有过电话面试中问到Android中的缓存策略,当时模糊不清的回答,现在好好理一下吧. Android中一般情况下采取的缓存策略是使用二级缓存,即内存缓存+硬盘缓存->LruCache+Dis ...
- adb shell后出现error解决方案
解决办法: 解决办法: 1.adb kill-server 2.adb start-server 3.adb remount 4.adb shell 一般情况下都可以在此启动adb相关
- iOS监听模式系列之NSNotificationCenter的简单使用
NSNotificationCenter 对于这个没必要多说,就是一个消息通知机制,类似广播.观察者只需要向消息中心注册感兴趣的东西,当有地方发出这个消息的时候,通知中心会发送给注册这个消息的对象.这 ...