我正试图收集我的IP地址和子网位(如192.168.2.17/24)传递给fing以及名称以扫描网络并创建一组输出文件。我编写了一个工作脚本,其中包含地址/ len和作业名称。我最终希望能够扩展shell脚本自动拉地址/ len,这样我只需要输入作业名称。如果有帮助,我使用Mac。为什么我的bash函数将参数作为字符串处理?
fing.sh(这工作!)
#Create a fing profile and scan
mkdir $2
fing -n $1 -r 3 -d false --session $2/persist.fing \
-o table,html,$2/fing.html -o table,csv,$2/fing.csv \
-o table,xml,$2/fing.xml -o table,json,$2/fing.json
test.sh
#!/bin/bash
#Variables
ipaddr=ip
hexmask=netmask
testmask=255.255.252.0
#thing=0
的IP地址和子网掩码职能的工作;至少他们似乎是。
#Functions
ip()
{
ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'
}
netmask()
{
ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $4}'
}
的功能,mask2cidr和hex2decip为个别脚本。
mask2cidr() {
#Source:
#http://www.linuxquestions.org/questions/programming-9/bash-cidr-calculator-646701/#post3173472
#Convert dotted decimal subnet mask to cidr format
nbits=0
IFS=.
for dec in $1 ; do
case $dec in
255) let nbits+=8;;
254) let nbits+=7;;
252) let nbits+=6;;
248) let nbits+=5;;
240) let nbits+=4;;
224) let nbits+=3;;
192) let nbits+=2;;
128) let nbits+=1;;
0);;
*) echo "Error: $dec is not recognised"; exit 1
esac
done
echo "$nbits"
}
hex2decip()
{
#Source:
#https://forums.freebsd.org/threads/ifconfig-display-non-hex-netmasks.2834/#post-86216
#Converts hex formatted subnet to dotted decimal format
if [ ! "$1" ] ; then
echo
echo "$MyName - converts an IP address in hexadecimal to dotted decimal"
echo "Usage: $MyName <hex_address>"
echo
exit 1
fi
echo $1 | sed 's/0x// ; s/../& /g' | tr [:lower:] [:upper:] | while read B1 B2 B3 B4 ; do
echo "ibase=16;$B1;$B2;$B3;$B4" | bc | tr '\n' . | sed 's/\.$//'
done
}
IPADDR和hexmask输出作为预期
${ipaddr}
${hexmask}
对于hex2decip,hexmask似乎通过 “子网掩码”,而不是掩码函数的结果。
hex2decip $hexmask
exit 0
./test.sh OUTPUT
10.0.180.14
0xffffff00
(standard_in) 1: illegal character: N
(standard_in) 1: illegal character: T
(standard_in) 1: illegal character: M
(standard_in) 1: illegal character: S
(standard_in) 1: illegal character: K
为什么'$ hexmask'会调用任何东西?你需要使用反引号或'$()'语法。 –