有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Jsch ssh与edittext 安卓

你好,我有一个问题,我使用jsch通过ssh连接到例如一个设备,我的代码工作,但它使用集成的用户名,密码和主机,我想在那里创建活动写用户名密码和主机,然后连接并执行一些命令,例如“重新启动”。。。当我在edittext字段中输入ip adpress时,从那里获取数据而不是固定的ip,我想你可以理解我的意思,并为我糟糕的英语道歉:)这是我的代码>>

活动。爪哇

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ssh);

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    mToolbar.inflateMenu(R.menu.main_menu);
    setSupportActionBar(mToolbar);
}

@SuppressLint("StaticFieldLeak")
public void onClick11 (View v) {
    new AsyncTask<Integer, Void, Void>(){
        @Override
        protected Void doInBackground(Integer... params) {
            try {
                executeSSHcommand();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute(1);
}


@SuppressLint("StaticFieldLeak")
public void onClickct (View v) {
    new AsyncTask<Integer, Void, Void>(){
        @Override
        protected Void doInBackground(Integer... params) {
            try {
                Compliancetest();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    }.execute(1);
}

public void executeSSHcommand(){
    String user = "user";
    String password = "user";
    String host = "192.168.1.1";
    int port=22;
    try{

        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setTimeout(10000);
        session.connect();
        ChannelExec channel = (ChannelExec)session.openChannel("exec");
        channel.setCommand("reboot");
        channel.connect();
        channel.disconnect();

    }
    catch(JSchException e){

    }
}

public void Compliancetest(){
    String user = "user";
    String password = "user";
    String host = "192.168.1.1";
    int port=22;
    try{

        JSch jsch = new JSch();
        Session session = jsch.getSession(user, host, port);
        session.setPassword(password);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setTimeout(10000);
        session.connect();
        ChannelExec channel = (ChannelExec)session.openChannel("exec");
        channel.setCommand("reboot");
        channel.connect();
        channel.disconnect();

    }
    catch(JSchException e){

    }
}

还有我的活动。xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/activity_main"
安卓:orientation="vertical"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:weightSum="1">

<include
    安卓:id="@+id/toolbar"
    layout="@layout/toolbar">
</include>

<EditText
    安卓:id="@+id/ip"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:ems="10"
    安卓:inputType="textPersonName"
    安卓:hint="IP Address" />
<EditText
    安卓:id="@+id/uname"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:ems="10"
    安卓:inputType="textPersonName"
    安卓:hint="Username" />

<EditText
    安卓:id="@+id/pw"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:ems="10"
    安卓:inputType="textPassword"
    安卓:hint="Password" />

<Button
    安卓:id="@+id/button"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="20dp"
    安卓:onClick="onClick11"
    安卓:text="@string/command" />

<Button
    安卓:id="@+id/button1"
    安卓:layout_width="match_parent"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="20dp"
    安卓:onClick="onCommand"
    安卓:text="@string/reboot" />


共 (1) 个答案

  1. # 1 楼答案

    例如,让我们谈谈从以下方面获取ip:

    String ipName;
    

    EditText ip =findViewById(R.id.ip);

    ipName = ip.getText();

    把它放在底部。单击Listener在编辑文本填充后执行此操作