Files
wireadmin/src/lib/network.ts
Shahrad Elahi 4e3b93b2ef fix
2023-10-08 15:20:57 +03:30

39 lines
1.2 KiB
TypeScript

import Shell from "@lib/shell";
export default class Network {
public static async createInterface(inet: string, address: string): Promise<boolean> {
// First, check if the interface already exists.
const interfaces = await Shell.exec(`ip link show | grep ${inet}`, true)
if (interfaces.includes(`${inet}`)) {
console.error(`failed to create interface, ${inet} already exists!`)
return false
}
const o2 = await Shell.exec(`ip address add dev ${inet} ${address}`)
// check if it has any error
if (o2 !== '') {
console.error(`failed to assign ip to interface, ${o2}`)
console.log(`removing interface ${inet} due to errors`)
await Shell.exec(`ip link delete dev ${inet}`, true)
return false
}
return true
}
public static async dropInterface(inet: string) {
await Shell.exec(`ip link delete dev ${inet}`, true)
}
public static async defaultInterface(): Promise<string> {
return await Shell.exec(`ip route list default | awk '{print $5}'`)
}
public static async checkInterfaceExists(inet: string): Promise<boolean> {
return await Shell.exec(`ip link show | grep ${inet}`, true)
.then((o) => o.trim() !== '')
}
};