Skip to content

Commit 7e5adb3

Browse files
author
Thidas Senavirathna
committed
[FEATURE] Telnet command #66
1 parent 1e7f7c1 commit 7e5adb3

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private static void registerCommands(Map<String, Command> commands) {
9696
commands.put("date", new DateCommand());
9797
commands.put("history", new HistoryCommand());
9898
commands.put("ping", new PingCommand());
99+
commands.put("telnet", new TelnetCommand());
99100
commands.put("pwd", new PwdCommand());
100101
commands.put("uptime", new UptimeCommand());
101102
commands.put("clearhistory", new ClearHistoryCommand());
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.ShellContext;
4+
5+
import java.io.*;
6+
import java.net.Socket;
7+
8+
/**
9+
* TelnetCommand - simple TCP client for interactive sessions.
10+
* Usage: telnet <host> [port]
11+
*
12+
* Notes:
13+
* - This is a minimal implementation (no Telnet option negotiation).
14+
* - It will take over stdin while connected; type 'exit' to return to the shell.
15+
*/
16+
public class TelnetCommand implements Command {
17+
18+
@Override
19+
public void execute(String[] args, ShellContext context) throws Exception {
20+
if (args.length < 1) {
21+
System.out.println("Usage: telnet <host> [port]");
22+
return;
23+
}
24+
25+
String host = args[0];
26+
int port = 23;
27+
if (args.length >= 2) {
28+
try {
29+
port = Integer.parseInt(args[1]);
30+
} catch (NumberFormatException e) {
31+
System.out.println("Invalid port: " + args[1]);
32+
return;
33+
}
34+
}
35+
36+
try (Socket socket = new Socket(host, port)) {
37+
socket.setSoTimeout(0); // blocking reads
38+
System.out.println("Connected to " + host + ":" + port + " (type 'exit' to quit)");
39+
40+
// Reader thread: prints remote data to stdout
41+
Thread reader = new Thread(() -> {
42+
try (InputStream in = socket.getInputStream();
43+
InputStreamReader isr = new InputStreamReader(in);
44+
BufferedReader br = new BufferedReader(isr)) {
45+
46+
char[] buffer = new char[2048];
47+
int read;
48+
while ((read = br.read(buffer)) != -1) {
49+
System.out.print(new String(buffer, 0, read));
50+
System.out.flush();
51+
}
52+
} catch (IOException ignored) {
53+
// socket closed or stream ended
54+
}
55+
}, "telnet-reader");
56+
reader.setDaemon(true);
57+
reader.start();
58+
59+
// Writer loop: read stdin and send to remote
60+
try (OutputStream out = socket.getOutputStream();
61+
OutputStreamWriter osw = new OutputStreamWriter(out);
62+
BufferedWriter bw = new BufferedWriter(osw);
63+
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in))) {
64+
65+
String line;
66+
while ((line = stdin.readLine()) != null) {
67+
if ("exit".equalsIgnoreCase(line.trim())) break;
68+
bw.write(line);
69+
bw.write("\r\n"); // typical telnet line ending
70+
bw.flush();
71+
}
72+
} catch (IOException ignored) {
73+
// stdin/socket error, will disconnect
74+
}
75+
76+
// ensure socket closes to stop reader
77+
try { socket.close(); } catch (IOException ignored) {}
78+
System.out.println("\nDisconnected.");
79+
} catch (IOException e) {
80+
System.out.println("Connection failed: " + e.getMessage());
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)