Adrian Chrostowski
Adrian Chrostowski

Reputation: 434

Write custom ssh command output to file

I'm completely new to SSH and I'm trying different things. I wanted to write my custom commands output to a log file.
Here is my command:

hostname:~$ sudo checktemp grep
/dev/sda: ST3320820AS: 38°C
coretemp-isa-0000
Adapter: ISA adapter
Core 0:       +45.0°C  (high = +78.0°C, crit = +100.0°C)
Core 1:       +45.0°C  (high = +78.0°C, crit = +100.0°C)
radeon-pci-0100
Adapter: PCI adapter
temp1:        +59.0°C

I want to write to file the following:

ST3320820AS: 38°C
Core 0:       +45.0°C
Core 1:       +45.0°C
temp1:        +59.0°C

I looked up grep command but no success. Additionaly I would like to now how to set the log file destination and wrote settings (write new line etc.).

I would like the log file to look like below:

ST3320820AS; Core 0; Core 1; temp1; date; time

38°C; +45.0°C; +45.0°C; +59.0°C; yyyy-mm-dd; hh:mm:ss

Thanks for your help.

Upvotes: 0

Views: 245

Answers (1)

aprado
aprado

Reputation: 24

To create the log file:

sudo checktemp | grep -E "Core|temp1|sda" | awk -F"(" '{print $1}' | sed 's,/dev/sda: ,,g' > log

To append to the end of the log file:

sudo checktemp | grep -E "Core|temp1|sda" | awk -F"(" '{print $1}' | sed 's,/dev/sda: ,,g' >> log

To print in the one-line format with the date and the time:

sudo checktemp | grep -E "Core|temp1|sda" | awk -F"(" '{print $1}' | sed 's,/dev/sda: ,,g' | sed 's, ,,g' > log

echo -n date: >> log
date +%Y-%m-%d >> log
echo -n time: >> log
date +%H=%M=%S >> log

cat log | awk -F":" '{ for (i=1; i<=NF; i++) { a[NR,i] = $i } } ; NF>p { p = NF } END { for(j=1; j<=p; j++) { str=a[1,j] ; for(i=2; i<=NR; i++){ str=str"; "a[i,j]; } print str } }' | sed 's,Core,Core ,g;s,=,:,g' > log.txt

Upvotes: 1

Related Questions