shell脚本出现错误“/bin/bash^M:坏的解释器”的解决方法

原因 由于windows系统下换行符为 \r\n,linux下换行符为 \n,所以导致在windows下编写的文件会比linux下多回车符号 \r。 解决方法 只需要去掉多余的 \r 回车符 即可。操作办法可以用sed命令进行全局替换 sed 's/\r//' -i test.sh

2023年12月1日 · 1 分钟 · 14 字 · AIHugoBlog

Ubuntu20.04搭建v2ray服务vpn的教程

1.更新系统 apt-get update -y apt-get upgrade -y 2.一键安装v2ray bash <(wget -qO- -o- https://git.io/v2ray.sh) 3.开放防火墙端口 # 根据实际情况开放端口 iptables -I INPUT -p tcp --dport 891 -j ACCEPT 端口测试网站 https://ping.eu/port-chk 4.开启BBR echo net.core.default_qdisc=fq >> /etc/sysctl.conf echo net.ipv4.tcp_congestion_control=bbr >> /etc/sysctl.conf sysctl -p # 查看是否开启 lsmod | grep bbr

2023年12月1日 · 1 分钟 · 44 字 · AIHugoBlog

华为AP修改fat和fit模式

远程进入AP配置,默认是开启Stelnet的。 stelnet IP # 登录会创建账号密码 查看当前AP模式 display version 设置AP模式 ap-mode-switch fit AC中查看ap连接状态 dis ap all dis arp | include mac地址

2023年12月1日 · 1 分钟 · 20 字 · AIHugoBlog

Windows通过命令行共享文件夹

从命令行创建网络共享 语法如下: net share sharename=folderpath /grant:username,permissions sharename: 要创建的共享名称 username : 可以访问共享该文件夹的用户ID permission: 访问共享文件夹的权限:Read, Change or Full 例如,与域中的每个人共享文件夹E:\Documents并授予完全权限 net share Docs=C:\Users\Public\Downloads /grant:everyone,FULL 如果允许多个用户访问共享,则可以限制同时访问共享的用户数量。这将防止对系统的性能影响。以下命令将限制设置为10个用户。 net share Docs=E:\Documents /grant:everyone,FULL /users:10 与特定用户共享并仅授予读取权限的命令 net share Docs=E:\Documents /grant:username,READ 从命令行删除网络共享(即禁用文件夹共享) 删除网络共享语法如下 net share sharename /delete 例如,要删除上面创建的共享,命令将如下所示。 net share Docs /delete 另外,我们也可以使用文件夹的物理位置来禁用共享 net share E:\Docs /delete 列出在本地计算机上创建的共享 net share

2023年12月1日 · 1 分钟 · 51 字 · AIHugoBlog

HP惠普Gen8安装黑群晖

1.USB启动盘制作 找一个u盘至少8G大小。 打开github链接下载制作镜像,比如:tinycore-redpill.v0.9.4.3.img.gz https://github.com/pocopico/tinycore-redpill 解压后,使用Rufus制作工具,把镜像写入u盘 2.USB启动并制作引导 设置Bios,硬盘启动模式修改为ACHI模式,不要使用raid卡模式 把USB启动设置为第一启动,或者开机后按F11选择 3.进入USB系统后,在终端输入以下命令,遇到选择选“Y”,或者默认选项。 ./rploader.sh update ./rploader.sh fullupgrade ./rploader.sh serialgen DS3615xs ./rploader.sh identifyusb ./rploader.sh satamap ./rploader.sh build ds3615xs-7.1.1-42962 4.重启GEN8服务器 仍然从USB引导,选择第一项。 5.安装群晖系统 从官方下载:ds3615xs-7.1.1-42962安装包。 此时看不到启动的IP地址,可以从路由器中找到。 输入:IP:5000就可以安装了,安装完重启就可以设置了。

2023年3月16日 · 1 分钟 · 29 字 · AIHugoBlog

centos7源更换为国内163源

1.安装wget yum install wget 2.备份原来的源 cd /etc/yum.repos.d mv CentOS-Base.repo CentOS-Base.repo.bak 3.下载替换源 wget http://mirrors.163.com/.help/CentOS7-Base-163.repo mv CentOS7-Base-163.repo /etc/yum.repos.d/CentOS-Base.repo 4.清理缓存 yum clean all yum makecache 5.更新系统 yum update -y

2023年2月21日 · 1 分钟 · 26 字 · AIHugoBlog

Redis集群和集群加密设置

1.修改所有redis.conf配置,如端口6001-1006 vim redis.conf bind 127.0.0.1 #69行,注释掉bind项或不修改,默认监听所有网卡 protected-mode no #88行,修改,关闭保护模式 port 6001 #92行,修改,redis监听端口, daemonize yes #136行,开启守护进程,以独立进程启动 cluster-enabled yes #832行,取消注释,开启群集功能 cluster-config-file nodes-6001.conf #840行,取消注释,群集名称文件设置(启动redis后自动生成) cluster-node-timeout 15000 #846行,取消注释群集超时时间设置 appendonly yes #700行,修改,开启AOF持久化 2.启动每一个redis服务 redis-server redis.conf 3.启动集群(IP和端口根据实际情况填写) redis-cli --cluster create 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006 --cluster-replicas 1 4.给集群添加密码验证(密码必须相同) redis-cli -c -h 127.0.0.1 -p 6001 ...... config set masterauth passwd123 config set requirepass passwd123 config rewrite

2023年1月28日 · 1 分钟 · 60 字 · AIHugoBlog

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