4

I am configuring a Bastion Host

Jump Server

I'm trying to record SSH terminals in the server automatically, I'm using TermRecord. The problem starts when I add TermRecord -o ss.html to my .bashrc:

echo "TermRecord -o ss.html" >> ~/.bashrc

The terminal will flood "script started" without saving anything.

2

I have absolutely no experience with TermRecord, my answer is based on a reasonable (I hope so) guess about what happens.

It looks like TermRecord sources .bashrc again, or more likely it runs another bash that sources the file. I assume the latter. Anyway it runs TermRecord again, yet another bash sources .bashrc again and so on.

If this stops you from logging in, see Login without running bash_profile or bashrc. I would try ssh -t username@hostname /bin/sh and fix my .bashrc from there.

To make it work as you expected you need to find a way to suppress executing TermRecord if already inside TermRecord. The best way would be if the program added some variable to the environment, so you could detect it. I cannot find whether or not it sets any variable.

In case it doesn't, you can do this by yourself. Instead of

TermRecord -o ss.html

in your .bashrc use something like this:

[ -z "$TR_PPID" ] && { export TR_PPID=$$; TermRecord -o ss.html; }

It means: if the variable is empty, set it to the shell PID and run TermRecord; otherwise do nothing (continue to the next line).

Notes:

  • Make sure the variable named TR_PPID is not used by TermRecord or anything else. I made this name up (from "TermRecord parend PID"), there may be a collision though.
  • Hardcoded -o ss.html may cause trouble if you log in multiple times.
  • If TermRecord gets executed then the rest of .bashrc will be sourced by the outer bash as late as TermRecord exits. The inner bash will source the file on its own.
  • To avoid nesting processes, you may consider replacing the outer bash with TermRecord instead of creating a separate process; it can be done with exec TermRecord -o ss.html. In this case you probably want the whole line to be at the very beginning of your .bashrc. Now the variable would mean "TermRecord PID" so TR_PID would be a better name.

Possible(?) alternative: use TermRecord as your login shell.

  • Only if it supports being a login shell (I don't know this); otherwise it may cause trouble (but maybe a custom wrapper would fix it). Experiments with changing your login shell (see chsh) should be performed without leaving the old shell or even with an extra "emergency shell". If you by any means lock yourself out and cannot log in anew, you still have the old shell running and you can fix it.
  • This may require editing /etc/shells.
  • In this case you shouldn't start TermRecord from your .bashrc at all.
0

As I understand it, you wish to record all your terminal stdin/stdout while ssh'd to a server?

consider using script command

script <output_filename>

and then ssh to your server, once you are done close your ssh connection and

exit

to stop the script recording, the $output_filename you provided will be in your current directory.

ps: you could make sure your recordings are always named uniquely by using the following convention

script foo_$(date  +"%Y-%m-%d%H%M")   # full date and time included in filename

or

script foo_$(date +%s)  # unique number calculate by seconds since the Unix epoch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.