Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b42328196c5ffea791387af356ab582c8d3bdb37 (plain) (blame)
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
73
74
75
76
77
78
79
80
/*******************************************************************************
 * Copyright (c) 2009, 2011 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.internal.debug.ui.launch.setup;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketException;

class TelnetClient extends AbstractRemoteShell {

    private Socket socket;
    private boolean logged;

    TelnetClient(InetAddress address, int port,
            String user, String password) throws Exception {
        socket = new Socket(address, port);
        socket.setTcpNoDelay(true);
        InputStream ist = new BufferedInputStream(socket.getInputStream());
        OutputStream ost = new BufferedOutputStream(socket.getOutputStream());
        ist = new TelnetInputStream(ist, ost, true, PROMPT);
        ist = new TimeOutInputStream(ist, 512, 30000);
        out = new PrintWriter(ost, true);
        inp = new BufferedReader(new InputStreamReader(ist, "UTF-8"));
        expect("ogin: ");
        write(user + "\n");
        expect("assword: ");
        write(password + "\n");
        int i = expect(new String[]{"incorrect", PROMPT, "$ ", "# ", "> "});
        if (i == 0) {
            close();
            throw new Exception("Login incorrect");
        }
        logged = true;
        write("export PS1=\"" + PROMPT + "\"\n");
        expect(PROMPT + "\"\n");
        waitPrompt();
    }

    public synchronized void close() throws IOException {
        if (socket == null) return;
        if (logged) {
            write("exit\n");
            logged = false;
        }
        out.close();
        try {
            char[] buf = new char[0x100];
            while (inp.read(buf) >= 0) {}
        }
        catch (SocketException x) {
            if (!x.getMessage().startsWith("Socket closed")) throw x;
        }
        inp.close();
        socket.close();
        socket = null;
        inp = null;
        out = null;
    }

    public void finalize() throws Throwable {
        close();
        super.finalize();
    }
}

Back to the top