LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-15-2019, 05:23 AM   #1
lgrech25
LQ Newbie
 
Registered: Feb 2019
Posts: 6

Rep: Reputation: Disabled
Nmap not working when implemented in crontab


I am trying to scan the network every day at a particular time(below example at 12:18) by using cronjobs and then output the IPs in a file. The command works perfectly but does not work if implemented in cronjobs. The following is implemented in /etc/crontab:

18 12 * * * root /usr/bin/nmap -O 10.0.0.10-253 | grep 'Nmap scan' | cut -d' ' -f5> HostList
 
Old 02-15-2019, 06:33 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by lgrech25 View Post
I am trying to scan the network every day at a particular time(below example at 12:18) by using cronjobs and then output the IPs in a file. The command works perfectly but does not work if implemented in cronjobs. The following is implemented in /etc/crontab:
Code:
18 12   * * *   root    /usr/bin/nmap -O 10.0.0.10-253 | grep 'Nmap scan' | cut -d' ' -f5> HostList
What do the errors in your cron log say? And is this job in roots cron, or a user cron?

The syntax is not correct, either. It should be

[Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]

...so six parameters. You have, as a COMMAND, the word 'root', followed by the nmap statement, with lots of other commands after it. Personally, I'd put the whole nmap command with the pipes in a small script file, and let cron execute that.
 
1 members found this post helpful.
Old 02-15-2019, 06:39 AM   #3
lgrech25
LQ Newbie
 
Registered: Feb 2019
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
What do the errors in your cron log say? And is this job in roots cron, or a user cron?

The syntax is not correct, either. It should be

[Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]

...so six parameters. You have, as a COMMAND, the word 'root', followed by the nmap statement, with lots of other commands after it. Personally, I'd put the whole nmap command with the pipes in a small script file, and let cron execute that.
In /etc/crontab there is a row called user. In fact other cronjobs work, only the nmap cronjob does not work. I tried it with a script as well but nothing happens in the cronjob. How can I check the the cron logs?

Last edited by lgrech25; 02-15-2019 at 06:41 AM.
 
Old 02-15-2019, 07:15 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by lgrech25 View Post
In /etc/crontab there is a row called user.
Sorry, no. That is not the crontab file that gets executed, but part of the crontab SYSTEM. If you edited that file...don't, and remove any other stuff you have in there. Cron tabs are typically in /var/spool/cron/xxx (depends on the distro...which you haven't told us)
Quote:
In fact other cronjobs work, only the nmap cronjob does not work. I tried it with a script as well but nothing happens in the cronjob.
If you put the cron job in that file, I'm hardly surprised it doesn't work, especially if you continue to put "root" in front of it. You were given the syntax of the Linux cron statements; either follow them or you won't get things to work. And what are you doing to put cron jobs in? If you're not running "crontab -e" (as root, for the root cron), then you're not doing it correctly. There is AMPLE documentation about this, how to edit crontabs, and the syntax for them.

You say you tried with a script..how about telling us how you tried it, what you put in the script, and the results??
Quote:
How can I check the the cron logs?
You should have something fairly obvious in your log directory, either marked "cron", "chrony", etc. Depends on the version/distro of Linux, and how you, as the administrator, configured the system. You can also check the output file of any commands.

If you're putting that "root" in, it won't work. If you're putting this in /etc/crontab, it won't work, period.
 
Old 02-15-2019, 07:15 AM   #5
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by lgrech25 View Post
In /etc/crontab there is a row called user. In fact other cronjobs work, only the nmap cronjob does not work. I tried it with a script as well but nothing happens in the cronjob. How can I check the the cron logs?
The crontab entry is still not correct....
Code:
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
Your entry is missing the day of week field (which I'm guessing should be "*").
 
1 members found this post helpful.
Old 02-15-2019, 07:29 AM   #6
lgrech25
LQ Newbie
 
Registered: Feb 2019
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by jpollard View Post
The crontab entry is still not correct....
Code:
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
Your entry is missing the day of week field (which I'm guessing should be "*").
So in my case. How should my command be written? Please

18 12 * * * root /usr/bin/nmap -O 10.0.0.10-253 | grep 'Nmap scan' | cut -d' ' -f5> HostList
 
Old 02-15-2019, 08:43 AM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by lgrech25 View Post
So in my case. How should my command be written? Please

18 12 * * * root /usr/bin/nmap -O 10.0.0.10-253 | grep 'Nmap scan' | cut -d' ' -f5> HostList
Sorry, but are you not reading the replies you're getting??? You've been asked several questions, such as how you're editing the crontab, what version/distro of Linux, etc. You were given the EXACT SYNTAX to use, but seem to be ignoring it. You were asked about the script you said you tried, but don't tell us anything about that, either. If you're not going to answer questions or take the advice given, there's little point in posting a question.

AGAIN, the syntax is [Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]

SIX parameters, period. jpollard even provided a diagram. You are *STILL* putting 'root' in front of the command, which is **NOT VALID** and will not run, for obvious reasons, the main one being there IS no 'root' command.
Code:
18 12 * * * /usr/bin/nmap -O 10.0.0.10-253 | grep 'Nmap scan' | cut -d' ' -f5> HostList
AGAIN: If you put this in /etc/crontab, it WILL NOT WORK. You need to run crontab -e as root/sudo, and put the command in correctly.
 
Old 02-15-2019, 09:09 AM   #8
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
I must admit here that I am confused. I can see nothing fundamentally wrong with the crontab entry.
In my opinion there is no weekday missing and in my own experience adding entries to /etc/crontab does technically work (and must contain the user, in this case root).

I agree 100% with TBOne that it is bad style and should be avoided, but technically it should work.

The initial thought I had when looking at the question was: Where is HostList supposed to end up? I would specifiy this file with an absolute path.
Also: I generally try to avoid long commands with pipes and redirects and such in crontab entries. Write a script that does the job (even if it is a oneliner) to make things easier to debug.
You can than add logging etc. to find out what's going wrong. Also be aware that error output might go to stderr, so if you want to redirect it to a log file you need to do something like:

Code:
command > logfile 2>&1
Also be aware that sometimes the commands you can use in an interactive shell don't work in cron because the PATH variable is different.

Last edited by joe_2000; 02-15-2019 at 09:10 AM.
 
Old 02-15-2019, 09:15 AM   #9
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,635

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by joe_2000 View Post
I must admit here that I am confused. I can see nothing fundamentally wrong with the crontab entry.
In my opinion there is no weekday missing and in my own experience adding entries to /etc/crontab does technically work (and must contain the user, in this case root).

I agree 100% with TBOne that it is bad style and should be avoided, but technically it should work.
...providing that the CRON service it stopped/restarted to pick it up, since adding it there without the use of crontab -e won't do it automatically. This is one of the things that (as you said) is DOABLE, but shouldn't be done. You *CAN* have a service start automatically through the use of script files, etc....but why? Use systemd or init.d as they were intended.
Quote:
The initial thought I had when looking at the question was: Where is HostList supposed to end up? I would specifiy this file with an absolute path.
Also: I generally try to avoid long commands with pipes and redirects and such in crontab entries. Write a script that does the job (even if it is a oneliner) to make things easier to debug. You can than add logging etc. to find out what's going wrong. Also be aware that error output might go to stderr, so if you want to redirect it to a log file you need to do something like:
Code:
command > logfile 2>&1
Also be aware that sometimes the commands you can use in an interactive shell don't work in cron because the PATH variable is different.
Yep...OP was advised to use a script as well, and said they tried it, but didn't provide details.
 
Old 02-15-2019, 09:17 AM   #10
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Small addition for the "user" part: Here is the crontab of the system I am posting from. (I am not sure if it was edited since the install, but it should be mainly debian default content...)

Code:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
 
Old 02-15-2019, 09:20 AM   #11
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by TB0ne View Post
...providing that the CRON service it stopped/restarted to pick it up, since adding it there without the use of crontab -e won't do it automatically.
Hmm, could this be distro-dependant? Just tested here with

Code:
*  *   *  *  *  root    /usr/bin/touch /tmp/foobarlq
And sure enough, the file appeared after the minute...
 
Old 02-15-2019, 09:24 AM   #12
lgrech25
LQ Newbie
 
Registered: Feb 2019
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
Small addition for the "user" part: Here is the crontab of the system I am posting from. (I am not sure if it was edited since the install, but it should be mainly debian default content...)

Code:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user	command
17 *	* * *	root    cd / && run-parts --report /etc/cron.hourly
25 6	* * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6	* * 7	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6	1 * *	root	test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Thank you for your replies guys. Sorry for causing you all this trouble I am new to this stuff. I inserted the following crontab in sudo crontab -e

21 16 * * * /home/admin19/HostDiscovery

In the script I copied the same command:

/usr/bin/nmap -O 10.0.0.10-253 | grep 'Nmap scan' | cut -d' ' -f5> /home/admin19/HostList

However, its still not working

Last edited by lgrech25; 02-15-2019 at 09:31 AM.
 
Old 02-15-2019, 09:30 AM   #13
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Quote:
Originally Posted by lgrech25 View Post
Thank you for your replies guys. Sorry for causing you all this trouble I am new to this stuff. I inserted the following crontab in sudo crontab -e

21 16 * * * /home/admin19/HostDiscovery

In the script I copied the same command:

/usr/bin/nmap -O 10.0.0.10-253 | grep 'Nmap scan' | cut -d' ' -f5> /home/admin19/HostList

However, its still not working
Sorry, my instructions were unprecise. You now have a double redirect in your script.
Remove the redirect from the script and put it into the crontab:
Code:
21 16 * * * /home/admin19/HostDiscovery > /tmp/logfile 2>&1
When the execution time passed post the content of /tmp/logfile.
Do not forget to make the script executable:

Code:
chmod +x /home/admin19/HostDiscovery

Last edited by joe_2000; 02-15-2019 at 09:31 AM. Reason: fix typo
 
Old 02-15-2019, 09:32 AM   #14
lgrech25
LQ Newbie
 
Registered: Feb 2019
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by joe_2000 View Post
Sorry, my instructions were unprecise. You now have a double redirect in your script.
Remove the redirect from the script and put it into the crontab:
Code:
21 16 * * * /home/admin19/HostDiscovery > /tmp/logfile 2>&1
When the execution time passed post the content of /tmp/logfile.
Do not forget to make the script executable:

Code:
chmod +x /home/admin19/HostDiscovery
Yes the script is executable. In fact it works perfectly fine. Only in crontabs it doesn't work

-rwxrwxrwx 1 admin19 admin19 104 Feb 15 16:22 HostDiscovery
 
Old 02-15-2019, 09:34 AM   #15
mralk3
Slackware Contributor
 
Registered: May 2015
Distribution: Slackware
Posts: 1,900

Rep: Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050Reputation: 1050
Nmap not working when implemented in crontab

did you make the script executable?

Edit: I was too slow.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
port 5432 open nmap online but closed status with firewalld and local nmap scan mtdew3q Linux - Security 6 06-04-2018 06:20 PM
nmap ? how do i do nmap in linux ? command not found abbasakhtar Linux - Newbie 2 01-02-2011 01:08 AM
LXer: Learn how to use nmap, and nmap GUI, a great port scan tool LXer Syndicated Linux News 0 01-03-2008 09:10 AM
nmap increase send delay for nmap 4.20 matters Slackware 1 10-01-2007 11:37 PM
Cant nmap from but can nmap to procfs Linux - General 6 08-01-2006 02:08 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration