0

I have been utilizing this command line to secure shell into a host computer and record the session terminal and direct it to an associated .txt file.

ssh -t dobby1997@${IP} "script -t 2> timing.log" | tee "ssh_session.txt"

I am trying to prepend the client's timestamp into the .txt file, instead of utilizing the host's timestamp, but have been unsuccessful.

I have tried using awk:

ssh -t dobby1997@${IP} "script -t 2> timing.log" | awk -v epoch="$(date +%s)" '{print epoch, $0 }' | tee "ssh_session.txt"

In order to prepend the client's timestamp before it is directed to the .txt file. I am aiming to be capable of doing this live while in ssh, and for each line in the .txt fie to be stamped with the current client's epoch. I want to run it, log in with the host's password, record the terminal session, exit, and the be able to view the .txt file with the client's timestamps.

However, when I begin running the script, I receive the prompt for the user password and attempt to log into the host computer with it but nothing happens afterward, once the password is submitted.

This is the desired output when the .txt file is viewed:

1686322204 dobby@ubuntu:~/matrix_math_timer$ make
1686322205 gcc -c main.c
1686322206 gcc main.o matrix_impl.o -o matrix_math_timer
1686322207 dobby@ubuntu:~/matrix_math_timer$ ./cache_money 
1686322208 Time taken: 0.189859
1686322208 Time taken: 0.152965
1686322214 Time taken: 6.197541
1686322218 Time taken: 4.893529
1686322220 dobby@ubuntu:~/matrix_math_timer$ make clean
1686322220 rm *.o matrix_math_timer
1686322220 dobby@ubuntu:~/matrix_math_timer$

I would greatly appreciate any tips & assistance.

0

2 Answers 2

2

With GNU awk I suggest to replace your awk command with this to get unix time continuous:

awk '{ print strftime("%s"), $0 }'

See: Time Functions

Sign up to request clarification or add additional context in comments.

2 Comments

Hello Cyrus, I appreciate your rapid response! I see what you did with the unix time continuous. I replaced my previous awk command with yours, and it appears that I am still having the issue of receiving the prompt for the user password and attempt to log into the host computer with it but nothing happens afterward. When I run the line original line without awk, it works. But I was hoping to prepend the client's timestamps. I'm not too sure what else I may be doing incorrectly? In the meantime, I am going to look closer at your response. Thanks again, Cyrus!
@DobbyFenk You may be having trouble because awk won't pass anything through to the terminal until it's a complete line. Try ssh -t dobby1997@${IP} "script -t 2> timing.log" | tee >(awk -v epoch="$(date +%s)" '{print epoch, $0 }' >ssh_session.txt)
0

use fifo if other ways is not working : https://unix.stackexchange.com/questions/56189/appending-timestamp-stamp-along-with-log-file-lines

$ mkfifo autossh.log.fifo

in autossh.sh script I use :

export AUTOSSH_LOGFILE="/root/autossh.log.fifo"
rm /root/autossh.log
(while true; do cat /root/autossh.log.fifo | ts '[%d-%m-%Y %H:%M:%S]' >> /root/autossh.log; done;) &
autossh ... -E /root/autossh.log.fifo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.