51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1821|回复: 1
打印 上一主题 下一主题

[求助] loadrunner远程调用sh脚本

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2013-3-11 14:09:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 junior777 于 2013-3-19 14:09 编辑

package com.sun.junior;
import java.io.InputStream;  
import java.io.PrintStream;  
import org.apache.commons.net.telnet.TelnetClient;  

public class Shell  
{  
    private TelnetClient telnet = new TelnetClient();  
  
    private InputStream in;  
  
    private PrintStream out;  
  
    private char prompt = '$';// 普通用户结束  
  
    public Shell(String ip, int port, String user, String password)  
    {  
        try  
        {  
            telnet.connect(ip, port);  
            in = telnet.getInputStream();  
            out = new PrintStream(telnet.getOutputStream());  
            // 根据root用户设置结束符  
            this.prompt = user.equals("root") ? '#' : '$';  
            login(user, password);  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
  
    /**
     * 登录
     *
     * @param user
     * @param password
     */  
    public void login(String user, String password)  
    {  
//        readUntil("login:");  
        readUntil("login:");  
        write(user);  
        readUntil("Password:");  
        write(password);  
        readUntil(prompt + "");  
    }  
  
    /**
     * 读取分析结果
     *
     * @param pattern
     * @return
     */  
    public String readUntil(String pattern)  
    {  
        try  
        {  
            char lastChar = pattern.charAt(pattern.length() - 1);  
            StringBuffer sb = new StringBuffer();  
            char ch = (char)in.read();  
            while (true)  
            {  
                sb.append(ch);  
                if (ch == lastChar)  
                {  
                    if (sb.toString().endsWith(pattern))  
                    {  
                        return sb.toString();  
                    }  
                }  
                ch = (char)in.read();  
                System.out.print(ch);  
            }  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        return null;  
    }  
  
    /**
     * 写操作
     *
     * @param value
     */  
    public void write(String value)  
    {  
        try  
        {  
            out.println(value);  
            out.flush();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
  
    /**
     * 向目标发送命令字符串
     *
     * @param command
     * @return
     */  
    public String sendCommand(String command)  
    {  
        try  
        {  
            write(command);  
            return readUntil(prompt + "");  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
        return null;  
    }  
  
    /**
     * 关闭连接
     */  
    public void disconnect()  
    {  
        try  
        {  
            telnet.disconnect();  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  
    public static void main(String[] args) {  
        TelnetClient telnet = new TelnetClient();  
        try {
               
            String cmd="sh /home/test.sh";// sh脚本
            String ip="*.16.16.233";//远程服务器IP地址
            int port=23;//端口号
            String user="root";//远程服务器登录用户名
            String pass="root";//远程服务器登录密码

            Shell she =new Shell(ip, port, user,pass); //远程登录
  
            she.sendCommand("export LANG=en");//语言
            she.sendCommand(cmd);  //执行sh脚本

            System.out.println("********"+she.sendCommand(cmd));         
            she.disconnect();  //关闭
  
        }catch (Exception e) {  
            // TODO: handle exception  
        }  
  
    }  
}
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2013-3-11 15:19:55 | 只看该作者
太水了,这种东西也敢卖分?
回复 支持 反对

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-5-5 04:32 , Processed in 0.085302 second(s), 27 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表