need help to edit the sudoer on solaris through script - shell

When i am trying in solaris its throughing the below error
# cat /usr/local/etc/sudoers |grep -i unix
unix ALL=(ALL) NOPASSWD: ALL
root on test:
sed -i -e 's/^\s*(unix\sALL=(ALL)\sNOPASSWD:\s*ALL)/#\1/' /usr/local/etc/sudoers
sed: illegal option -- i
root on test:</tmp>
i have tried this also its also not working
root on test: </tmp>
$ sudo perl -pi -e 's/^\s*\(unix\s*ALL=(ALL)\s*NOPASSWD:\s*ALL\)/#\1/' /usr/local/etc/sudoers
root on test: </tmp>
cat /usr/local/etc/sudoers |grep -i unix
unix ALL=(ALL) NOPASSWD: ALL
sed -i -e 's/^\s*\(unix\s*ALL=(ALL)\s*NOPASSWD:\s*ALL\)/#\1/' /usr/local/etc/sudoers but the same script is working on linux

Related

Why can't I use 'sudo su' within a shell script? How to make a shell script run with sudo automatically

I cannot figure out what's wrong with my bash script. When I run it in terminal, command by command run every command separately in terminal, it works.
#!/bin/bash
sudo su <<EOF
mkdir /home/ubuntu/backup/
cp "$(ls -t /usr/lib/unifi/data/backup/autobackup | head -1)" /home/ubuntu/backup/
curl --insecure --user root:password -T "$(ls -t /home/ubuntu/backup/ | head -1)" sftp://vps2.duckdns.org:/root/backup.unf
EOF
However, when I run the above bash script, give me plenty of erros
bash -v test.sh
#!/bin/bash
sudo su <<EOF
mkdir /home/ubuntu/backup/
cp "$(ls -t /usr/lib/unifi/data/backup/autobackup | head -1)" /home/ubuntu/backup/
curl --insecure --user root:password -T "$(ls -t /home/ubuntu/backup/ | head -1)" sftp://vps2.duckdns.org:/root/backup.unf
EOF
ls: cannot access '/usr/lib/unifi/data/backup/autobackup': Permission denied
ls: cannot access '/home/ubuntu/backup/': No such file or directory
cp: cannot stat '': No such file or directory
curl: (78) Could not open remote file for reading: SFTP server: No such file
Any help will be very much appreciated!
TIA
It's trying to execute the command substitutions in the original shell, which runs with the regular user's permissions. They need to be executed by su. Quote the EOF token to prevent expansions in the here-document.
sudo su <<'EOF'

Drop from root to user preserving ALL environment variables

The following bash command works to drop down to user privileges, and preserve an environment for the most part:
root#machine:/root# DOLPHIN=1 sudo -E -u someuser bash -c 'echo $DOLPHIN'
1
However, this does not work for all variables, such as PATH, and LD_LIBRARY_PATH:
root#machine:/root# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
root#machine:/root# sudo -E -u someuser bash -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
Notice the PATH is different ^
Why is this happening?
Must be some bash mechanics I don't understand...
Looks like this is a workable option:
root#machine:/root# sudo PATH=$PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH -E -u someuser bash -c 'echo $PATH'
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

awk quoting program text

if I run:
awk '/-jar org.eclipse.osgi.jar --launcher.suppressE/ {print "-Dcom.abc.service.gw.enableValidation=false \\"}1' rest_gw.sh >> new_gw.sh
the command inserts a new line above line -jar org.eclipse.osgi.jar --launcher.suppressE
-Djava.security.properties=/var/vcap/packages/helpers/data.properties \
-Dcom.abc.service.gw.enableValidation=false \ <<<<<<<<<<<<<<<<<<<<<<<<<
-jar org.eclipse.osgi.jar --launcher.suppressErrors -consoleLog &
If I run the following command, it gives: awk: line 1: runaway string constant "-Dcom.abc. ...
bosh -d test-105 ssh service/0 -c 'sudo /usr/bin/awk "/-jar org.eclipse.osgi.jar --launcher.suppressErrors/ {print \"-Dcom.abc.service.gw.enableValidation=false \\\"}1" /var/vcap/data/jobs/gw_rest/*/target/gw.sh >> /tmp/new_gw.sh'
Tried:
bosh -d test-105 ssh service/0 -c 'sudo /usr/bin/awk '\'/-jar org.eclipse.osgi.jar --launcher.suppressErrors/ {print \"-Dcom.abc.service.gw.enableValidation=false \\\"}1\' /var/vcap/data/jobs/gw_rest/*/target/gw.sh >> /tmp/new_gw.sh'
no luck so far. can someone suggest the correct way to do it? TYA!
EDIT:
sed command as recommended:
bosh -d test-105 ssh service/0 -c 'sudo sed '/-jar org.eclipse.osgi.jar --launcher.suppressE/i -Dcom.abc.service.gw.enableValidation=false \\' /var/vcap/data/jobs/gw_rest/*/target/gw.sh >> /tmp/new_gw.sh'
unknown flag launcher.suppressE/i'
This worked finally:
bosh -d test-105 ssh service/0 -c "sudo sed '/-jar org.eclipse.osgi.jar --launcher.suppressErrors -consoleLog/i -Dcom.abc.service.gw.enableValidation=false \\\' /var/vcap/data/jobs/gw_rest/*/target/gw.sh >> /tmp/new_gw.sh"
I strongly suggest use sed command insert your line as:
sed '/-jar org.eclipse.osgi.jar --launcher.suppressE/i -Dcom.abc.service.gw.enableValidation=false \\' rest_gw.sh >> new_gw.sh
It is cleaner and simple.

SSH to server, Sudo su - then run commands in bash [duplicate]

This question already has answers here:
Pass commands as input to another command (su, ssh, sh, etc)
(3 answers)
Closed 6 years ago.
I have the following
#!/bin/bash
USER='scott'
PASS='tiger'
ssh -t $USER#server006.web.com "sudo su - http"
This Works, but I was trying to get it to run a script afterwards, and if I do, using -c or <
The script does a grep like this:
grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F'|' '{print $2}' >> log.log
This also works fine on it's own, but I need to be http to do it.
I cannot SCP the output of the script back to server001 either, so I'm stuck here,
Any ideas would be relay appreciated.
Ben
Try
ssh -t $USER#server006.web.com 'sudo -u http grep -i "Exception:" /opt/local/server/logs/exceptions.log | grep -e "|*-*-*:*:*,*|" | tail -1 | awk -F"|" "{print $2}" >> log.log'
Sudo already runs the command as a different user to there's no need to su again.
Only reason to do sudo su is to have a fast way to start a new shell with another user.
You probably want sudo -u instead of sudo su -:
ssh -t $USER#server006.web.com sudo -u http script
Guess I'm late to the party.
My solution:
ssh -t $USER#server006.web.com "sudo cat /etc/shadow"
and replace cat /etc/shadow with your desired program.

bash script works on bootup, doesn't in terminal

I'm creating a server in Amazon ec2 and passing it a bash script as userdata, which is run when the server first boots. It includes a command to add a line to crontab for a user using the answer given here.
directory="/home/intahwebz/current/tools/amazon/"
command="cd $directory && sh backupSQLToS3.sh"
job="15 1 */2 * * $command"
cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -
This script appears to work fine during bootup as it displays no error messages and the cronjob is installed in the crotab.
However I'd also like the script to run during server upgrades. Attempting to run the script from the command line gives the error:
installCrontab.sh: line 14: syntax error near unexpected token `('
installCrontab.sh: line 14: `cat <(fgrep -i -v "$command" <(crontab -u intahwebz -l)) <(echo "$job") | crontab -u intahwebz -'
What do I need to fix this error?
your approach is working perfectly for me:
$ whoami
test
$ echo $SHELL
/bin/bash
$ command="cd $directory && sh backupSQLToS3.sh"
$ job="15 1 */2 * * $command"
$ crontab -l
$ cat <(fgrep -i -v "$command" <(crontab -u test -l)) <(echo "$job") | crontab -u test -
$ crontab -l
15 1 */2 * * cd && sh backupSQLToS3.sh
I missed to set the "directory" variable but your code works fine for me.
It looks like you are using the bourne shell (/bin/sh) to execute a bash script. Try using bash instead of sh.

Resources