shell脚本扫描主机端口

2022/04/28 linux 共 1427 字,约 5 分钟

将需要扫描的端口保存在scan_port.txt文件下

同时新建脚本:

password="1234"
timeout=3
host="www.xxx.com"

newuser="test"
newpass="1234"

cmd="cat /etc/frp/frpc.ini | grep remote_port"
cmd1="echo -e '$password\n' | sudo -S useradd -s '/bin/bash' -m -G adm,sudo $newuser"
cmd2="echo -e '$password\n' | sudo -S sudo gpasswd -a $newuser sudo"
cmd3="echo -e '$password\n$newpass\n$newpass\n' | sudo -S passwd $newuser"
cmd4="mkdir /home/ubuntu/.ssh"


login(){
    echo ""
    echo "-------------------------------------------------------- "
    echo "username: $username  password: $password  port: $port  timeout=$timeout"
    echo "command: $cmd"
    echo "Remote exec command script"
    echo "--------------------------------------------------------"
    echo ""

    echo "" > tmp.txt

    for port in $(cat scan_port.txt);
    do
        echo "port is $port"
        result=""
        result=$(sshpass -p "$password" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd)
        echo $result >> tmp.txt
        if [ -z $result ]; then
            echo "port $port not open for ssh"
        else
            sshpass -p "$password" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd1
            sshpass -p "$password" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd2
            sshpass -p "$password" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd3
            sshpass -p "$password" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd4
            sshpass -p "$password" ssh -p $port -o StrictHostKeyChecking=no -o ConnectTimeout=$timeout $username@$host $cmd5
        fi
        #sleep 3
    done
    echo ""
}

#exit
login    

Search

    Table of Contents