我想说的是,我是个新手,两周前就开始学shell脚本了。
嘿,伙计们,我需要帮助,希望有人能给我指明正确的方向。我有一个脚本,当我从命令行运行它时,但是每次我使用crontab运行它时,输出都是一些空文件。有人知道为什么吗?下面的代码就是这样
#!/bin/bash
#Provide an IP address as an argument to use nmap
#make sure to add the full range with (0-225 or 0/24) at the end
IPADDRESS=$(hostname -I | awk '{print $1}')
network-scan(){
if [ $1 ]
then
sudo nmap -sn $1
else
sudo nmap -sn 192.168.1.0-255
fi
}
#Scan the whole network and only prints the IP addresses minus your own
#Sends the IP addresses to a file
network-scan | grep -i 'Nmap scan report' | \
sed 's/\ /\n/g'|sed 's/(//g'|sed 's/)//g' | \
grep '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | grep -v ${IPADDRESS} > ip_addresses
#Scan the whole network and only prints the MAC addresses
#Sends the MAC addresses to a file
network-scan | grep -i 'MAC Address:' | \
awk '{print $3}' > mac_addresses
#Put the IP and MAC addresses in the same file
paste ip_addresses mac_addresses | \
column -s $'\t' -t > "scan_$(date +%d-%m-%Y_%H:%M:%S)"
#Notify that a file with the IP and MAC addresses has been created on the Desktop
echo "A file containing the results of the scan has been created on the Desktop"
exit 0发布于 2022-07-31 05:27:45
你在用
network-scan | grep而不传递任何参数。因此,网络扫描功能总是使用
sudo nmap -sn 192.168.1.0-255当您从命令行运行它时,您是否传递任何参数? debugging.
在cron和命令行执行时,
network-scan | grep -i 'Nmap scan report' | \
sed 's/\ /\n/g'|sed 's/(//g'|sed 's/)//g' | \
grep '[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*' | grep -v ${IPADDRESS}由于要获得空输出,请验证每个命令,并附加(测试)每个OR操作符,以了解其移除所需输出的位置。
https://stackoverflow.com/questions/73180335
复制相似问题