Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6063033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:07:10+00:00 2026-05-23T09:07:10+00:00

For some reason,I need to connect to a firewall(based on Linux) and add some

  • 0

For some reason,I need to connect to a firewall(based on Linux) and add some rules with Java.

After searching with google for a while, I found that jsch is my best choice.But when I

use it to execute a command,”show hostname” for example, error is returned.If I

execute commands like “ls -l” and “whoami”,the results are ok.That’s to say,I can only

connect to the Bash Shell! My question is how can I connect to the firewall shell?

My test code as follows:

import java.io.InputStream;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;

public class SSHDemo {
    public static void main(String[] args) {
        SSHDemo t = new SSHDemo();
        try {
            t.go();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void go() throws Exception {
        String host = "10.4.44.192";
        String user = "root";
        String password = "root";
        int port = 22;
//      int tunnelLocalPort = 9080;
//      String tunnelRemoteHost = "10.4.44.192";
//      int tunnelRemotePort = 8000;
        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        localUserInfo lui = new localUserInfo();
        session.setUserInfo(lui);
        session.connect();

        ChannelExec channel = (ChannelExec)session.openChannel("exec");
        channel.setInputStream(null);

            //Pay attension to this line
        channel.setCommand("show hostname");
        channel.setErrStream(System.err);
        channel.connect();
        InputStream in=channel.getInputStream();
        int i=0;
        byte[] tmp=new byte[1024*1024];
        while ((i = in.read(tmp, 0, tmp.length)) != -1) {
            System.out.write(tmp, 0, i);
        }
        in.close();


        channel.disconnect();
        session.disconnect();
        // session.setPortForwardingL(tunnelLocalPort,tunnelRemoteHost,tunnelRemotePort);
        System.out.println("Connected");

    }


    class localUserInfo implements UserInfo {
        String passwd;

        public String getPassword() {
            return passwd;
        }

        public boolean promptYesNo(String str) {
            return true;
        }

        public String getPassphrase() {
            return null;
        }

        public boolean promptPassphrase(String message) {
            return true;
        }

        public boolean promptPassword(String message) {
            return true;
        }

        public void showMessage(String message) {
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:07:10+00:00Added an answer on May 23, 2026 at 9:07 am

    Try using ChannelShell by opening shell rather then exec.

    Following is small code sample for log in and executing file fro remote java class.

    private Expect4j SSH(String hostname, String username,String password, int port) throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession(username, hostname, port);
        if (password != null) {         
            session.setPassword(password);
        }
        Hashtable<String,String> config = new Hashtable<String,String>();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect(60000);
        channel = (ChannelShell) session.openChannel("shell");
        Expect4j expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
        channel.connect();      
        return expect;
    }
    

    This method will open up SSH stream to remote server which will be used by expect4j for sending commands.

    private boolean executeCommands() {
            boolean isSuccess = true;
            Closure closure = new Closure() {
                public void run(ExpectState expectState) throws Exception {
                    buffer.append(expectState.getBuffer());//buffer is string buffer for appending output of executed command             
                    expectState.exp_continue();
                }
            };
            List<Match> lstPattern =  new ArrayList<Match>();
            String[] regEx = SSHConstants.linuxPromptRegEx;  
            if (regEx != null && regEx.length > 0) {
                synchronized (regEx) {
                    for (String regexElement : regEx) {//list of regx like,  :>, /> etc. it is possible command prompts of your remote machine
                        try {
                            RegExpMatch mat = new RegExpMatch(regexElement, closure);
                            lstPattern.add(mat);                        
                        } catch (MalformedPatternException e) {                     
                            return false;
                        } catch(Exception e) {                      
                            return false;
                        }
                    }
                    lstPattern.add(new EofMatch( new Closure() { // should cause entire page to be collected
                        public void run(ExpectState state) {
                        }
                    }));
                    lstPattern.add(new TimeoutMatch(defaultTimeOut, new Closure() {
                        public void run(ExpectState state) {
                        }
                    }));
                }
            }
            try {
                Expect4j expect = SSH(objConfig.getHostAddress(), objConfig.getUserName(), objConfig.getPassword(), SSHConstants.SSH_PORT);
                expect.setDefaultTimeout(defaultTimeOut);       
                if(isSuccess) {
                    for(String strCmd : lstCmds)
                        isSuccess = isSuccess(lstPattern,strCmd);
                }
                boolean isFailed = checkResult(expect.expect(lstPattern));
                return !isFailed;
            } catch (Exception ex) {            
                return false;
            } finally {
                closeConnection();
            }
        }
    
    
    private boolean isSuccess(List<Match> objPattern,String strCommandPattern) {
            try {   
                boolean isFailed = checkResult(expect.expect(objPattern));
    
                if (!isFailed) {
                    expect.send(strCommandPattern);         
                    expect.send("\r");              
                    return true;
                } 
                return false;
            } catch (MalformedPatternException ex) {    
                return false;
            } catch (Exception ex) {
                return false;
            }
    } 
    
     private boolean checkResult(int intRetVal) {
        if (intRetVal == SSHConstants.COMMAND_EXECUTION_SUCCESS_OPCODE) {
            return true;
        }
        return false;
     }
    
        public static final int COMMAND_EXECUTION_SUCCESS_OPCODE = -2; 
    public static final String BACKSLASH_R = "\r";
    public static final String BACKSLASH_N = "\n";
    public static final String COLON_CHAR = ":";
    public static String ENTER_CHARACTER = BACKSLASH_R;
    public static final int SSH_PORT = 22;  
    public static String[] linuxPromptRegEx = new String[]{"~]#","~#","#",":~#","/$",}  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason I need to cast/convert a DateTime into one of many custom
Let's say that for some reason you need to write a macro: MACRO(X,Y) .
pulled down a repo from git. For some reason I need to do ./script/rails.rb
Perviously the client uploaded same app on iTunes connect and rejected for some reason.
For some reason I never see this done. Is there a reason why not?
For some reason when I attempt to make a request to an Ajax.net web
For some reason when I create a new namespace in Visual Studio 2008 its
For some reason, when I try to install SQL Server 2008 Express , I
For some reason, lately the *.UDL files on many of my client systems are
For some reason, Section 1 works but Section 2 does not. When run in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.