网站首页 > linux / 正文
一、利用python批量测试网站是否可正常被访问
应用场景:当有大量网站需要检测是否能正常打开时,可以采用此方式;
import requests
#创建函数netcheck,传入参数为url,即需要被访问的网址
def netcheck(url):
try:
#利用requests.get访问url地址,timeout为超时参数,单位为秒;r.status_code为请求状态码
r = requests.get(url, timeout = 1)
status_code = r.status_code
return status_code
except Exception as e:
return e
if __name__ == "__main__":
#需要准备好都是url 的文件,每个url一行,打开一个txt文件,每次读入一行line
with open("urllist.txt") as f:
try:
for line in f:
#strip() 移除字符串开头和结尾的空格和换行符
status = netcheck(line.strip())
if status == 200:
print(line.strip() + ': successful')
#打开一个txt文件,以追加的方式(a),将请求状态码为200的url写入到一个文档中
with open('valid_feedlist.txt', 'a') as f1:
f1.write(line)
else:
#打开另一个txt文件,以追加的方式(a),将请求状态码为不是200的url写入到一个文档中
print(line.strip()+': unsuccessful')
with open('invalid_feedlist.txt', 'a') as f2:
f2.write(line)
except Exception as e:
print (e)
不足之处:对于网站中子模块是否能正常访问,并不能检测到;
他山之石:利用【Xenu】这个软件,可以对一个网站中所有的连接进行尝试访问,并给出结果。
二、利用python批量获得linux系统时间
应用场景:当有一批服务器时,需要查看各个服务器上的系统时间;
#-*- coding: utf-8 -*-
import paramiko
import threading
import time
def ssh2(ip,username,passwd,cmd):
try:
ssh = paramiko.SSHClient() #创建ssh对象
#允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,'22',username,passwd,timeout=5)
for m in cmd:
#exec_command 执行命令并获取命令结果.
#stdin为输入的命令,stdout为命令返回的结果,stderr为命令错误时返回的结果
stdin, stdout, stderr = ssh.exec_command(m)
#stdin.write("Y") #简单交互,输入 ‘Y’
out = stdout.readlines()
if (m=='date'):
#out是个list,转成str,"".join(out)
result1 = ip+ "=="+"".join(out)
#print (result1)
with open('timecheckresult.txt','a') as f1:
f1.write(result1)
else:
#获得当前时间,并转成时间戳
current_timestampe = int(time.time())
os_timestampe = int("".join(out))
#当前时间和系统时间差5秒,则输出服务器IP和服务器时间和当前时间
if (current_timestampe-os_timestampe) >5:
os_time = time.localtime(os_timestampe)
os_time_time = time.strftime("%Y--%m--%d %H:%M:%S", os_time)
current_time = time.localtime(current_timestampe)
current_time_time = time.strftime("%Y--%m--%d %H:%M:%S", current_time)
result2 = "current_time=="+current_time_time+"==="+ip+ "==server-time==" + os_time_time+'\n'
print(result2)
with open('timecheckresult_dif.txt','a') as f2:
f2.write(result2)
ssh.close()
except :
print (stdin.readlines())
print (stderr.readlines())
print ('%s\tError\n'%(ip))
if __name__=='__main__':
# cmd 你要执行的命令列表;在linux中date +%s返回时间戳,单位为秒
cmd = ['date','date +%s']#你要执行的命令列表
# 读取存储ip、用户名、密码的文件,循环执行
with open("ipandpass.txt") as f:
lines = f.readlines()
for i in range(len(lines)):
cols = lines[i].split()
ip = cols[0]
passwd = cols[1]
username='root'
#单线程
ssh2(ip,username,passwd,cmd)
#threads = [] #多线程
#a=threading.Thread(target=ssh2,args=(ip,username,passwd,cmd))
#a.start()
扩展应用:可以执行date命令,那也可以执行其他命令,比如内存,存储,性能等
Tags:linux获取系统时间
猜你喜欢
- 2024-11-26 解决Linux+Windows双系统时间不一致问题
- 2024-11-26 Linux的进程调度时机(Schedule函数何时调用)
- 2024-11-26 Linux time test---事件类型、时间操作、延时函数
- 2024-11-26 你可能不知道的Linux实时机制,dl_sched_class,死了都要Run
- 2024-11-26 精心整理了超详细的Linux入门笔记,零基础也能看懂,一学就会
- 2024-11-26 Linux系统Shell使用方法(二)——时间运算
- 2024-11-26 windows、linux双系统时间设置
- 2024-11-26 linux基础---修改系统时间
- 2024-11-26 Linux学习笔记-时间操作
- 2024-11-26 Linux 启动时间优化实战,2.41 秒启动应用