Run shell script as sudo user along with its internal content on unix rhel - shell

I have below script named as sample.sh
#!/bin/bash
echo "inside script file"
echo whoami
echo cftping -v
echo "Ping completed"
I am running it as other user with the help of below command.
sudo -H -u xfbcft bash -c 'bash /data/_temp/sample.sh'
I am getting below output:
xfbcft
/data/_temp/sample.sh: line 4: cftping: command not found
Ping completed
When I sudo to xfbcft directly then I can run 'cftping -v' command but not via above shell script. Could anyone guide me here?

Related

Why my script returns a segmentation error when executing a command via ssh?

I'm writing a bash script that is supposed to run applications with arguments on a remote computer. On the computer, root just needs to write the command:
# ./MyApp -t '{"name":"john"}'
So on my own computer I wrote a script (it is simplified in terms of what is actually to show the problem):
#!/bin/bash
VAR="{\"name\":\"john\"}"
echo "VAR: $VAR"
COMMAND="./MyAPP -t '$VAR'"
echo "COMMAND: $COMMAND"
ssh root#192.168.100.1 "$COMMAND"
Output:
$ ./my_script
VAR: {"name":"john"}
COMMAND: ./MyApp -t '{"name":"john"}'
root#192.168.100.1's password:
Segmentation fault
Why does I get segmentation error?

How can I run a bash script with sudo from another bash script

I am trying to run script a from script b, I didn't create script a and I am not sure if i want to add script as location to PATH.
Making things even more hard to do is the fact I want to run script a with sudo, but not script b so I have whiptail in script b to ask for password and pass along with echo $pass | sudo -S.
my current code:
#!/bin/bash
input=$(whiptail --passwordbox "Enter password" 10 50 3>&1 1>&2 2>&3 )
echo $input | sudo -S bash /home/<username>/multibootusb/makeUSB.sh -e -b "$1" "$2"

Connecting to SSH using a bash script

I am trying to write a bash script to make things faster. Is it not possible to connect to the server with the code below in a bash script? I can't make it work, even though it works in the terminal.
#!/bin/bash -x
echo "Starting connection script"
sh -i /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
What I get when I run is a not found output for each line in the pem file,
$ /home/EC2_KEY_HEHE.pem: 1: /home/EC2_KEY_HEHE.pem: -----BEGIN: not found
$ /home/EC2_KEY_HEHE.pem: 1: /home/EC2_KEY_HEHE.pem: adsnaleAFemasdsdsdnds: not foundMadfdasfdasfnda;vonraada
...
Some debug is needed.
Please change:
ssh -i /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
to:
#!/bin/bash -x
echo "Starting connection script"
ssh -vi /home/EC2_KEY_HEHE.pem ubuntu#ec2-IP.blabla.amazonaws.com
does it produce an idea about the reason ?
you are calling sh which is kind of shell change it to ssh

Execute root command in shell script and change to normal user after a process

I am trying to create a shell script where it uses the root access to install all the dependencies and after completing it, it exits from the root command and continue executing the script as normal user.
This is the test code:
#!/bin/sh
output=$(whoami)
if [ "$(whoami)" != "root" ]
then
su -c "echo \"hi\""
echo $output
//continue doing some installtion
exit
fi
echo $output //this should show the normal username not the root name
#!/bin/sh
su -c 'echo $(whoami)'
echo $(whoami)
When you pass the command with su following with an option -c it runs as root user, so when you want to install any dependencies you can run the following command as shown in above example.

Change host file with script on Mac

I'm trying to append a line to the hosts file on a mac.
The command I'm using is:
sudo echo "192.168.99.100 test" >> /private/etc/hosts
This method does work on windows & linux but on Mac I do not have the permissions to run this even when running it in sudo mode.
Can anybody tell me what I'm doing wrong and how I can fix this?
StefanJanssen
Try echo '192.168.99.100 test' | sudo tee -a /private/etc/hosts.
>> is syntax of the shell itself, which is running as your user. sudo echo "192.168.99.100 test" >> /private/etc/hosts runs echo "192.168.99.100 test" as root and the >> "pipe to file" is run as your user.
tee is an ordinary command you can run as root with sudo which outputs to both stdout and a file, so echo 'line' | sudo tee -a file will do what you want. tee -a will append to the file instead of overwriting it.

Resources