Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Wednesday, August 31, 2022

A dumb script

 It just does the job and forgive me for not having time to make it better (e.g ..at least putting the pswd in a variable!!!.) ..nah ...Im just too lazy to do it:

###Need sshpass installed , for passing over the password text


#!/bin/sh
SERVERLIST=serverlist.input
RCMD1='uptime'
RCMD2='cat /etc/passwd | grep hacker' 
while read SERVERNAME
 do
    echo "----------------------">>Remote_Exec_report.txt
    echo $SERVERNAME>>Remote_Exec_report.txt
    sshpass -p 'YourPassword'    ssh -o StrictHostKeyChecking=no -n root@$SERVERNAME $RCMD1 >>Remote_Exec_report.txt
    echo "------checking hacker user, no output expected-------">>Remote_Exec_report.txt
    sshpass -p 'YourPassword'    ssh -o StrictHostKeyChecking=no -n root@$SERVERNAME $RCMD2 >>Remote_Exec_report.txt
    echo "------Done checking-------">>Remote_Exec_report.txt
done < "$SERVERLIST"

Friday, September 10, 2021

Shell script with ps and kill

====Gotta love awk and shell!!!!======

# ps -ef | grep sshd
root      152725       1  0 Jul21 ?        00:00:46 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root      408824  152725  0 06:44 ?        00:00:00 sshd: root
root      408949  152725  0 06:44 ?        00:00:00 sshd: root
root      409031  152725  0 06:44 ?        00:00:00 sshd: root
root      409113  152725  0 06:44 ?        00:00:00 sshd: root
root      409194  152725  0 06:44 ?        00:00:00 sshd: root
root      409265  152725  0 06:44 ?        00:00:00 sshd: root
root      409266  152725  0 06:44 ?        00:00:00 sshd: root
root      409407  152725  0 06:44 ?        00:00:00 sshd: root
root      409478  152725  0 06:44 ?        00:00:00 sshd: root
root      409549  152725  0 06:45 ?        00:00:00 sshd: root
root      409630  152725  0 06:45 ?        00:00:00 sshd: root
root      409701  152725  0 06:45 ?        00:00:00 sshd: root
root      409785  152725  0 06:45 ?        00:00:00 sshd: root
root      410949  152725  0 18:56 ?        00:00:00 sshd: root@pts/0
root      411192  411077  0 19:15 pts/0    00:00:00 grep --color=auto sshd

# ps -ef | grep sshd | grep -v -e listener -e color -e pts | awk '{print $2}'

408824
408949
409031
409113
409194
409265
409266
409407
409478
409549
409630
409701
409785

# for pid in $(ps -ef | grep sshd | grep -v -e listener -e color -e pts | awk '{print $2}'); do kill -9  $pid; done

Friday, June 25, 2021

Shell to check process

=== Test if a port is open============
nc -z localhost 6000 || echo "no tunnel open"

====Cron table check================

We can use ps -aux | grep "<our command>" | sed '$ d' to check whether the connection is established or not. Based on this our script could be:

#!/bin/bash
SSH_COMMAND="ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa"
if [[ -z $(ps -aux | grep "$SSH_COMMAND" | sed '$ d') ]]
then exec $SSH_COMMAND
fi

Call this script my_autossh, place it in ~/bin and make it executable. Then run crontab -e and add the following job:

* * * * * $HOME/bin/my_autossh
If you do not want to use Cron, modify the scrip my_autossh in this way:
#!/bin/bash
SSH_COMMAND="ssh user@host -fTN -R 2222:127.0.0.1:22 -i $HOME/.ssh/id_rsa"
while true; do
    if [[ -z $(ps -aux | grep "$SSH_COMMAND" | sed '$ d') ]]
    then eval $SSH_COMMAND
    else sleep 60
    fi
done

And use nohup to push it into the background:
nohup my_autossh >/dev/null 2>&1 &