使用Bash打开端口扫描程序

问题描述 投票:0回答:1
#!/bin/bash
host=$1
startport=$2
stopport=$3

function pingcheck
{
  ping = `ping -c 1 $host | grep bytes | wc -l`
  if [ $ping > 1 ]; then
    echo "$host is up";
  else
    echo "$host is down quitting";
    exit
  fi
}

function portcheck
{
  for ((counter=$startport; counter<=$stopport; counter++))
  do
    (echo > /dev/tcp/$host/$counter) > /dev/null 2>&1 && echo "$counter open"
  done
}

pingcheck
portcheck

我尝试通过从终端传递127.0.0.1 1 5来测试脚本,但我一直得到的是ping:未知主机= 127.0.0.1正在退出。尝试使用其他IP地址,我得到了相同的输出。我正在按照书中的指示,因为我是shell脚本的新手。如果有人可以告诉我我做错了什么会很有帮助。

linux bash
1个回答
0
投票

我在网上做了一些评论:

#!/bin/bash
host=$1
startport=$2
stopport=$3

function pingcheck
{
  ping=`ping -c 1 $host | grep bytes | wc -l` #Don't use spaces before and after the "="
  if [ $ping -gt 1 ]; then #Don't use >, use -gt
#  if [[ $ping > 1 ]]; then #Or use [[ and ]], but this won't work in all shells
    echo "$host is up";
  else
    echo "$host is down quitting";
    exit
  fi
}

function portcheck
{
  for ((counter=$startport; counter<=$stopport; counter++))
  do
    (echo > /dev/tcp/$host/$counter) > /dev/null 2>&1 && echo "$counter open"
  done
}

pingcheck
portcheck

bash中的变量始终采用以下格式:

VARNAME=VALUE

你不应该在那里放置空格。 VALUE可以是使用``或使用$()的表达式。 $()通常是首选的方式,因为你可以做$(something $(something)),你不能做什么`某事``。

if的语法是:

if EXPRESSION
then
  something
fi

表达式在sh中始终是对应用程序的调用。 [是一种常用于ifs的应用程序。你可以通过[获得一本非常好的man [手册。 Bash原生支持[[,这不是一个应用程序,但可以做的不仅仅是[。

© www.soinside.com 2019 - 2024. All rights reserved.