具有可变长度的数组元素的格式输出

时间:2018-05-19 11:17:24

标签: arrays bash format output

我有这个数组,输出到file.txt: 第一个数组包含子网中的所有IP(192.168.1.0,192.168.1.1,ecc) 如果该主机已启动,则第二个数组包含1,如果向下,则包含0,以便获得如下矩阵:

#!/bin/bash
# written by Cosimo Colaci


# Declarations
ROOT_UID=0
E_NOROOT=65
declare -a iplist

echo "Reading hosts status..."


# Must run as root.
# if [ "$UID" -ne "$ROOT_UID" ]
# then
#  echo "You need to run this script as root. Exiting..."
#  exit $E_NOROOT
# fi



# Find Gateway IP and Netmask
ip_gateway=$(route | tail -1 | awk '{print $1}')
netmask=$(route | tail -1 | awk '{print $3}')
# echo "$netmask"           FOR DEBUG



# Find subnet
OLDIFS="$IFS"
IFS=.
read -r i1 i2 i3 i4 <<< "$ip_gateway"
read -r m1 m2 m3 m4 <<< "$netmask"
subnet_ip=$(printf "%d.%d.%d.%d\n" "$((i1 & m1))" "$((i2 & m2))" "$((i3 & m3))" "$((ii4 & m4))")
# echo "$subnet_ip"         FOR DEBUG
IFS="$OLDIFS"



# Express netmask in CIDR notation, to know how many hosts to scan
cidr_by_netmask() {
    #function returns prefix for given netmask in arg1
    bits=0
    for octet in $(echo $1| sed 's/\./ /g'); do 
         binbits=$(echo "obase=2; ibase=10; ${octet}"| bc | sed 's/0//g') 
         let bits+=${#binbits}
    done
    echo "/${bits}"
}

suffix=$(cidr_by_netmask $netmask)
# echo "$suffix"            FOR DEBUG



# First raw of matrix file: all IPs in subnet
iplist=( $(nmap -sL $subnet_ip$suffix | grep "Nmap scan report" | awk '{print $NF}') )

# echo "${iplist[@]}"           FOR DEBUG

let i=1
let j=0
while [ $j -lt ${#iplist[@]} ]
do
  raw1[$i]=${iplist[$j]}
  ((i++))
  ((j++))
done
raw1[0]="#IP/TIME"

for value in ${raw1[@]}
do
  echo -en "$value\t"
done >ip-time_matrix.txt
echo -e "\n" >>ip-time_matrix.txt


# Other raws
let j=1
let k=0

export j
export k

echo "Scanning hosts every 10s..." 
echo "Hit CTRL-C to stop and check file \"ip-time_matrix.txt\""

while true
do
  nmap -sP $subnet_ip$suffix &>temp_esame95a.txt
  raw2[0]="$(date +%T)"
  for ip in ${iplist[@]}
  do
    if grep "$ip" temp_esame95a.txt &>/dev/null
    then
      raw2[$j]="---1---"
    else
      raw2[$j]="---0---"
    fi
    ((j++))
  done
  for value in ${raw2[@]}
  do
    echo -en "$value\t"
  done >>ip-time_matrix.txt
  echo -e "\n" >>ip-time_matrix.txt
  sleep 10
  j=1
done

我如何分配这些元素?我希望位状态(0或1)位于IP地址的中心。 考虑到IP地址可能会有所不同,具体取决于机器,例如它可能 71.5.0.0

如果您需要更多信息,我会将所有脚本放入:

width= '{{width}}px'

2 个答案:

答案 0 :(得分:0)

在评论中建议printf "%15.15s %s" "${ip}" ${bitstatus}。我应该添加一个\n,printf“%15.15s%s \ n”“$ {ip}”$ {bitstatus}。
OP反应过来,它解决了他的问题。

答案 1 :(得分:0)

对于数组:

ips=(192.168.1.0 127.001.0.1 192.168.1.1 192.169.1.1)                    
raw=(1 1 0 1)                                                            

您可以使用printfcut命令,

printf "%15s" "${ips[@]}" | cut -c5-                                               
printf "%15s" "${raw[@]}" | cut -c5-

对齐输出:

192.168.1.0    127.001.0.1    192.168.1.1    192.169.1.1             
          1              1              0              1