| 12
 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
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 
 | public interface_Snmp(){
        this.setTitle(" SNMP ");
 
        setSize(800,700);
 
        jmenuFichier = new JMenu("Fichier");
        jmenuFichier.setMnemonic(KeyEvent.VK_F);
 
 
        jmenuitemRetourMenu= new JMenuItem("Retour Menu Principal");
        jmenuitemRetourMenu.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_R,ActionEvent.CTRL_MASK));
 
 
        jmenuitemFermer= new JMenuItem("Fermer");
        jmenuitemFermer.setAccelerator(KeyStroke.getKeyStroke( KeyEvent.VK_X,ActionEvent.CTRL_MASK));
 
        jmenuFichier.add(jmenuitemRetourMenu);
        jmenuFichier.add(jmenuitemFermer);
 
 
        JMenuBar mb = new JMenuBar();
        mb.add(jmenuFichier);
        setJMenuBar(mb);
 
        jpnIp= new JPanel(new GridLayout(4,1));
        JPanel jpnIp2 = new JPanel();
        JLabel jlbIp2 = new JLabel("  Donner l'Adresse IP de l'équipement et la Community: ");
        jpnIp2.setLayout(new FlowLayout(FlowLayout.CENTER));
        jlbIp=new JLabel("Adresse   IP   : ");
        jtfIp=new JTextField(10);
        jtfIp.setText("127.0.0.1");
 
        jpnIp2.add(jlbIp);
        jpnIp2.add(jtfIp);
        jpnIp.add(jlbIp2);
        jpnIp.add(jpnIp2);
 
        jpnOID=new JPanel(new GridLayout(2,1));
        jlbOID=new JLabel("OID : ");
        jtfOID=new JTextField(15);
        jtfOID.setText("1.3.6.1.2.1.1.1");
        jpnOID2= new JPanel(new FlowLayout(FlowLayout.CENTER));
        jpnOID2.add(jlbOID);
        jpnOID2.add(jtfOID);
        jpnOID.add(jpnOID2);
        jpnIp.add(jpnOID);
 
        jpnCom = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JPanel jpnCom2 = new JPanel();
        jpnCom2.setLayout(new FlowLayout(FlowLayout.CENTER));
        jlbCom=new JLabel("  Community   : ");
        jtfCom=new JTextField(10);
        jtfCom.setText("public");        
 
        jpnCom2.add(jlbCom);
        jpnCom2.add(jtfCom);
        jpnCom.add(jpnCom2);
        jpnIp.add(jpnCom);
 
        jpnGenerale = new JPanel();
        jpnGenerale.setLayout(new GridLayout(3,1));
 
        //jpnGenerale.add(jpnOID);
        jbtValider= new JButton("Retrouver OID");
        jbtEffacer= new JButton("Effacer");
        jpnIp.add(jpnOID);
        jpnButton = new JPanel(new FlowLayout(FlowLayout.CENTER));
        jpnButton.add(jbtValider);
        jpnButton.add(jbtEffacer);
        jpnGenerale.add(jpnIp);
        jpnGenerale.add(jpnButton);
 
        /*  */
        jtaMessage = new JTextArea();
        jtaMessage.setEditable(false);
        scrollPane = new JScrollPane(jtaMessage,
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
                JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        jpnGenerale.add(scrollPane);
        getContentPane().add(jpnGenerale);
 
        jbtValider.addActionListener(this);
        jbtEffacer.addActionListener(this);
 
    }
        public void actionPerformed(ActionEvent e) {
            try{
            if (e.getSource()==jbtValider){
                Ip=jtfIp.getText();
                OID1=jtfOID.getText();
                Community=jtfCom.getText();
                Address targetAddress = GenericAddress.parse("udp:"+Ip+"/161"); 
                TransportMapping transport = new DefaultUdpTransportMapping(); 
                Snmp snmp = new Snmp(transport); 
                USM usm = new USM(SecurityProtocols.getInstance(), 
                new OctetString(), 0); 
                SecurityModels.getInstance().addSecurityModel(usm); 
                transport.listen(); 
                snmp.listen(); 
 
                CommunityTarget target = new CommunityTarget();
                target.setCommunity(new OctetString(Community));
                target.setAddress(targetAddress);
                target.setRetries(2);
                target.setTimeout(1500);
                target.setVersion(SnmpConstants.version1);
                // creating PDU
                PDU pdu = new PDU();
                pdu.add(new VariableBinding(new OID(OID1)));
                //pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,2})));
                pdu.setType(PDU.GETNEXT);
                // sending request
                ResponseListener listener = new ResponseListener() {
                public void onResponse(ResponseEvent event) {
                   // Always cancel async request when response has been received
                   // otherwise a memory leak is created! Not canceling a request
                   // immediately can be useful when sending a request to a broadcast
                   // address.
                   ((Snmp)event.getSource()).cancel(event.getRequest(), this);
                  // System.out.println("Received response PDU is: "+event.getResponse());
                   jtaMessage.append(event.getResponse().get(0).toString());
                   jtaMessage.append("\n");
                 }
               };
               snmp.sendPDU(pdu, target, null, listener);
               }
 
            else if (e.getSource()==jbtEffacer){
                jtaMessage.setText("");
            }
 
            }
            catch(IOException ex){}    
        }
    public static void main(String[] args)  
    { 
        interface_Snmp interface_Snmp = new interface_Snmp();
        interface_Snmp.setVisible(true);
    } 
} | 
Partager