我想通过Android应用程序中的覆盆子pi上的ssh执行以下命令:
echo 'value of a variable string named cmd' > filename.txt
我试过以下:
String a="echo '";
String c="' > filename.txt";
String cmd=a+clip+b;
channelSsh.setCommand(cmd);
普通命令,如" sudo reboot"有效但不是这个!
我的程序有点长但你可以找到一个简单的JSch实现程序Here(第二个答案)。
如果您仍想查看我的代码(事情在copyMethod()和executeRemoteCommand()函数中):
JAVA:
package com.quickclip.panky.quickclip;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.ByteArrayOutputStream;
import java.util.Properties;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText e1;
Button b1,b2,b3;
TextView t1,t2;
Activity activity = this;
static int flag=2,time=3000;
static ClipData clip=null;
String output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText);
t1 = (TextView) findViewById(R.id.textView);
t2 = (TextView) findViewById(R.id.textView3);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CopyMethod();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
VolDown();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CopyPace();
}
});
Runnable myRunnable = new Runnable() {
@Override
public void run() {
while (true) {
if (flag % 2 == 0) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (flag % 2 != 0) {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
activity.runOnUiThread(new Runnable() {
public void run() {
CopyMethod();
flag += 1;
}
});
}
}
};
Thread myThread = new Thread(myRunnable);
myThread.start();
}
public void VolDown(View view) {VolDown();}
public void CopyPace(View view) {CopyPace();}
public void ManCopy(View view) {CopyMethod();}
public void VolDown() {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Copied Text","decrease volume");
clipboard.setPrimaryClip(clip);
}
public void CopyPace() {
if(time==5000) time=2000;
else if(time<5000) time+=1000;
t1.setText("Automatically Sending in: "+(time/1000)+" sec");
}
public void CopyMethod() {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Copied Text", (CharSequence) e1.getText().toString());
clipboard.setPrimaryClip(clip);
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
output=executeRemoteCommand();
t2.setText(output);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
e1.setText("");
}
public static String executeRemoteCommand()
throws Exception {
String username="pi";
String password="10<,mmXLSQ";
String hostname="192.168.43.41";
int port=22;
String a="echo '",c="' > yo.txt";
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelSsh = (ChannelExec)
session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelSsh.setOutputStream(baos);
// Execute command
String cmd=a+clip+c;
channelSsh.setCommand(cmd);
channelSsh.connect();
channelSsh.disconnect();
return baos.toString();
}
@Override
public void onClick(View view) {CopyMethod();VolDown();CopyPace();}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.quickclip.panky.quickclip.MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:ems="100"
android:inputType="textAutoCorrect" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@+id/editText"
android:text="Send it Now"
android:onClick="ManCopy"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText"
android:text="Volume Down"
android:onClick="VolDown"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true"
android:text="Switch Pace"
android:onClick="CopyPace"/>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:layout_marginTop="130dp"
android:text="Automatically Sending in: 3 sec" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:text="Speak/Type in this time" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:textSize="20dp"
android:text="Connecting to SSH" />
</android.widget.RelativeLayout>
答案 0 :(得分:0)
始终clip
中使用null
String cmd=a+clip+c;
你在这里调用ClipData.toString(),返回对象的字符串表示
从ClipData中获取文本
String text = "";
ClipData clip = getPrimaryClip();
if (clip != null && clip.getItemCount() > 0) {
text = clip.getItemAt(0).coerceToText(mContext);
}
它将检索主剪辑并强制将其转换为字符串
在执行前使用Log
记录命令,并检查您是否使用了正确的命令
答案 1 :(得分:0)
我解决了我的问题,...谢谢你的时间...... thnx Arpan ......
package com.quickclip.panky.quickclip;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.ByteArrayOutputStream;
import java.util.Properties;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText e1;
Button b1,b2,b3;
TextView t1,t2;
Activity activity = this;
static int flag=2,time=3000;
static ClipData clip=null;
String output;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText);
t1 = (TextView) findViewById(R.id.textView);
t2 = (TextView) findViewById(R.id.textView3);
b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button) findViewById(R.id.button3);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CopyMethod();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
VolDown();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CopyPace();
}
});
Runnable myRunnable = new Runnable() {
@Override
public void run() {
while (true) {
if (flag % 2 == 0) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (flag % 2 != 0) {
try {
Thread.sleep(800);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
activity.runOnUiThread(new Runnable() {
public void run() {
CopyMethod();
flag += 1;
}
});
}
}
};
Thread myThread = new Thread(myRunnable);
myThread.start();
}
public void VolDown(View view) {VolDown();}
public void CopyPace(View view) {CopyPace();}
public void ManCopy(View view) {CopyMethod();}
public void VolDown() {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Copied Text","decrease volume");
clipboard.setPrimaryClip(clip);
}
public void CopyPace() {
if(time==5000) time=2000;
else if(time<5000) time+=1000;
t1.setText("Automatically Sending in: "+(time/1000)+" sec");
}
public void CopyMethod() {
final String username="pi";
final String password="10<,mmXLSQ";
final String hostname="192.168.43.41";
final int port=22;
final String a="echo '",c="' > panky.txt";
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
final ClipData clip = ClipData.newPlainText("Copied Text", (CharSequence) e1.getText().toString());
clipboard.setPrimaryClip(clip);
new AsyncTask<Integer, Void, Void>(){
@Override
protected Void doInBackground(Integer... params) {
try {
JSch jsch = new JSch();
Session session = jsch.getSession(username, hostname, port);
session.setPassword(password);
// Avoid asking for key confirmation
Properties prop = new Properties();
prop.put("StrictHostKeyChecking", "no");
session.setConfig(prop);
session.connect();
// SSH Channel
ChannelExec channelSsh = (ChannelExec)
session.openChannel("exec");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
channelSsh.setOutputStream(baos);
// Execute command
String cmd=a+clip+c;
channelSsh.setCommand(cmd);
channelSsh.connect();
channelSsh.disconnect();
t2.setText(output);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}.execute(1);
e1.setText("");
}
@Override
public void onClick(View view) {CopyMethod();VolDown();CopyPace();}
}