linux调用iperf3测试服务器之间的传输速度,并发送邮件

前言: 测试环境ubuntu18.04 在两台服务器之间安装iperf3 apt install iperf3 -y 放脚本的服务器上必须安装了python3,且安装了paramiko模块 apt install python -y apt install python3-pip -y pip3 install paramiko #代码: import smtplib,paramiko,os,sys from email.header import Header from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText hostname = "10.10.30.49" # 服务器的ip port = 33001 # 服务器的端口 username = "root" # 服务器的用户名 password = "%79@b!aqeTDtx" # 用户名对应的密码 # smtp服务器信息 smtp_server = 'mail.southbaytech.co' server_port = 25 # 发送方信息 sender = 'cherry_xu@southbaytech.co' mail_password = 'xxxxxx' # 收件人地址,列表可发给多人 receivers = ['cherry_xu@southbaytech.co'] if(os.system('ping -c 2 -W 1 %s' %hostname)==0): #ssh远程登录目标服务器 ssh = paramiko.SSHClient() policy = paramiko.AutoAddPolicy() ssh.set_missing_host_key_policy(policy) ssh.connect( hostname, #服务器的ip port, #服务器的端口 username, #服务器的用户名 password #用户名对应的密码 ) stdin,stdout,stderr = ssh.exec_command('iperf3 -s -i 2',get_pty=True) stdin.close() os.system('iperf3 -c %s -u -i 2 -b 200M | tee /tmp/result.txt' %hostname) file=open('/tmp/result.txt','r') text=file.readlines() file.close() # 实例化,先添加正文内容 msg = MIMEMultipart() msg.attach(MIMEText('服务器之间的速度测试', 'plain', 'utf-8')) # 添加附件1 att1 = MIMEApplication(open('/tmp/result.txt', 'rb').read(), 'utf-8') att1['Content-Disposition'] = 'attachment; filename="result.txt"' # filename随便起,是接收到的附件显示名称 # att1["Content-Type"] = 'application/octet-stream' Content-Type默认为application/octet-stream msg.attach(att1) # 邮件头信息 msg['From'] = Header(sender) # 发件人 msg['To'] = Header(', '.join(receivers)) # 发到哪里,参数为字符串 msg['Subject'] = Header(text[1]) # 邮件标题 # 发送邮件的操作 server=smtplib.SMTP(smtp_server,server_port) try: server.starttls() server.login(sender, mail_password) # 登录发信邮箱 server.sendmail(sender, receivers, msg.as_string()) # 发送邮件 print('邮件发送成功') except smtplib.SMTPException: print('邮件发送失败') ssh.close() else: ping_result = os.popen('ping -c 2 -W 1 %s' % hostname).read() # 实例化,先添加正文内容 msg = MIMEMultipart() msg.attach(MIMEText(ping_result, 'plain', 'utf-8')) # 邮件头信息 msg['From'] = Header(sender) # 发件人 msg['To'] = Header(', '.join(receivers)) # 发到哪里,参数为字符串 msg['Subject'] = Header('与目标服务器链接失败') # 邮件标题 # 发送邮件的操作 server=smtplib.SMTP(smtp_server,server_port) try: server.starttls() server.login(sender, mail_password) # 登录发信邮箱 server.sendmail(sender, receivers, msg.as_string()) # 发送邮件 print('邮件发送成功') except smtplib.SMTPException: print('邮件发送失败') #静默执行脚本 ...

2023年1月13日 · 2 分钟 · 269 字 · AIHugoBlog

python写的服务器性能监控脚本

代码示例 # -*- coding: UTF-8 -*- #!/usr/bin/env python import time,math,socket,struct import MySQLdb,psutil ###### 内存信息 ####### def getMemoryState(): phymem = psutil.virtual_memory() total = long(phymem.total) used = long(phymem.used) # percent = phymem.percent # mem_str = "Total:"+ str(total/1024/1024) + "MB;Used:" + str(used/1024/1024) +"MB;Percent:" + str(percent) +"%" mem_list =[total/1024/1024,used/1024/1024] return mem_list ###### CPU信息 ####### def getCpuState(interval=1): cpu_util = psutil.cpu_percent(interval) return int(cpu_util) ###### 硬盘信息 ####### def getDiskState(): disk_total = 0 disk_used = 0 disk_free = 0 disk_list = psutil.disk_partitions() # disk_info_list = [] # 硬盘所有信息包含挂载点 for disk_part in disk_list: disk_sub_total = long(math.ceil(psutil.disk_usage(disk_part.mountpoint).total)) # 获取分区总容量 disk_sub_used = long(math.ceil(psutil.disk_usage(disk_part.mountpoint).used)) # 获取分区已使用 disk_sub_free = long(math.ceil(psutil.disk_usage(disk_part.mountpoint).free)) # 获取分区剩余 disk_sub_percent = str((math.ceil(psutil.disk_usage(disk_part.mountpoint).percent))) disk_sub_info = "point:" + disk_part.mountpoint + ";total:"+ str(disk_sub_total/1024/1024/1024) + "GB;used:" + str(disk_sub_used/1024/1024/1024) + "GB;free:" + str(disk_sub_free/1024/1024/1024) + "GB;percent:" + disk_sub_percent + "%" disk_total = disk_total + disk_sub_total disk_used = disk_used + disk_sub_used disk_free = disk_free + disk_sub_free # disk_info_list.append(disk_sub_info) # disk_percent = str(round(float(disk_used)/float(disk_total)*100,2)) # disk_info = "Total:" + str(disk_total/1024/1024/1024) + "GB;Used:" + str(disk_used/1024/1024/1024) + "GB;Free:" + str(disk_free/1024/1024/1024) + "GB;Percent:" + disk_percent + "%" # disk_info_list.append(disk_info) disk_info = [disk_total/1024/1024/1024,disk_used/1024/1024/1024] return disk_info ###### 网络信息 ####### def getNetState(): key_info, net_in, net_out = getNetDate(get_key) net_list = [] received = 0 send = 0 for key in key_info: i = unicode(key, errors='ignore') if i !="lo" and ("Loopback" not in i): # j = i + ";received:" + str(net_in.get(key)) + "MB;send:" + str(net_out.get(key)) + "MB" # net_str = net_str + " " + j received = received + net_in.get(key) send = send + net_out.get(key) net_list.append(received) net_list.append(send) return net_list def getNetDate(func): key_info, old_recv, old_sent = func() # 上一秒收集的数据 time.sleep(1) key_info, now_recv, now_sent = func() # 当前所收集的数据 net_in = {} net_out = {} for key in key_info: net_in.setdefault(key, (now_recv.get(key) - old_recv.get(key)) / 1024 /1024) # 每秒接收速率 net_out.setdefault(key, (now_sent.get(key) - old_sent.get(key)) / 1024 /1024) # 每秒发送速率 return key_info, net_in, net_out def get_key(): # 获取网卡名称、收发字节 key_info = psutil.net_io_counters(pernic=True).keys() # 获取网卡名称 recv = {} sent = {} for key in key_info: recv.setdefault(key, psutil.net_io_counters(pernic=True).get(key).bytes_recv) # 各网卡接收的字节数 sent.setdefault(key, psutil.net_io_counters(pernic=True).get(key).bytes_sent) # 各网卡发送的字节数 return key_info, recv, sent ###### 获取主机名和IP地址 ####### def get_ip_host(): lis = [] hostname = socket.gethostname() # ip = socket.gethostbyname(hostname) lis.append(hostname) # lis.append(ip) info = psutil.net_if_addrs() for k,v in info.items(): for item in v: if item[0] == 2 and not item[1] == '127.0.0.1': lis.append(item[1]) return lis ###### 执行数据库操作 ####### def mysql_sh(ip_host,machine_info): ### 数据初始化 ip = socket.ntohl(struct.unpack("I", socket.inet_aton(str(ip_host[1])))[0]) # if ip_host[2]: # print("second ip is %s"% ip_host[2]) sql_search = "select * from machine where ip='" + str(ip) + "'" machine_info_search = "select id from machine where ip='" + str(ip) + "'" # 打开数据库连接 db = MySQLdb.connect("itopdb.southbaytech.co", "itop", "itop", "itop",3304,charset='utf8') # 使用cursor()方法获取操作游标 cursor = db.cursor() # 查询插入machine_info cursor.execute(sql_search) results = cursor.fetchall() if results: cursor.execute(machine_info_search) server_search = cursor.fetchall() for j in server_search: machine_id = j[0] # 设定machine_info插入 disk_total = machine_info['disk_state'][0] disk_used = machine_info['disk_state'][1] mem_total = machine_info['mem_state'][0] mem_used = machine_info['mem_state'][1] net_received = machine_info['net_state'][0] net_send = machine_info['net_state'][1] create_time = int(time.time()) machine_info_insert = 'insert into machine_info(machine_id,cpu_state,disk_total,disk_used,mem_total,mem_used,create_time,net_received,net_send) \ values (%d,%f,%d,%d,%d,%d,%d,%d,%d)' % \ (machine_id ,machine_info['cpu_state'],disk_total,disk_used,mem_total,mem_used,create_time,net_received,net_send) # 执行server_info插入 try: cursor.execute(machine_info_insert) db.commit() except: db.rollback() else: print("machine is not exist!") # 关闭数据库连接 db.close() ###### 主程序 ####### if __name__=='__main__': while 1 : try: machine_info = {} machine_info["cpu_state"] = getCpuState() machine_info["disk_state"] = getDiskState() machine_info["mem_state"] = getMemoryState() machine_info["net_state"] = getNetState() ip_host = get_ip_host() # 获取主机名和IP地址 mysql_sh(ip_host,machine_info) # 数据库插入更新 time.sleep(3) except KeyboardInterrupt: exit()

2023年1月13日 · 3 分钟 · 527 字 · AIHugoBlog

IPv4内网保留网段

A类地址 10.0.0.0/8 10.0.0.0 - 10.255.255.255 B类地址 172.16.0.0/12 172.16.0.0 - 172.31.255.255 C类地址 192.168.0.0/16 192.168.0.0 - 192.168.255.255

2023年1月13日 · 1 分钟 · 15 字 · AIHugoBlog

华为交换机清空所有配置

1. 首先通过串口线与交换机的console连接登陆该交换机 2.不需要进入sys,可以直接在现有的状态下输入命令,然后选择Y reset saved-configuration 3.此时配置已经清空,现在进行重启操作。输入reboot,并在接下来的第一个询问中选择“N”,第二个询问中选择“Y”等待交换机重启。

2023年1月13日 · 1 分钟 · 6 字 · AIHugoBlog

华为交换机开启3a认证(ssh)

创建一个账户为is,密码为xxx #创建用户 aaa local-user is password cipher xxx local-user is privilege level 15 #最高权限 local-user is service-type ssh #走ssh协议 quit #创建ssh user ssh user is authentication-type password #is用户的认证方式为密码认证 ssh user is service-type stelnet #stelnet实际上就是ssh #创建本地秘钥对 rsa local-key-pair create #启用ssh服务 stelnet server enable #配置vty界面支持的登录协议 user-interface vty 0 4 authentication-mode aaa #认证方式为3a protocol inbound ssh #允许ssh登录

2023年1月13日 · 1 分钟 · 54 字 · AIHugoBlog

华为设备配置console口参数

user-interface console 0 authentication-mode password set authentication password cipher idle-timeout 20 0 #设置空闲超时时间为20分钟,默认10分钟 display this #查看配置结果

2023年1月13日 · 1 分钟 · 16 字 · AIHugoBlog

H3C新建用户并设置系统最高权限

1.配置登录管理的用户名、认证级别、口令 local-user admin password simple satelit service-type telnet level 3 save 2.H3C提权 超级终端(基础密码system) super 3 sys local-user admin password simple system #设置密码为system level 3 service-type telnet save 3.修改H3C的默认密码 telnet 172.21.100.1 password: #输入原密码 super 3 system user-int vty 0 4 #改telnet密码 user-int console 0 #改console口密码 user-int aux 0 #改aux口密码 set aut pass cip abcde #设置密码

2023年1月13日 · 1 分钟 · 54 字 · AIHugoBlog

华为防火墙配置跨三层MAC识别

华为防火墙配置跨三层MAC识别 1.需求 FW作为企业的出口网关,内网用户通过三层交换机与FW相连,并通过FW访问Internet。FW需要以MAC地址为匹配条件配置安全策略、策略路由、带宽策略等来控制内网流量。 拓扑图 2. S5700交换机为例 开启snmp [Switch] snmp-agent [Switch] snmp-agent sys-info version v2c [Switch] snmp-agent community read Public@123 3. 防护墙配置 配置FW的接口IP地址。 [FW] interface GigabitEthernet 1/0/2 [FW-GigabitEthernet1/0/2] ip address 192.168.2.100 24 [FW-GigabitEthernet1/0/2] quit [FW] firewall zone trust [FW-zone-trust] add interface GigabitEthernet 1/0/2 配置Local到Trust区域的安全策略,允许防火墙向交换机发送SNMP报文。 [FW] security-policy [FW-policy-security] rule name policy_sec [FW-policy-security-rule-policy_sec] source-zone local [FW-policy-security-rule-policy_sec] destination-zone trust [FW-policy-security-rule-policy_sec] destination-address 192.168.2.110 32 [FW-policy-security-rule-policy_sec] action permit 配置跨三层MAC识别。 [FW] snmp-server arp-sync enable [FW] snmp-server target-host arp-sync address 192.168.2.110 community Public@123 v2c [FW] snmp-server arp-sync interval 5 timeout 3

2023年1月13日 · 1 分钟 · 83 字 · AIHugoBlog

华为防火墙和三层交换机对接上网配置

1.需求 公司拥有多个部门且位于不同网段,各部门均有访问Internet需求。现要求用户通过三层交换机和防火墙访问外部网络,且要求三层交换机作为用户的网关。 拓扑图 2. 配置交换机 配置连接用户的接口和对应的VLANIF接口。 <HUAWEI> system-view [HUAWEI] sysname Switch [Switch] vlan batch 2 3 [Switch] interface gigabitethernet 0/0/2 [Switch-GigabitEthernet0/0/2] port link-type access //配置接口接入类型为access [Switch-GigabitEthernet0/0/2] port default vlan 2 //配置接口加入VLAN 2 [Switch-GigabitEthernet0/0/2] quit [Switch] interface gigabitethernet 0/0/3 [Switch-GigabitEthernet0/0/3] port link-type access [Switch-GigabitEthernet0/0/3] port default vlan 3 [Switch-GigabitEthernet0/0/3] quit [Switch] interface vlanif 2 [Switch-Vlanif2] ip address 192.168.1.1 24 [Switch-Vlanif2] quit [Switch] interface vlanif 3 [Switch-Vlanif3] ip address 192.168.2.1 24 配置连接防火墙的接口和对应的VLANIF接口。 [Switch] vlan batch 100 [Switch] interface gigabitethernet 0/0/1 [Switch-GigabitEthernet0/0/1] port link-type access [Switch-GigabitEthernet0/0/1] port default vlan 100 [Switch-GigabitEthernet0/0/1] quit [Switch] interface vlanif 100 [Switch-Vlanif100] ip address 192.168.100.2 24 配置缺省路由。 [Switch] ip route-static 0.0.0.0 0.0.0.0 192.168.100.1 //缺省路由的下一跳是防火墙接口的IP地址192.168.100.1 配置DHCP服务器。 [Switch] dhcp enable [Switch] interface vlanif 2 [Switch-Vlanif2] dhcp select interface //DHCP使用接口地址池的方式为用户分配IP地址 [Switch-Vlanif2] dhcp server dns-list 114.114.114.114 223.5.5.5 //配置的DNS-List 114.114.114.114是公用的DNS服务器地址,是不区分运营商的。在实际应用中,请根据运营商分配的DNS进行配置 [Switch-Vlanif2] quit [Switch] interface vlanif 3 [Switch-Vlanif3] dhcp select interface [Switch-Vlanif3] dhcp server dns-list 114.114.114.114 223.5.5.5 [Switch-Vlanif3] quit 3.配置防火墙 配置连接交换机的接口对应的IP地址。 <USG6600> system-view [USG6600] interface gigabitethernet 1/0/1 [USG6600-GigabitEthernet1/0/1] ip address 192.168.100.1 255.255.255.0 配置连接公网的接口对应的IP地址。 [USG6600] interface gigabitethernet 1/0/2 [USG6600-GigabitEthernet1/0/2] ip address 1.1.1.2 255.255.255.0 //配置连接公网接口的IP地址和公网的IP地址在同一网段 配置缺省路由和回程路由。 [USG6600] ip route-static 0.0.0.0 0.0.0.0 1.1.1.1 //配置静态缺省路由的下一跳指向公网提供的IP地址1.1.1.1 [USG6600] ip route-static 192.168.0.0 255.255.0.0 192.168.100.2 //配置回程路由的下一跳就指向交换机上行接口的IP地址192.168.100.2 配置安全策略 [USG6600] firewall zone trust //配置trust域 [USG6600-zone-trust] add interface gigabitethernet 1/0/1 [USG6600-zone-trust] quit [USG6600] firewall zone untrust //配置untrust域 [USG6600-zone-untrust] add interface gigabitethernet 1/0/2 [USG6600-zone-untrust] quit 配置安全策略,允许域间互访。 [USG6600] security-policy [USG6600-policy-security] rule name policy1 [USG6600-policy-security-rule-policy1] source-zone trust [USG6600-policy-security-rule-policy1] destination-zone untrust [USG6600-policy-security-rule-policy1] source-address 192.168.0.0 mask 255.255.0.0 [USG6600-policy-security-rule-policy1] action permit [USG6600-policy-security-rule-policy1] quit [USG6600-policy-security] quit 配置PAT地址池,开启允许端口地址转换。 [USG6600] nat address-group addressgroup1 [USG6600-address-group-addressgroup1] mode pat [USG6600-address-group-addressgroup1] route enable [USG6600-address-group-addressgroup1] section 0 1.1.1.2 1.1.1.2 //转换的公网IP地址 [USG6600-address-group-addressgroup1] quit 配置源PAT策略,实现私网指定网段访问公网时自动进行源地址转换。 [USG6600] nat-policy [USG6600-policy-nat] rule name policy_nat1 [USG6600-policy-nat-rule-policy_nat1] source-zone trust [USG6600-policy-nat-rule-policy_nat1] destination-zone untrust [USG6600-policy-nat-rule-policy_nat1] source-address 192.168.0.0 mask 255.255.0.0 //允许进行PAT转换的源IP地址 [USG6600-policy-nat-rule-policy_nat1] action nat address-group addressgroup1 [USG6600-policy-nat-rule-policy_nat1] quit

2023年1月13日 · 2 分钟 · 279 字 · AIHugoBlog

chrome解决http自动跳转https问题

地址栏输入: chrome://net-internals/#hsts 找到底部Delete domain security policies一栏,输入想处理的域名,点击delete。

2023年1月13日 · 1 分钟 · 6 字 · AIHugoBlog