Skip to content

Commit c285286

Browse files
Merge branch 'main' into feat/Kaveesha/renameCmd
2 parents d7c6562 + 00e06aa commit c285286

File tree

5 files changed

+202
-13
lines changed

5 files changed

+202
-13
lines changed

CheckList.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,4 @@
216216
- [PhoenixNAP CMD Commands Guide](https://phoenixnap.com/kb/cmd-commands)
217217

218218
---
219+

README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,23 @@ MyCMD/
6666

6767
---
6868

69-
## 🚀 Features
69+
## 🚀 Key Features
7070

71-
- ✅ Windows-like commands (`dir`, `cd`, `mkdir`, `del`, `copy`, `type`, `cls`)
72-
- ✅ Extensible via Java classes (easy to add new commands)
73-
- ✅ Packaged into `.exe` using **Launch4j**
74-
- ✅ Installer wizard using **Inno Setup**
75-
- ✅ Works on **Windows** and **Linux** (via `jpackage`)
71+
<div align="center">
72+
73+
| Feature | Description |
74+
|---------|-------------|
75+
|**Windows-like Commands** | Supports `dir`, `cd`, `mkdir`, `del`, `copy`, `type`, `cls` |
76+
|**Extensible** | Easily add new commands via Java classes |
77+
| 🖥️ **Executable Packaging** | Packaged into `.exe` using **Launch4j** |
78+
| 🛠️ **Installer Wizard** | Simple setup using **Inno Setup** |
79+
| 🌐 **Cross-Platform** | Works on **Windows** and **Linux** (via `jpackage`) |
80+
81+
</div>
7682

7783
---
7884

85+
7986
## 📦 Requirements
8087

8188
To build MyCMD you need:
@@ -140,20 +147,27 @@ You can now use commands like:
140147
> [!NOTE]
141148
> There are many commands left like `ls` and `Ping Command`
142149
143-
### 🤝 Contributing
150+
## 🤝 Contributing
144151

145-
- Fork the repo (or request an invite if it’s in the org)
152+
Contributions are what make the open-source community such an amazing place to learn, inspire, and create! 💪
146153

147-
- Create a feature branch
154+
We welcome all kinds of improvements — from fixing bugs 🐛 to adding new commands ⚙️.
148155

149-
- Commit your changes
156+
### 🪜 Steps to Contribute
150157

151-
- Open a Pull Request
158+
1. 🍴 **Fork** the repository
159+
2. 🌿 **Create a new branch**
160+
```bash
161+
git checkout -b feature/your-feature-name
152162

153163
## 📜 License
154164

155-
Licensed under the Apache License
156-
You are free to use, modify, and distribute.
165+
🧾 **Apache License 2.0**
166+
This project is open-source and available for anyone to use, modify, and share under the terms of the Apache License.
167+
168+
> ✨ Attribution is appreciated but not required.
169+
> 📎 For complete terms, check the [LICENSE](./LICENSE) file.
170+
157171

158172

159173
<img src="https://capsule-render.vercel.app/api?type=waving&color=gradient&customColorList=6,11,20&height=120&section=footer&text=Java,%20Java,%20and%20My%20Java&fontSize=20&fontColor=fff&animation=twinkling"/>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ 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());
102103
commands.put("ipconfig", new IpConfig());
103104
commands.put("alias", new AliasCommand());
104105
commands.put("unalias", new UnaliasCommand());
105106
commands.put("rename", new RenameCommand());
107+
commands.put("tasklist", new TasklistCommand());
106108
}
107109
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
import java.io.IOException;
7+
import java.lang.management.ManagementFactory;
8+
import java.lang.management.RuntimeMXBean;
9+
10+
/**
11+
* TasklistCommand - displays information about running processes.
12+
* Usage: tasklist
13+
*
14+
* Notes:
15+
* - Shows Java process information and system processes if available
16+
* - Limited to what Java can access without platform-specific tools
17+
*/
18+
public class TasklistCommand implements Command {
19+
20+
@Override
21+
public void execute(String[] args, ShellContext context) throws IOException {
22+
System.out.println();
23+
System.out.printf("%-40s %10s %15s%n", "Image Name", "PID", "Memory Usage");
24+
System.out.println("=" .repeat(70));
25+
26+
// Get current Java process information
27+
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
28+
String jvmName = runtimeMXBean.getName();
29+
String[] parts = jvmName.split("@");
30+
String pid = parts.length > 0 ? parts[0] : "Unknown";
31+
32+
// Get memory info
33+
Runtime runtime = Runtime.getRuntime();
34+
long usedMemory = (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024); // MB
35+
36+
System.out.printf("%-40s %10s %12s MB%n", "java.exe", pid, usedMemory);
37+
38+
// Try to get system processes using ProcessHandle (Java 9+)
39+
try {
40+
ProcessHandle.allProcesses()
41+
.limit(20) // Limit to first 20 processes
42+
.forEach(process -> {
43+
ProcessHandle.Info info = process.info();
44+
long processPid = process.pid();
45+
String command = info.command().orElse("Unknown");
46+
47+
// Extract just the executable name from full path
48+
String execName = command;
49+
if (command.contains("/") || command.contains("\\")) {
50+
int lastSlash = Math.max(command.lastIndexOf('/'), command.lastIndexOf('\\'));
51+
execName = command.substring(lastSlash + 1);
52+
}
53+
54+
// Truncate long names
55+
if (execName.length() > 40) {
56+
execName = execName.substring(0, 37) + "...";
57+
}
58+
59+
System.out.printf("%-40s %10d %15s%n", execName, processPid, "N/A");
60+
});
61+
} catch (Exception e) {
62+
// ProcessHandle not available or error accessing processes
63+
System.out.println("\n[Additional process information not available]");
64+
}
65+
66+
System.out.println();
67+
}
68+
69+
@Override
70+
public String description() {
71+
return "Display a list of currently running processes.";
72+
}
73+
74+
@Override
75+
public String usage() {
76+
return "tasklist";
77+
}
78+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.mycmd.commands;
2+
3+
import com.mycmd.Command;
4+
import com.mycmd.ShellContext;
5+
6+
import java.io.*;
7+
import java.net.Socket;
8+
9+
/**
10+
* TelnetCommand - simple TCP client for interactive sessions.
11+
* Usage: telnet <host> [port]
12+
*
13+
* Notes:
14+
* - This is a minimal implementation (no Telnet option negotiation).
15+
* - It will take over stdin while connected; type 'exit' to return to the shell.
16+
*/
17+
public class TelnetCommand implements Command {
18+
19+
@Override
20+
public void execute(String[] args, ShellContext context) throws IOException {
21+
if (args.length < 1) {
22+
System.out.println("Usage: telnet <host> [port]");
23+
return;
24+
}
25+
26+
String host = args[0];
27+
int port = 23;
28+
if (args.length >= 2) {
29+
try {
30+
port = Integer.parseInt(args[1]);
31+
} catch (NumberFormatException e) {
32+
System.out.println("Invalid port: " + args[1]);
33+
return;
34+
}
35+
}
36+
37+
try (Socket socket = new Socket(host, port)) {
38+
socket.setSoTimeout(0); // blocking reads
39+
System.out.println("Connected to " + host + ":" + port + " (type 'exit' to quit)");
40+
41+
// Reader thread: prints remote data to stdout
42+
Thread reader = new Thread(() -> {
43+
try (InputStream in = socket.getInputStream();
44+
InputStreamReader isr = new InputStreamReader(in);
45+
BufferedReader br = new BufferedReader(isr)) {
46+
47+
char[] buffer = new char[2048];
48+
int read;
49+
while ((read = br.read(buffer)) != -1) {
50+
System.out.print(new String(buffer, 0, read));
51+
System.out.flush();
52+
}
53+
} catch (IOException ignored) {
54+
// socket closed or stream ended
55+
}
56+
}, "telnet-reader");
57+
reader.setDaemon(true);
58+
reader.start();
59+
60+
// Writer loop: read stdin and send to remote
61+
try (OutputStream out = socket.getOutputStream();
62+
OutputStreamWriter osw = new OutputStreamWriter(out);
63+
BufferedWriter bw = new BufferedWriter(osw);
64+
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in))) {
65+
66+
String line;
67+
while ((line = stdin.readLine()) != null) {
68+
if ("exit".equalsIgnoreCase(line.trim())) break;
69+
bw.write(line);
70+
bw.write("\r\n"); // typical telnet line ending
71+
bw.flush();
72+
}
73+
} catch (IOException ignored) {
74+
// stdin/socket error, will disconnect
75+
}
76+
77+
// ensure socket closes to stop reader
78+
try { socket.close(); } catch (IOException ignored) {}
79+
System.out.println("\nDisconnected.");
80+
} catch (IOException e) {
81+
System.out.println("Connection failed: " + e.getMessage());
82+
}
83+
}
84+
85+
@Override
86+
public String description() {
87+
return "Simple TCP client for interactive sessions.";
88+
}
89+
90+
@Override
91+
public String usage() {
92+
return "telnet <host> [port]";
93+
}
94+
}

0 commit comments

Comments
 (0)