public
anonymous / Myshell.java
Created

  • Download Gist
Myshell.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
package installer;
 
import java.io.*;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URLEncoder;
import java.net.UnknownHostException;
import java.util.Properties;
 
/* Use: call it from other class as: Myshell.system("sudo convert /tmp/testing.tiff /tmp/testing.jpg");
*/
public class Myshell
{
public static String system(String cmd)
{
String value = "";
try
{
Process p=Runtime.getRuntime().exec(cmd);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
value += line + "\n\r";
line=reader.readLine();
}
 
} catch(IOException e1) {
} catch(InterruptedException e2) {
}
return value;
}
 
}
bash.sh
Shell
1 2 3 4 5 6 7
I have also tried:
=================
$ touch /tmp/bash.sh
$ chmod +x /tmp/bash.sh
$ echo "sudo convert /tmp/testing.tiff /tmp/testing.jpg" > /tmp/bash.sh
$ java -jar Myshell.jar # note: system("/tmp/bash.sh"); was used as absolute path
[sudo] password for alex:
username.isvalid
1 2 3 4 5 6 7
$ whoami
alex
$ which java
/usr/bin/java
$ which convert
/usr/bin/convert

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.