Sudo: parted: command not found while executing script to format disk - linux

I am trying to create a script to format a newly added disk on a VM in vmware. I want to run the script with a sudouser.
echo "- - -" > /sys/class/scsi_host/host0/scan
echo "scsi add-single-device 0 0 1 0" > /proc/scsi/scsi
dev=/dev/disk/by-path/*-scsi-0:0:1:0
count=0
while [ ! -r ${dev} ]; do
count=$(($count+1))
if [ $count -gt 5 ]; then
echo "New disk for target is not visible by the OS"
exit 1
fi
echo $count
sleep 5
done
dev=$(readlink -f $dev) || exit 1
parted -s -- $dev mklabel msdos
When I run the script as sudo ./mount.sh, the script fails saying "parted: command not found". I added the path for parted in the PATH variable for the sudouser. What am I missing? Where did I go wrong?
I tried adding sudo to each command individually, then the script fails in the while loop. Do I need to add any more permissions for the sudouser? Or is there any implementation error? Am I missing something? Please help me.
I observed that this happens when I try to run any of the commands in /sbin/
sudo ifconfig throws sudo: ifconfig: command not found

Related

How to check from commandline if sudo authentication failed?

I was making a graphical front end to sudo for myself in bash scripting. Using something like gksudo is not an option for me, because the OS that I use doesn't provide one.
I designed it this way-
#!/bin/bash
sudo -S $# <<< $(/usr/lib/gtkdialog/box_passwd "$USER password" "Enter password" 2>/dev/null)
STATUS=$?
if [ $STATUS -eq 127 ]; then
/usr/lib/gtkdialog/box_splash -bg red -fg white -timeout 5 -text "sudo not found!"
exit 1
fi
while [ $STATUS -ne 0 ]; do
sudo -S $# <<< $(/usr/lib/gtkdialog/box_passwd "$USER password" "Authentication failed! Please retry" 2>/dev/null)
export STATUS=$?
done
(Note that /usr/lib/gtkdialog/box_passwd and /usr/lib/gtkdialog/box_splash are specific to this OS).
It works if I remove the while loop. But then the program would ask for the password only once, even if it was wrong. But currently, the while loop many a times doesn't end, because even if authentication is correct, sudo may also return 1 if the program it runs is not found or the program returns 1.
So how can I make this code run the while loop only if authentication for sudo is failed, and not other times when sudo returns 1?
EDIT: Just to make it clear, /usr/lib/gtkdialog/box_passwd is used here as graphical program to prompt the user for his/her password, which /usr/lib/gtkdialog/box_passwd passes to its stdout (prints to its stdout). /usr/lib/gtkdialog/box_splash is a program which just creates a new window and show some specific message
In some OSes, the man page for sudo specifies which error messages would come from sudo. If your OS does that, you can grep stderr for these messages.
Otherwise, sudo isn't very helpful in that department, but if your usage of sudo is simple (no fancy configuration in /etc/sudoers, and no specific commands are configured to be allowed by sudo), then you can just do something like:
#!/bin/bash
sudo -S true <<< $(/usr/lib/gtkdialog/box_passwd "$USER password" "Enter password" 2>/dev/null)
export STATUS=$?
if [ $STATUS -eq 127 ]; then
/usr/lib/gtkdialog/box_splash -bg red -fg white -timeout 5 -text "sudo not found!"
exit 1
fi
while [ $STATUS -ne 0 ]; do
sudo -S true <<< $(/usr/lib/gtkdialog/box_passwd "$USER password" "Authentication failed! Please retry" 2>/dev/null)
STATUS=$?
done
sudo "$#"
Namely, you don't run the command, you just run a dummy command (true in this case, which never fails).
In the default configuration, sudo remembers you for a few minutes.
So if you run sudo "$#" immediately after sudo true succeeded, sudo wouldn't ask you for a password in order to run the actual command.

SSH Remote command exit code

I know there are lots of discussions about it but i need you help with ssh remote command exit codes. I have that code:
(scan is a script which scans for viruses in the given file)
for i in $FILES
do
RET_CODE=$(ssh $SSH_OPT $HOST "scan $i; echo $?")
if [ $? -eq 0 ]; then
SOME_CODE
The scan works and it returns either 0 or (1 for errors) or 2 if a virus is found. But somehow my return code is always 0. Even, if i scan a virus.
Here is set -x output:
++ ssh -i /home/USER/.ssh/id host 'scan Downloads/eicar.com; echo 0'
+ RET_CODE='File Downloads/eicar.com: VIRUS: Virus found.
code of the Eicar-Test-Signature virus
0'
Here is the Output if i run those commands on the "remote" machine without ssh:
[user#ws ~]$ scan eicar.com; echo $?
File eicar.com: VIRUS: Virus found.
code of the Eicar-Test-Signature virus
2
I just want to have the return Code, i dont need all the other output of scan.
!UPDATE!
It seems like, echo is the problem.
The reason your ssh is always returning 0 is because the final echo command is always succeeding! If you want to get the return code from scan, either remove the echo or assign it to a variable and use exit. On my system:
$ ssh host 'false'
$ echo $?
1
$ ssh host 'false; echo $?'
1
$ echo $?
0
$ ssh host 'false; ret=$?; echo $ret; exit $ret'
1
$ echo $?
1
ssh returns the exit status of the entire pipeline that it runs - in this case, that's the exit status of echo $?.
What you want to do is simply use the ssh result directly (since you say that you don't want any of the output):
for i in $FILES
do
if ssh $SSH_OPT $HOST "scan $i >/dev/lull 2>&1"
then
SOME_CODE
If you really feel you must print the return code, that you can do that without affecting the overall result by using an EXIT trap:
for i in $FILES
do
if ssh $SSH_OPT $HOST "trap 'echo \$?' EXIT; scan $i >/dev/lull 2>&1"
then
SOME_CODE
Demo:
$ ssh $host "trap 'echo \$?' EXIT; true"; echo $?
0
0
$ ssh $host "trap 'echo \$?' EXIT; false"; echo $?
1
1
BTW, I recommend you avoid uppercase variable names in your scripts - those are normally used for environment variables that change the behaviour of programs.

Bash script to create virtual clusters isn't working

I am trying to create a script to create virtual clusters on my virtual machine which is a CentOS 7 minimal.
I got a script named cluster
#!/bin/bash
function vc
{
echo
echo -n "Enter project name: "
read platform_name
echo
echo -n "web extension: "
read web_extension
echo
echo -e "The following website will be created"
echo -e "\e[32m Platform:\e[0m\t${platform_name}"
echo -e "\e[32m Extension:\e[0m\t${web_extension}"
echo -e "\e[32m Full URL:\e[0m\t http://www.${platform_name}.${web_extension}"
echo
echo -e "Do you wish to proceed? [Y/n]"
read -p "Are you sure? " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo
echo -e "\e[32m Creating platform \e[0m\t"
else
echo
echo -e "\e[32m Not creating platform \e[0m\t"
fi
}
if [ -n "$(type -t $FUNCTION_NAME)" ] && [ "$(type -t $FUNCTION_NAME)" =
function ];
then $FUNCTION_NAME $2; else help; fi
Then as far as I understood I just have to make it executable
chmod +x cluster
And after this I should make a syslink for it ln -s cluster /bin/cluster
And now I should normally be able to just typ cluster vc in the terminal and it should execute the script but it keeps giving me "command cluster not found"
Am I doing something obviously wrong? Or do I need to use another chmod on it so I can run this?
Symbolic link targets are resolved relative the the symlink location. In your case that means, if you run /bin/cluster it looks for a file named cluster (the target) in the /bin/directory. Either provide a relative path which points to your file or link to an absolute path: ln -s /path/to/cluster /bin/cluster.
Also make sure that the target location is readable and executable by whomever executes the symlink.

script to check script is running and start it, if it’s stopped

I have a script with this name : Run.sh
I run this script with this command :
./run.sh
I don't like stop this script but this script Suddenly stops and need run again.
I need a script to check it , if my run.sh stopped , run it again.
this is run.sh codes:
#!/usr/bin/env bash
install() {
sudo apt-get update
sudo apt-get upgrade
}
if [ "$1" = "install" ]; then
install
else
if [ ! -f ./tg/tgcli ]; then
echo "tg not found"
echo "Run $0 install"
exit 1
fi
#sudo service redis-server restart
#./tg/tgcli -s ./bot/bot.lua -l 1 -E $#
./tg/tgcli -s ./bot/bot.lua $#
fi
And i want run this script at boot (with screen or tmux) if my server restart
i have Ubuntu 16.04 version
Thank you Ljm Dullaart
Can you help me about this ?
You should not need to run the complete bash script again. Changing
./tg/tgcli -s ./bot/bot.lua $#
to
while :; do
./tg/tgcli -s ./bot/bot.lua $#
done
will restart bot.lua everytime it exits.
You can check if your run.sh is running and re-run it if stopped with a single command:
$ if ! pgrep run.sh ;then /path/to/run.sh;fi
If script runs pgrep will return exit status 0 = success and will print the pid of run.sh
If script does not run pgrep will return exit status 1 and then script will be called.
You can also use pgrep inst.sh >/dev/null to "mute" pgrep in case script is running.

How to tell the status of a Linux Daemon

We have a Linux Daemon in c and a bash script to start it. The daemon sometimes fail to start because of some configuration file errors but the script reports the daemon was started successfully. A snippet of the script is shown as below, could someone tell me what's wrong with the script?
...
case "$1" in
start)
echo -n "Starting Demo Daemon: "
sudo -u demouser env DEMO_HOME=$DEMO_HOME /usr/local/demouser/bin/democtl startup > /dev/null 2> /dev/null
if [ "$?" = "0" ]; then
echo_success
else
echo_failure
fi
echo
;;
...
Thanks!
I feel there is nothing wrong with the script,it is the reponsibility of daemon to return non zero exit status if failed to start properly and based on those the script will display the message.(which i think it does)
You can add following line in your script to get running status of your Linux Daemon
status=`ps -aef |grep "\/usr\/local\/demouser\/bin\/democtl" |grep -v grep|wc -l`
if [ "$status" = "1" ]; then
echo_success
else
echo_failure
fi

Resources