Skip to content

Instantly share code, notes, and snippets.

@Julybui199x
Forked from stakemepro/set-ports-node.js
Last active September 18, 2023 01:09
Show Gist options
  • Save Julybui199x/f183010db62be32fbd0cd289163464e7 to your computer and use it in GitHub Desktop.
Save Julybui199x/f183010db62be32fbd0cd289163464e7 to your computer and use it in GitHub Desktop.
Auto set free port in cosmos node
set-ports-token-node.js
const shell = require("shelljs");
const TOKEN = process.argv[2];
const CONFIG = process.argv[3];
function openPort(port) {
return shell.exec(`sudo lsof -Pi :${port} -sTCP:LISTEN`, {shell: '/bin/bash', silent: true}).code !== 0;
}
for (let i = 0; i < 60; i++) {
let port = `${i + 26}657`;
if (openPort(port)) {
const proxyAppPort = `${i + 26}658`;
const firstLaddrPort = `${i + 26}657`;
const pprofLaddrPort = `${i + 6}060`;
const secondLaddrPort = `${i + 26}656`;
const externalAddress = `${i + 26}656`;
const prometheusListenAddr = `${i + 26}660`;
const firstAddress = `${i + 1}317`;
const secondAddress = `${i + 8}080`;
const thirdAddress = `${i + 9}090`;
const fourthAddress = `${i + 9}091`;
if (
openPort(proxyAppPort) && openPort(firstLaddrPort) && openPort(pprofLaddrPort) &&
openPort(secondLaddrPort) && openPort(externalAddress) && openPort(prometheusListenAddr) &&
openPort(firstAddress) && openPort(secondAddress) && openPort(thirdAddress) &&
openPort(fourthAddress)
) {
console.log('Set port:', port);
const firstCommandChange = `sudo sed -i.bak -e "\\
s%^proxy_app = \\"tcp://127.0.0.1:26658\\"%proxy_app = \\"tcp://127.0.0.1:${i+26}658\\"%; \\
s%^laddr = \\"tcp://127.0.0.1:26657\\"%laddr = \\"tcp://127.0.0.1:${i+26}657\\"%; \\
s%^pprof_laddr = \\"localhost:6060\\"%pprof_laddr = \\"localhost:${i+6}060\\"%; \\
s%^laddr = \\"tcp://0.0.0.0:26656\\"%laddr = \\"tcp://127.0.0.1:${i+26}656\\"%; \\
s%^external_address = \\"\\"%external_address = \\"\`echo 127.0.0.1:${i+26}656\`\\"%; \\
s%^prometheus_listen_addr = \\":26660\\"%prometheus_listen_addr = \\":${i+26}660\\"%" $HOME/${CONFIG}/config/config.toml`;
const secondCommandChange = `sudo sed -i.bak -e "\\
s%^address = \\"tcp://0.0.0.0:1317\\"%address = \\"tcp://127.0.0.1:${i+1}317\\"%; \\
s%^address = \\":8080\\"%address = \\":${i+8}080\\"%; \\
s%^address = \\"0.0.0.0:9090\\"%address = \\"127.0.0.1:${i+9}090\\"%; \\
s%^address = \\"0.0.0.0:9091\\"%address = \\"127.0.0.1:${i+9}091\\"%" $HOME/${CONFIG}/config/app.toml`
const thirdCommandChange = `${TOKEN} config node http://localhost:${i+26}657`
console.log(firstCommandChange);
console.log(secondCommandChange);
console.log(thirdCommandChange);
shell.exec(firstCommandChange, {silent: true, shell: '/bin/bash'});
shell.exec(secondCommandChange, {silent: true, shell: '/bin/bash'});
shell.exec(thirdCommandChange, {silent: true, shell: '/bin/bash'});
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment