Trying to run a shell script with node.js using sudo -S - node.js

I'm trying to run a shell script with sudo access which executes a python script using Node.js on Mac. This was a big help How to run shell script file using nodejs? but I've gotten stuck:
Shell script:
#!/bin/sh
sudo -S python [pathToPythonScript]/someScripts.py
Node.js code:
const shell = require('shelljs');
shell.exec("./[pathToShellScript]");
When I run this in WebStorm, I am prompted to enter my password. I do so, but nothing happens; the script isn't executed. Can anybody help me with this?

The issue appears to be that you are not executed the node script with root permissions: sudo node filename.js.
Furthermore, it could be that the child process you are spawning with the shelljs module does not inherit the effective UID of the parent node process.
If sudo node filename.js does not solve your problem, I would recommend using the execa package instead of shelljs for better debugging and instrumentation of the spawned child-process: https://www.npmjs.com/package/execa
You can define the uid, gid of the child process as well as the stdin stream, which you could set to ignore such that no interactive prompts can occur (the sudo command would recognize that no stdin is provided and therefore not open an interactive prompt).

Related

Can't execute global Node.js module from Bash shell script (Truffle)?

I installed Truffle, the Ethereum development toolkit, on my Ubuntu 14.04 PC. I can execute it easily from a terminal window by simply typing "truffle". However, when I try to execute Truffle from a Bash shell script, I get the following error:
ide-do-truffle.sh: line 3: truffle: command not found
The line inside the shell script is just:
truffle compile --network local
How can I execute Truffle from within a shell script?
If someone can also explain what goes on behind the scenes when you execute a globally installed Node.JS package like Truffle, that would be helpful too.
Its possible the PATH in your terminal window is not the same as the PATH that your shell script sees.
Try echo "$PATH" in both your terminal window and in your script just before your truffle line in your script, and compare the two. If there is a difference, then the problem is with the PATH in your shell script.

How can I use RobotFramework to sudo in a ssh session where the sudo command doesn't require a password?

I trying to automate a program where I log into a ssh session with a username and password then I'm required to do a sudo su - "username" with no password. I'm able to log into the ssh session with out any problem but I am not able to get the sudo command to work. Looking at the Robot docs you should be able to put none for the password if there is no password but that just seems to cause the program to hang as if it's waiting for the password. The command I tried using is this.
Execute Command su - "username" sudo=True sudo_password=None
I'm not sure if this is a bug with Robot Framework or it simply requires that password to work correctly. I did write a interactive ssh using C where somebody could manually enter the sudo su command then the automation would continue. If anybody has any suggestions for the robot command, or how to possibly execute this using paramiko then I can import that library. Other thought I had was if I can get the C program to work with the robotframework then I can use that. Thank you for any suggestions.
I don't think it's a bug in Robot Framework. It's how the shell in Linux works and has nothing to do with the sudo_password=None.
I'm far from an expert explaining these stuff.
However, Robot Framework SSHLibrary use paramiko exec_command to execute the command.
And as described in the documentation, Execute Command will always be executed in a new shell that is then closed.
SSHLibrary Doc - Execute Command
Let's try run this manually in the command line.
ssh <login_user>#<server_ip> sudo su - <the_su_username>
The command will not give any response and it hangs, just as it will do in Robot Framework and also Paramiko.
If we instead execute a command that will give a response it will work.
ssh <login_user>#<server_ip> sudo ls -ltr
This will work in robot framework as well if you try.
Execute Command ls -ltr sudo=True sudo_password=None
As you say, it works if you use a interactive implementation in C.
I recommend you try implement using an interactive shell in Robot Framework as well.
That will run multiple commands in same shell.
You can find more details here how to use Write and Read.

Running two shell commands using Python 3.6 on Ubuntu 18.02

In order to get some software running I need to 1. Run a script that will execute a remote license manage, 2. Execute a shell script to start the software. I can do this by opening a command window in the directory with the rlm , and then type ./rlm to run the Linux executable. Then I can go into the directory that contains the shell script, open a terminal in that location and run ./myshell.sh. This opens the GUI for my software.
I would like to run these steps using a single Python script. I have tried:
#change the working directory...
os.chdir(r'/opt/mysoftwarelocation')
#confirm location change...
print(os.getcwd() )
#run ./rlm...
os.system('./rlm')
At this point I can see from a python terminal that the rlm is running.
I would then like to run the code below to run the shell script...
os.chdir(r'/opt/mysoftwarelocation/sumsubdirectory')
print(os.getcwd() )
os.system('./some.sh')
Unfortunately, after os.system('./rlm') finishes the script stalls and will not execute further and without an errors.
How to I get the second part of my script to run within a single Python script?
Have you tried to run the rlm command in the background?
subprocess module gives a nice interface for that

Specify which shell Yarn uses for running scripts

My package.json has a script in it like this:
"buildTslint": "node_modules/typescript/bin/tsc node_modules/awesomeLibrary_node_tslint/{,helpers/}*.ts",
Note the {,helpers/}*.ts part, this is called Brace Expansion and is only possible in bash, not sh.
When running yarn buildTslint I get the following output:
# yarn buildTslint
yarn buildTslint v0.22.0
$ node_modules/typescript/bin/tsc node_modules/awesomeLibrary_node_tslint/{,helpers/}*.ts
error TS6053: File 'node_modules/awesomeLibrary_node_tslint/{,helpers/}*.ts' not found.
error Command failed with exit code 2.
It seems that Yarn uses sh to execute these scripts, but I'd like to use bash for this, to be able to use brace expansion.
yarn version 1.19 added support for a new config parameter script-shell. You can now do the following:
yarn config set script-shell /bin/bash
It may launch the command using system function see also man 3 system.
To see which system call is used :
strace yarn ...
system uses fork+exec+wait and the exec family functions uses shell /bin/sh
to use bash the command can be changed to bash -c 'command ..'
Yarn doesn't yet provide a way to configure the default shell used for running scripts, see: https://github.com/yarnpkg/yarn/issues/4248
However, since yarn uses Node for spawning its processes, you can work around that by changing the default shell that Node itself uses.
On Ubuntu, if you have root permissions, you can do this by changing the symlink /bin/sh to point to something other than dash:
sudo ln -sf /bin/bash /bin/sh
In Git-bash in Windows, you can change the COMSPEC environment variable to something other than C:\Windows\system32\cmd.exe, but I haven't gotten that to work for me.
See also:
Force node to use git bash on windows

Node: Move the current directory of the interactive shell

I'm writing a little CLI tool for Node.js. There are some situations where I'd like to run modify the current directory of the shell that runs my program:
some/location$ myProg moveToSomewhere
some/other/location$
child_process can't help here, since its commands run in a different shell.
Is there a way to do this?

Resources