Thursday 1 June 2017

SSH with nodejs

Attempting to create files and such on new vps that was created using api. This function does not run the second ssh command for some reason. Before this function is run there's another that is also run that sends a few commands to install packages before it installs the files. I have the function running after a timer has run out so that it can use some of the packages to run the commands like screen. It runs the first ssh but not the second one and i have tried with a timer up to 180 seconds.

function CreateServer( SIP, GUSER, SPORT ) {
var CDir = "";
var RPassword = generator.generate({
    length: 10,
    numbers: true
});
var ssh = new SSH({
    host: SIP,
    user: 'root',
    key: fs.readFileSync('serverkey.ppk')
    //pass: 'password'
});

ssh.exec('if test -d /home/' + GUSER + '; then echo "exist"; fi', {
    out: function(stdout) {
        CDir = stdout;
        //console.log(stdout);
    }
}).start();
setTimeout(function(){
    if(CDir == DMkdir) {
        console.log("User on machine not adding......");
        ssh.exec('chown -R ' + GUSER + ':' + GUSER + ' /home/' + GUSER + '; su - ' + GUSER + ' -c "cd /home/' + GUSER + '/steamcmd ; screen -mdSL ' + SIP + ':' + SPORT + ' ./steamcmd.sh +login anonymous +force_install_dir /home/' + GUSER + '/' + SIP + '.' + SPORT + ' +app_update 4020 validate +quit"', {
            out: function(stdout) {
                //console.log(stdout);
            }
        }).start();             
    } else {
        console.log("User not on machine adding......");
        ssh.exec('cd /home/; sudo adduser ' + GUSER + ' --gecos "First Last,RoomNumber,WorkPhone,HomePhone" --disabled-password; echo "' + GUSER + ':' + RPassword + '" | sudo chpasswd; cd /home/' + GUSER + '/; mkdir steamcmd; cd /home/' + GUSER + '/steamcmd; wget https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz; tar -xvzf steamcmd_linux.tar.gz; chown -R ' + GUSER + ':' + GUSER + ' /home/' + GUSER + '; su - ' + GUSER + ' -c "cd /home/' + GUSER + '/steamcmd ; screen -mdSL ' + SIP + ':' + SPORT + ' ./steamcmd.sh +login anonymous +force_install_dir /home/' + GUSER + '/' + SIP + '.' + SPORT + ' +app_update 4020 validate +quit"', {
            out: function(stdout) {
                console.log(stdout);
            }
        }).start();
    }
}, 5 * 1000);
return true;

}



via Raul Gonzales

No comments:

Post a Comment