码迷,mamicode.com
首页 > 编程语言 > 详细

IDEA+java通过SSH来进行分析日志,实现UI自动化动态验证码登录

时间:2019-12-03 10:27:07      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:自动   执行   tac   完整   方法   ash   日志   red   隧道   

在我写自动化脚本的时候经验药真实发送验证码才能往下进行UI自动化

思路:验证码会显示在哪些地方,手机短信?数据库存储?日志?

完整代码如下:

package guanyu.tools;

import com.jcraft.jsch.*;
//import jdk.nashorn.tools.Shell;
import java.awt.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Shell {
//远程主机的ip地址
private String ip;
//远程主机登录用户名
private String username;
//远程主机的登录密码
private String password;
//设置ssh连接的远程端口
public static final int DEFAULT_SSH_PORT = 22;
//保存输出内容的容器
private ArrayList<String> stdout;

/**
* 初始化登录信息
* @param ip ip地址
* @param username 用户名
* @param password ssh密码
*/
public Shell(final String ip, final String username, final String password) {
this.ip = ip;
this.username = username;
this.password = password;
stdout = new ArrayList<String>();
}
/**
* 执行shell命令
* @param command
* @return
*/
public int execute(final String command) {
int returnCode = 0;
JSch jsch = new JSch();
//调用 myuserinfo
MyUserInfo userInfo = new MyUserInfo();
try {
//创建session并且打开连接,因为创建session之后要主动打开连接
Session session = jsch.getSession(username, ip, DEFAULT_SSH_PORT);
session.setPassword(password);
session.setUserInfo(userInfo);
session.connect();

//打开通道,设置通道类型,和执行的命令
Channel channel = session.openChannel("exec");
ChannelExec channelExec = (ChannelExec)channel;
channelExec.setCommand(command);

channelExec.setInputStream(null);
BufferedReader input = new BufferedReader(new InputStreamReader
(channelExec.getInputStream()));

channelExec.connect();
System.out.println("shell执行命令 :" + command);

//接收远程服务器执行命令的结果
String line;
while ((line = input.readLine()) != null) {
stdout.add(line);
}
input.close();

// 得到returnCode
if (channelExec.isClosed()) {
returnCode = channelExec.getExitStatus();
}
// 关闭通道
channelExec.disconnect();
//关闭session
session.disconnect();

} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return returnCode;
}
/**
* get stdout
* @return
*/
public ArrayList<String> getStandardOutput() {
return stdout;
}
//编写一个main方法来进行测试一下
public static void main(final String [] args) {
Shell s = new Shell("ip地址", "用户名", "密码");
System.out.println("SSH隧道连接成功");
// System.out.print("验证码为:");
//进入/目录;打印日志文件 | 手机号为:13500774693 | awk过滤空格 | 取最后一行,这里根据你的实际情况来进行分析,这里的awk命令在我的博客里面会有更新,也可以自行百度
s.execute("cd /&&cat data/logs/guanyu.auth_web/external.log |grep 13500774693 | awk -F ‘[ ]+‘ ‘{print $11}‘| awk ‘END {print}‘");
ArrayList<String> stdout = s.getStandardOutput();
for (String str : stdout) {
System.out.println("for循环输出:"+str);
System.out.println("直接输出:"+stdout.get(0));
String yanzhengma = stdout.get(0);
String houjiwei = yanzhengma.substring(yanzhengma.length()-6);
//最后验证码取出来了,之后可以进行自动化了
System.out.println("输出验证码后几位:"+houjiwei);
}

}
}

IDEA+java通过SSH来进行分析日志,实现UI自动化动态验证码登录

标签:自动   执行   tac   完整   方法   ash   日志   red   隧道   

原文地址:https://www.cnblogs.com/zjwailxy/p/11975171.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!