⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 snmptftptest.java

📁 实现cisco snmp tftp,能用snmp tftp上传下载文件
💻 JAVA
字号:
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import org.opennms.protocols.snmp.SnmpParameters;
import org.opennms.protocols.snmp.SnmpPduPacket;
import org.opennms.protocols.snmp.SnmpPduRequest;
import org.opennms.protocols.snmp.SnmpPeer;
import org.opennms.protocols.snmp.SnmpSMI;
import org.opennms.protocols.snmp.SnmpSession;

public class SnmptftpTest {
	
	boolean snmpRequest(String RemoteAddr, String Community, String FileName, String FileType, String Flag)
	{
		String FailureCause=null;
		int ft;
		snmptftp walker = new snmptftp();
		
		walker.setCommunity(RemoteAddr, Community);
		
		if( FileType.equals("startupConfig"))
			ft = 3;
		else if(FileType.equals("runningConfig"))
			ft = 4;
		else if(FileType.equals("iosFile"))
			ft = 2;
		else
		{
			FailureCause = "参数(文件类型)错误";
			return false;
		}
	
		walker.setVarValue(FileName, Flag.equals("T"), ft);
		
		InetAddress remote = null;
		
		try
		{
			remote = InetAddress.getByName(walker.m_host);
		}
		catch(UnknownHostException e)
		{
			System.err.println("UnknownHostException: " + e.getMessage());
			FailureCause = "参数(路由器地址)错误";
			return false;
		}
		
		SnmpPeer peer = new SnmpPeer(remote);
		if(walker.m_port != -1)
			peer.setPort(walker.m_port);
		
		if(walker.m_timeout != -1)
			peer.setTimeout(walker.m_timeout);
		
		if(walker.m_retries != -1)
			peer.setRetries(walker.m_retries);

		SnmpParameters parms = peer.getParameters();
		parms.setVersion(SnmpSMI.SNMPV2);
		
		if(walker.m_community != null)
		{
			parms.setWriteCommunity(walker.m_community);
			parms.setReadCommunity(walker.m_community);
		}
	
		//
		// Now create the session, set the initial request
		// and walk the tree!
		//
		SnmpSession session = null;
		try
		{
			session = new SnmpSession(peer);
		}
		catch(SocketException e)
		{
			System.err.println("SocketException creating the SNMP session");
			System.err.println("SocketException: " + e.getMessage());
			FailureCause = "SNMP请求失败,建立连接错误";
			return false;
		}
		
		session.setDefaultHandler(walker);
		
		SnmpPduRequest pdu = new SnmpPduRequest(SnmpPduPacket.SET);
		pdu.addVarBindAt(0, walker.m_mibvar[walker.start_ndx]);
		pdu.setRequestId(SnmpPduPacket.nextSequence());
		
		try
		{
			synchronized(session)
			{
				session.send(pdu);
				session.wait();
			}
		}
		catch(InterruptedException e) 
		{ 
			// do nothing
		}
		finally
		{
			session.close();
		}
		 FailureCause = walker.getFailureCause();
		return walker.getResult();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -