LinuxQuestions.org
Review your favorite Linux distribution.
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 07-21-2016, 01:36 AM   #1
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Rep: Reputation: Disabled
Capture system information and provide output in Excel


Experts,

I was tasked to capture system information(s)and provide the output in excel sheet.
Below details need to be captured.
(Sudoers list,Last login session details,Users/group/Home directory info,Account details with password expiry info,/etc/group and Crontab jobs info list)

Below is the code I started with for capturing the last login details.
<QUOTE>
<CODE>
#!/bin/bash
# Sample script to demonstrate the creation of an HTML report using shell scripting
# Web directory
DIR=/tmp
# A little CSS and table layout to make the report look a little nicer
echo "<HTML>
<HEAD>
<style>
.titulo{font-size: 1em; color: white; background:#0863CE; padding: 0.1em 0.2em;}
table
{
border-collapse:collapse;
}
table, td, th
{
border:1px solid black;
}
</style>
<meta http-equiv='Content-Type' content='text/xls; charset=UTF-8' />
</HEAD>
<BODY>" > $DIR/report.xls
# View hostname and insert it at the top of the xls body
HOST=$(hostname)
echo "Host : <strong>$HOST</strong><br>
Date : <strong>$(date)</strong><br><br>

<table border='1'>
<tr><th class='titulo'>Last Login Session Details</td>
<tr><th class='titulo'>Username</td>
<th class='titulo'>Port</td>
<th class='titulo'>From</td>
<th class='titulo'>Latest</td>
</tr>" >> $DIR/report.xls
# Read the output of df -h line by line
while read line; do
echo "<tr><td align='left'>" >> $DIR/report.xls
echo $line | awk '{print $1}' >> $DIR/report.xls
echo "</td><td align='left'>" >> $DIR/report.xls
echo $line | awk '{print $2}' >> $DIR/report.xls
echo "</td><td align='left'>" >> $DIR/report.xls
echo $line | awk '{print $3}' >> $DIR/report.xls
echo "</td><td align='left'>" >> $DIR/report.xls
echo $line | awk '{print $5" " $6" "$9" "$7}' >> $DIR/report.xls
echo "</td></tr>" >> $DIR/report.xls
done < <(lastlog |grep -vi Username |grep -vi **Never)
echo "</table></BODY></HTML>" >> $DIR/report.xls

</CODE>
</QUOTE>
Error: In the o/p some default users will not be having the details of from session. Can anyone please help me how to change the code for checking the value of it..thanks

Last edited by Mitiraj; 07-21-2016 at 01:39 AM.
 
Old 07-21-2016, 03:20 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
A couple of things:

1. This is how [code][/code] tags are written

2. If you simply want Excel to read the data, why not just create a simple csv file

3. Please provide examples of the desired and current output (mask private information as needed)
 
Old 07-21-2016, 04:31 AM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
lshw can do html.
 
Old 07-22-2016, 01:20 AM   #4
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks for your time.User needs to see the o/p in excel file instead of having a csv file.
Below is the current output

Last Login Session Details
Username Port From Latest
root pts/0 Thu 21 17:19:34 +0800
daemon pts/0 Thu 9 20:08:17 +0800
sync pts/0 Thu 9 20:08:47 +0800
operator pts/0 Thu 9 20:08:11 +0800
games pts/0 Thu 9 20:08:00 +0800
nobody pts/0 Thu 9 20:07:53 +0800
sshd pts/0 Thu 9 20:08:25 +0800
oracle pts/1 XXX.XX.XX.XXX Jul 21 2016 13:50:31

Desired output should be

Last Login Session Details
Username Port From Latest
root pts/0 Thu 21 17:19:34 +0800
daemon pts/0 Thu 9 20:08:17 +0800
sync pts/0 Thu 9 20:08:47 +0800
operator pts/0 Thu 9 20:08:11 +0800
games pts/0 Thu 9 20:08:00 +0800
nobody pts/0 Thu 9 20:07:53 +0800
sshd pts/0 Thu 9 20:08:25 +0800
oracle pts/1 XXX.XX.XX.XXX Jul 21 2016 13:50:31

Except oracle all other fields don't have a value for From column. But, the current output is taking from the date field.
Pls advise how to check for a value and include it in the shell.
 
Old 07-22-2016, 01:21 AM   #5
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Is there a way to attach a file here? So that i can attach the current excel output..
 
Old 07-22-2016, 01:16 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You are aware that Excel immediately reads a csv file without issue ... hence why my suggestion to make your life easier

As to your output, the from field identifies users who have logged into said computer / server from somewhere other than the machine itself.
In your case, only oracle user is accessing this machine from an external machine, thus all the others have a blank field meaning they have accessed this machine from within.

You can add attachments if you press the 'Go Advanced' button, but excel files are not accepted so you would need to convert is to a csv and then add the .txt extension

As for finding which rows have a from field, this would best be found by the number of fields on a given row and then returning only those that exist.

To this end, I would probably just write the whole thing in awk instead of bash and then going to awk to get the details Although, your task would be just as easy using only bash and a while loop
 
Old 07-22-2016, 01:43 PM   #7
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
using Microsoft's Excel on linux will be ?? shale we say FUN ?

MS office dose not work
and the online monthly fee office 365 might not run using firefox


ti use MICROSOFT'S Excel
use a microsoft operating system


Now LibreOffice has a SPREADSHEET!!! program called "Calc"
but calc is NOT Microsofe Excel

also for a direct output of MICROSOFT'S "*.xlsx format
you NEED microsoft's excel

or save a imported csv into libreoffice
LO can save a a Microsoft xlsx

PS
Libreoffice imports csv files the exact same way as Microsoft Office Excel dose

Last edited by John VV; 07-22-2016 at 01:49 PM.
 
Old 07-22-2016, 11:42 PM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Hi OP.

In awk if the total number of fields in the record (identified by NF variable) is one less than expected, you should be able to print print $5" " "NO IP" " "$8" "$6 instead of print $5" " $6" "$9" "$7

This should solve your basic problem.

BUT
It seems that you haven't tested it out - since you haven't escaped the > and < within the double quotes in the echo statements.



OK

Last edited by AnanthaP; 07-22-2016 at 11:52 PM.
 
Old 07-24-2016, 10:09 PM   #9
Mitiraj
LQ Newbie
 
Registered: Aug 2014
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks Grail and John.
Anantha. Can help on how to check for NF.. Thanks
 
Old 07-25-2016, 03:14 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You can look it up

http://www.gnu.org/software/gawk/man...ode/index.html
 
  


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
[SOLVED] How to provide response to command output steven.c.banks Linux - General 3 05-04-2011 02:40 PM
expect output to excel file vanid Linux - Kernel 6 03-17-2011 09:52 PM
Interface which would provide complete System Information over the Network??? your_shadow03 Linux - Newbie 3 11-21-2008 09:35 AM
doc automation : retrieve information from microsoft word form or excel with scripts gink_oh Linux - Newbie 1 11-17-2008 05:29 AM
How to capture output of system call that uses printk? Ook Linux - Newbie 1 04-23-2008 11:22 PM

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

All times are GMT -5. The time now is 09:25 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