首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cron作业创建空文件

Cron作业创建空文件
EN

Stack Overflow用户
提问于 2022-07-31 02:41:13
回答 1查看 199关注 0票数 0

我想说的是,我是个新手,两周前就开始学shell脚本了。

嘿,伙计们,我需要帮助,希望有人能给我指明正确的方向。我有一个脚本,当我从命令行运行它时,但是每次我使用crontab运行它时,输出都是一些空文件。有人知道为什么吗?下面的代码就是这样

代码语言:javascript
复制
#!/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
EN

回答 1

Stack Overflow用户

发布于 2022-07-31 05:27:45

你在用

代码语言:javascript
复制
network-scan | grep

而不传递任何参数。因此,网络扫描功能总是使用

代码语言:javascript
复制
sudo nmap -sn 192.168.1.0-255

当您从命令行运行它时,您是否传递任何参数? debugging.

在cron和命令行执行时,

  1. 回显$IPADDRESS在脚本中。

代码语言:javascript
复制
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操作符,以了解其移除所需输出的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73180335

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档