Bonjour,
Je développe actuellement une application en java pour la gestion des ports de switch. Afin de pouvoir modifier certains paramètres sur les ports j'utilise l'API JSCH.
J'ai réussi à executer une commande sur mon switch mais lorsque j'en fais plusieurs j'obtiens une erreur.
Voici mon code lorsque j'utilise ChannelExec
Ici je n'arrive a executer qu'une commande. J'ai donc essayé d'utiliser un shell. Lors de l'execution je peux rentrer manuellement dans le prompt les différentes commandes et ça fonctionne. J'aimerai pouvoir passer les commandes à partir de string plutôt que les rentrer à la main.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.io.InputStream; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelExec; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class SSH2 { public static void main(String[] args) { // Session create String command1 = "sys"; String command2 = "int GigabitEthernet1/0/1"; String command3 = "duplex full"; SSH2 test = new SSH2(); JSch jsch = new JSch(); String host = "adresseIP"; String username = "user"; String password = "password"; try { Session session = jsch.getSession(username, host, 22); session.setPassword(password); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.setPassword(password); session.connect(); // Creation channel et passage de commande Channel channel = session.openChannel("exec"); channel.setInputStream(null); channel.setOutputStream(System.out); ((ChannelExec) channel).setCommand(command1); channel.connect(); InputStream in = channel.getInputStream(); System.out.println(in.available()); System.out.println(channel.isClosed()); byte[] tmp = new byte[1024]; while (true) { while (in.available()>0) { int i = in.read(tmp, 0, 1024); if (i < 0) break; System.out.print(new String(tmp, 0, i)); } if (channel.isClosed()) { System.out.println("exit-status:"+channel.getExitStatus()); break; } try { Thread.sleep(1000); } catch (Exception ee) { ee.printStackTrace(); } } channel.disconnect(); session.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
Voici mon code avec le shell :
Si quelqu'un pouvait m'aider ou me guider. J'ai parcouru beaucoup de forum et les réponses apportées sur l'execution de plusieurs commandes n'étaient pas adaptés ou ne fonctionnaient pas.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
70
71
72 import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.OutputStream; import javax.swing.JOptionPane; import com.jcraft.jsch.Channel; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.UIKeyboardInteractive; import com.jcraft.jsch.UserInfo; public class Shell{ public static void main(String[] arg){ String sys = "systeme"; String monPort ="interface gigabitEthernet 1/0/1"; String duplex = "duplex full"; String vlan = "port access vlan 10"; try{ JSch jsch=new JSch(); String user = "user"; String host="adresseIP"; String passwd="password"; Session session=jsch.getSession(user, host, 22); session.setPassword(passwd); UserInfo ui = new MyUserInfo(){ public void showMessage(String message){ JOptionPane.showMessageDialog(null, message); } public boolean promptYesNo(String message){ Object[] options={ "yes", "no" }; int foo=JOptionPane.showOptionDialog(null, message, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); return foo==0; } }; session.setUserInfo(ui); session.connect(30000); Channel channel=session.openChannel("shell"); channel.setInputStream(System.in); channel.setOutputStream(System.out); channel.connect(3*1000); InputStream in = new ByteArrayInputStream(sys.getBytes()); } catch(Exception e){ System.out.println(e); } } public static abstract class MyUserInfo implements UserInfo, UIKeyboardInteractive{ public String getPassword(){ return null; } public boolean promptYesNo(String str){ return false; } public String getPassphrase(){ return null; } public boolean promptPassphrase(String message){ return false; } public boolean promptPassword(String message){ return false; } public void showMessage(String message){ } public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo){ return null; } } }
Merci pour votre aide.
Partager