Skip to content

Commit

Permalink
Updated ShotGun to use Range and AddComputers to trim
Browse files Browse the repository at this point in the history
  • Loading branch information
dominusmars committed Mar 4, 2024
1 parent 0f8d21a commit 2cf23c4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 41 deletions.
68 changes: 34 additions & 34 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions src/front/page/addComputer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { Home } from "../menu/home";
import { pressEnter } from "../../modules/console/enddingModules";
import options from "../../modules/util/options";
const addComputer = async function () {
const { ip, user, pass } = await inquirer.prompt([
const { ip_input, user_input, pass_input }: { ip_input: string; user_input: string; pass_input: string } = await inquirer.prompt([
{
name: "ip",
name: "ip_input",
message: "please enter an ip:",
type: "input",
validate: (v) => {
Expand All @@ -20,17 +20,24 @@ const addComputer = async function () {
},
},
{
name: "user",
name: "user_input",
message: "please enter a username",
type: "input",
},
{
name: "pass",
name: "pass_input",
message: "please enter a password",
type: "input",
},
]);

const ip = ip_input.trim();
const user = user_input.trim();
const pass = pass_input.trim();
log(`Attempting connection to ${ip} using ${user}`, "info");

await trySSH();

async function trySSH(): Promise<void> {
var computer_info = await scanSSH(ip, user, pass);
// console.log(computer_info);
Expand Down
27 changes: 24 additions & 3 deletions src/front/page/ssh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function ShotGun() {

return true;
},
message: "host ids (the last digit of the ip)",
message: "host ids (separated by spaces) (the last digit of the ip)",
},
{
name: "usernames_string",
Expand All @@ -57,7 +57,8 @@ async function ShotGun() {
let network_ip = ip_net.split(".");
network_ip.pop();
network_ip = network_ip.join(".").trim();
let hosts = hosts_ids.split(" ");
let hosts = expandHostIds(hosts_ids);

var computers = hosts.map((value: string) => {
value = value.trim();
return network_ip + "." + value;
Expand Down Expand Up @@ -142,7 +143,9 @@ async function ShotGun() {
try {
var string = "\n";
for (const computer of computers) {
string += `${computer.ip} ${computer.name}\n`;
let computerInfo = await runningDB.getComputer(computer);
if (!computerInfo) continue;
string += `${computer} ${computerInfo.Name}\n`;
}
if (process.platform === "linux" || process.platform === "darwin" || process.platform === "freebsd" || process.platform === "openbsd") {
await exec(`echo '${string}' | sudo tee -a /etc/hosts`);
Expand All @@ -158,5 +161,23 @@ async function ShotGun() {
bar.stop();
console.clear();
}
function expandHostIds(hosts_ids: string): string[] {
const hosts: string[] = [];
const ranges: string[] = hosts_ids.split(" ");

ranges.forEach((range) => {
const [start, end] = range.split("-").map(Number);
if (!end) {
if (isNaN(start)) return;
hosts.push(start.toString());
} else {
if (isNaN(start) || isNaN(end)) return;
for (let i = start; i <= end; i++) {
hosts.push(i.toString());
}
}
});

return hosts;
}
export { ShotGun as sshMenu };

0 comments on commit 2cf23c4

Please sign in to comment.