【测试工具】通过Jsch访问服务器,发送指定linux命令的工具


前言

在平时测试的过程中,会遇到一些特殊的测试场景,需要进入指定服务发送对应消息,触发某一测试场景。为了提升测试效率,于是便开发了一个对应的测试工具,方便后续这对一模块的测试。


一、前端代码

<style lang="postcss" scoped>
.home-container{

}
</style>
<template>
<section class="home-container">
    <MasterPage title="测试工具">
        <div slot="title-icon">
            <Icon type="ios-game-controller-b" /> 
        </div>
         <div slot="searchContent" class="search-content-slot">
          <Form :model="formItem" :label-width="100" >
              <Row>
                <Col span="5">
                    <FormItem label="功能:" style="font-weight:bold">
                    <Input v-model.trim="form1.collection.command" type="textarea" :rows="4" placeholder="请输入linux指令,保证命令准确性" style="width:800px;"></Input>
                    </FormItem>
                </Col>
              </Row>
              <Row>
                <Col span="2">
                  <!-- <FormItem label=""> -->
                      <Button type="primary" icon="ios-search" @click="collection">提交</Button>
                  <!-- </FormItem>    -->
                 </Col>
                 <Col span="2">
                    <Input v-model.trim="form1.message" type="textarea" :rows="2" placeholder="输出结果,请耐心等待结果..." style="width:1000px;"></Input>
                 </Col>
              </Row>
          </Form>
        </div>
    </MasterPage>
</section>
</template>
<script>
import MasterPage from '@/components/Master'

export default {
    components:{
        MasterPage
    },
    data(){
      return{
        form1:{
            collection:{command:""},
            message:""
        },
      }
    },
    created(){
    },

    methods:{
      collection:function() {
      this.$axios.post("/v1/collection/",this.form1.collection).then((res)=>
      {
        console.log(res);
        this.form1.message=JSON.stringify(res.data);
      })
    }
  }
    
}
</script>


界面展示
在这里插入图片描述

二、Jsch工具后端代码

后端主要写一个接口给前端调用

1.controller层

@RestController
@RequestMapping("v1")
public class CollectionController {

    @Autowired
    private JschUtil jschUtil;

    /**
     * post请求
     * @return
     */
    @RequestMapping(value = "/collection",method = RequestMethod.POST)
    public String enterCollection(@RequestBody CollectionEntity collectionEntity){
        String command=collectionEntity.getCommand();
        return jschUtil.createSession(command);
    }

}

2.实体类

import lombok.Data;

@Data
public class CollectionEntity {

    private String command;
}

3.工具类

import com.jcraft.jsch.*;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: simple_tools
 * @description: 连接远程ES通道
 * @author: Mr.chen
 * @create: 2020-04-27 13:52
 **/
@Component
public class JschUtil {

    /**
     * 创建与远程服务的连接
     */
    public String createSession(String command){
        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession("服务器用户名", "服务器ip", 22);
            session.setPassword("服务器密码";
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            List<String> result=exec(session,command);
            StringBuilder s= new StringBuilder();
            if(!ObjectUtils.isEmpty(result)){
                for (int i = 0; i < result.size(); i++) {
                    s.append(result.get(i));
                }
            }
            //返回执行指定命令后的结果
            return s.toString();
        } catch (JSchException e) {
            System.out.println(e);
            return e.toString();
        }
    }

    /**
     * 执行命令
     * @param command 命令
     * @return list
     * @throws JSchException err
     */
    public static List<String> exec(Session session, String command) throws JSchException {
        List<String> resultLines = new ArrayList<>();
        ChannelExec channel = null;
        try{
            channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(command);
            channel.setInputStream(null);
            channel.setErrStream(System.err);
            channel.connect(10000);
            InputStream input = channel.getInputStream();
            try {
                BufferedReader inputReader = new BufferedReader(new InputStreamReader(input));
                String inputLine = null;
                System.out.println(inputReader);
                while((inputLine = inputReader.readLine()) != null) {
                    resultLines.add(inputLine);
                }
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (Exception e) {
                        // todo...
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
        } finally {
            if (channel != null) {
                try {
                    channel.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return resultLines;
    }
} 

总结

最终实现效果,如图
在这里插入图片描述
这样一个简单好用的测试工具就写完了,喜欢的同学给个点赞👍吧,谢谢

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jacky学习笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值