解决思路两种:
1.直接linux catalina.out读取:一分每10s轮询出现新的直接读取返回<br>
2.缓存注入cookie,先手动登录获取session 然后通过注入user,pwd 进cookie
import paramiko import os from time import sleep class SSH(object): def __init__(self,host,port,user,pwd): self.host=host self.port=port self.user=user self.pwd=pwd """此方法是统计在登陆之前获取的服务日志code的数量""" def before_count(self,logfile_absolute_path): logfile_parentdir,logfile_name=os.path.split(logfile_absolute_path) shell="cd {}; cat {}|grep 动态验证码为".format(logfile_parentdir,logfile_name) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.host,self.port,self.user,self.pwd) self.ssh=ssh stdin, stdout, stderr = self.ssh.exec_command(shell) res,err = stdout.readlines(),stderr.readlines() result = res if res else err return len(result) # """其实这样写不是最好办法,解决多行根本是paramiko执行机制, # 每次执行完ssh.exec_command()函数会自动回到session初始化root路径,多行建议以;分号隔开""" def ssh_connect(self,logfile_absolute_path,beforecode_number): target_code="" for i in range(6): logfile_parentdir,logfile_name=os.path.split(logfile_absolute_path) shell="cd {}; cat {}|grep 动态验证码为".format(logfile_parentdir,logfile_name) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(self.host,self.port,self.user,self.pwd) self.ssh=ssh stdin, stdout, stderr = self.ssh.exec_command(shell) res,err = stdout.readlines(),stderr.readlines() result = res if res else err if len(result)> beforecode_number: var=result[-1].strip() a=var.split("-")[-1] target_code=a.split(r":")[-1].strip() break else: sleep(10) self.ssh.close() return target_code
cookie注入:见我csdn直接可以跑代码自己观察效果邮箱手动登录是有滑块拦截的
https://blog.csdn.net/chen498858336/article/details/83795911