ftp服务检测

 1.直接的socket连接,改自openNMS,参考ftp://ftp.rfc-editor.org/in-notes/std/std9.txt
         int  retry  =   2 ;
        
int  port  =   21 ;
        
int  timeout  =   3000 ;
        String userid 
=   " test " ;
        String password 
=   " test " ;
        InetAddress ipv4Addr 
=   null ;
        
try   {
            ipv4Addr 
= InetAddress.getByName("192.168.7.61");
        }
  catch  (UnknownHostException e1)  {
            e1.printStackTrace();
        }


        
int  serviceStatus  =   1 ;
        
long  responseTime  =   - 1 ;
        
for  ( int  attempts  =   0 ; attempts  <=  retry  &&  serviceStatus  ==   1 ; attempts ++ {
            Socket socket 
= null;
            
try {
                
// create a connected socket
                long sentTime = System.currentTimeMillis();

                socket 
= new Socket();
                socket.connect(
new InetSocketAddress(ipv4Addr, port), timeout);
                socket.setSoTimeout(timeout);

                
// We're connected, so upgrade status to unresponsive
                serviceStatus = -1;

                BufferedReader lineRdr 
= new BufferedReader(
                        
new InputStreamReader(socket.getInputStream()));

                
// Tokenize the Banner Line, and check the first
                
// line for a valid return.
                responseTime = System.currentTimeMillis() - sentTime;
                String banner 
= lineRdr.readLine();
                System.out.println(
"banner=" + banner);
                StringTokenizer t 
= new StringTokenizer(banner);

                
int rc = -1;
                
try {
                    rc 
= Integer.parseInt(t.nextToken());
                }
 catch (NumberFormatException nfE) {
                    nfE.fillInStackTrace();
                }


                
// Verify that return code is in proper range.
                if (rc >= 200 && rc <= 299{
                    
// Attempt to login if userid and password available
                    boolean bLoginOk = false;
                    
if (userid == null || userid.length() == 0
                            
|| password == null || password.length() == 0{
                        bLoginOk 
= true;
                    }
 else {
                        
// send the use string
                        String cmd = "user " + userid + " ";
                        socket.getOutputStream().write(cmd.getBytes());
                        
// get the response code.
                        String response = null;
                        response 
= lineRdr.readLine();
                        System.out.println(
"response=" + response);
                        t 
= new StringTokenizer(response);
                        rc 
= Integer.parseInt(t.nextToken());

                        
// Verify that return code is in proper range.
                        if (rc >= 200 && rc <= 399{
                            
// send the password
                            cmd = "pass " + password + " ";
                            socket.getOutputStream().write(cmd.getBytes());

                            
// get the response...check for multi-line response
                            response = lineRdr.readLine();
                            
if (response == null)
                                
continue;

                            
// Verify that return code is in proper range.
                            System.out.println("FtpMonitor.poll: tokenizing respone to check for return code: "+ response);
                            t 
= new StringTokenizer(response);
                            rc 
= Integer.parseInt(t.nextToken());
                            
if (rc >= 200 && rc <= 299{
                                System.out.println(
"FtpMonitor.poll: Login successful, parsed return code: "+ rc);
                                bLoginOk 
= true;
                            }
 else {
                                System.out.println(
"FtpMonitor.poll: Login failed, parsed return code: "+ rc);
                                bLoginOk 
= false;
                            }

                        }

                    }


                    
if (bLoginOk) {
                        
// FTP should recognize the QUIT command
                        String cmd = "QUIT ";
                        socket.getOutputStream().write(cmd.getBytes());

                        
// get the returned string, tokenize, and
                        
// verify the correct output.
                        String response = lineRdr.readLine();

                        t 
= new StringTokenizer(response);
                        rc 
= Integer.parseInt(t.nextToken());

                        
// Verify that return code is in proper range.
                        System.out.println("rc==" + rc);
                        serviceStatus 
= 1;
                    }

                }


                
// If we get this far and the status has not been set
                
// to available, then something didn't verify during
                
// the banner checking or login/QUIT command process.
                if (serviceStatus == 1{
                    serviceStatus 
= -1;
                }


            }
 catch (Exception e) {
                e.printStackTrace();
            }

        }

2.运用开发包API,edtftpj-1.5.3, http://www.enterprisedt.com
        FTPClient ftp  =   new  FTPClient();
        
try   {
            ftp.setRemoteHost(
"192.168.7.61");
            ftp.setRemotePort(
21);
            ftp.setConnectMode(FTPConnectMode.ACTIVE);
            ftp.connect();
            ftp.login(
"test""test");
        }
  catch  (Exception e)  {
            System.out.println(
"connect error");
            e.printStackTrace();
        }
  finally   {
            
if (ftp != null)
                
try {
                    ftp.quit();
                }
 catch (Exception e) {
                    System.out.println(
"quit error");
                    e.printStackTrace();
                }

        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值