NSTask prompt user for password when script uses sudo - cocoa

I have a Cocoa Application that is using NSTask to run a shell script that receives variables upon task launch. These variables are dynamic, and the command that is run will vary based on these variables. (they are flags, directory paths, etc) The command is rsync and it HAS to be run with sudo because it is interacting with files that the current user will not have ownership of.
My question is this: How do I prompt the user for an Administrator username and password when the script would normally prompt for them if run in terminal? I CANNOT use a variable to store the password.
Is there some sort of way to use an input pipe? If so, how does NSTask know when to prompt for the username and password, and how would you send input through the input pipe to the script without using a variable?

Related

How do I embed an expect script within a bash script so the shell I open doesn't close after the expect script finishes?

I've been writing Bash scripts that work with a database lately.
To access the database, I need to ssh into a DiskStation (requires password) and then sudo a docker command (requires password) to access the container that the database is in. Only then can I execute and test out my scripts.
I wrote an expect script that automates this process and I want to embed it in my Bash scripts, but the only problem is the shell closes as soon as the expect script finishes executing.
Does anybody know how to work around this? I attached a photo with specific info removed. Bash script with embedded expect script

Run bash command from slack

Can I create a slack bot or a custom command to run a bash command or script from slack?
You can use Slack Remote Terminal to run a bash command or script (; separated) from slack, also you will get alerts about large tasks.
You can't execute a bash command or script directly from a Slack integration, but you could do so indirectly. For example, you could create a slash command that executes a command URL on a host that you control. When that command URL is executed, your host can then run whatever commands or sequences you want on that host.
But it would be very difficult, but not necessarily impossible, to ultimately execute a script on your local machine.

How to prevent PuTTY shell from auto-exit after executing command from batch file in Windows?

I have written a batch file like this:
Start putty.exe -ssh 172.17.0.52 -l root -m dummy.txt
Then in dummy.text I have written this command:
avahi-daemon --no-drop-root -D
export XVHMI_USERCONFIG_PATH=/home/UserProfileConfig
export XDG_RUNTIME_DIR=/tmp
cd /opt/bosch/airis/bin
When I run the .bat file, PuTTY starts, commands execute (hopefully, not sure) and it exits.
How to keep that window open?
I have googled for the same, but no solid help. I read on stack overflow itself that we need to define something in txt file, but what and most importantly how?
The SSH session closes (and PuTTY with it) as soon as the command finishes. Normally the "command" is shell. As you have overridden this default "command" and yet you want to run the shell nevertheless, you have to explicitly execute the shell yourself:
avahi-daemon ... ; /bin/bash
Also as use of -m switch implies a non-interactive terminal, you probably want to force an interactive terminal back using -t switch.
Though, I'm not really sure if you want to execute shell or if you just want to see your command output. If the latter, did you consider using plink? It's console terminal client from PuTTY package. Being console application, it inherits console of parent batch file, and you can pause the batch console from closing using pause command, if needed.
Another option (both for PuTTY and plink) is to pause on remote side. E.g. Using read command.
avahi-daemon ... ; read
As suggested by Martin I tried this step:
putty.exe -ssh 172.17.0.52 -l root -m dummy.txt -t
added /bin/bash at the end of commands in dummy.txt
It worked for me. Please note, you have to follow both the steps as mentioned above.
This way you can keep the session alive and can manually execute further commands.

Running a script after user logs in

How could a script wait for the login process to complete before running a command in shell script in Mac OS X?
I have tried wait and sleep commands, but that doesn't seem to stop the script running under the root that owns the login process.
I want the script run after the user logs in.
There are 2 easy(ier) options:
Create a Login Item. To see an example, click here.
Use launchd. You don't have to install anything. All you need to do is to create a configuration file to tell launchd what to do, and save it (with proper permissions) on a specific directory that's read by launchd.

Avoid interactive mode in shell script

There is an interactive shell console, I can get into it, run specific set of commands inside the console and exit from it.
Now I want to write a bash script that connects to an interactive shell console and runs my commands silently, exits at the end without any interaction. This means I want to have everything automated in a non-interactive way. Any ideas how can I achieve this?
I am trying something like, say, blabla shell is the interactive console here, it always bring me to the interactive mode :(
/usr/bin/blabla shell << EOF
do A,
do B,
do C
quit
EOF
I have a long/specific version of this question can be found here ->
Configure flume in shell/bash script - avoid interactive flume shell console
Closing stdin should do the trick:
exec <&-
The expect command if your friend. It can emulate interactive communication with other commands even in very sophisticated way.
From man expect:
Expect is a program that "talks" to other interactive programs according to a script.
You can try putting the commands you would input in the interactive prompt into a file, then run the command like:
command < file
Maybe the Secure SHell, ssh does what you need. It requires that the "remote" machine is configured as an SSH server. I use it regularly to run commands on other hosts, such as
ssh user#host command

Resources