Exécuter une commande Linux sur un serveur dans un réseau
Bonjour tout le monde,
Je suis un debutant java et je voudrais faire un programme qui va me permettre de lancer des commades (cat, ls, cd, more, ...) LINUX sur un serveur à distance mais je ne trouve pas une bonne idée.
celle que j'ai c'est avec la class
Code:
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
import java.io.*;
import java.net.*;
class SocketClient
{
Socket sock;
byte []server = new byte[] {(byte)127,(byte)0,(byte)10,(byte)11};
int port = 22;
String filename = "/etc/hosts";
String command = "cat " + filename + "\n";
public static void main(String[] args)
{
new SocketClient();
}
public SocketClient()
{
openSocket();
try
{
// write to socket
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
wr.write(command);
wr.flush();
// read from socket
BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String str;
while ((str = rd.readLine()) != null)
{
System.out.println(str);
}
rd.close();
}
catch (IOException e)
{
System.err.println(e);
}
}
private void openSocket()
{
// open a socket and connect with a timeout limit
try
{
InetAddress addr = InetAddress.getByAddress(server);
SocketAddress sockaddr = new InetSocketAddress(addr, port);
sock = new Socket();
// this method will block for the defined number of milliseconds
int timeout = 2000;
sock.connect(sockaddr, timeout);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (SocketTimeoutException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
} |
Mais ce code me ranvoie à tous les coup
Code:
1 2 3 4 5
|
run:
SSH-1.99-OpenSSH_3.9p1
Protocol mismatch.
BUILD SUCCESSFUL (total time: 0 seconds) |
Pourriez vous s'il vous plait m'en donner quelque une?
Cordialement,
SALL