Bash Script: Using "Script" Command from a Bash Script for Logging a Session

Bash script: Using script command from a bash script for logging a session

Your shell script did not terminate. It is still running. You are getting a prompt because script is spawning a new shell. the prompt you see is the prompt from the spawned shell.

The normal use case for script is something like this:

  1. start script. this spawns a new shell.
  2. do commands in the new shell.
  3. exit the shell and drop to previous shell
  4. examine the logfile created by script

So basically script is working as expected. You will have to find another way to achieve what you want.

You can log the execution of your script like this:

#! /bin/bash
exec > logfile 2>&1
set -x
FOO=BAR
echo $FOO

Explanation:

  • exec > logfile 2>&1 redirects stdout and stderr to logfile
  • set -x makes bash print every command before executing it

Example:

$ ./foo.sh
# (no output here because everything goes to logfile)
$ cat logfile
+ FOO=BAR
+ echo BAR
BAR

Disadvantage of this method is that the script prints no output for humans to see. Everything goes to the logfile.

Alternatively you can do it like this:

#! /bin/bash
# nothing special here
FOO=BAR
echo $FOO

Then execute like this:

$ script -c "bash -x foo.sh"
Script started, file is typescript
+ FOO=BAR
+ echo BAR
BAR
Script done, file is typescript

now output is directly visible and also saved to logfile (default name of logfile is typescript)

$ cat typescript 
Script started on Mi 18 Mai 2011 01:05:29 CEST
+ FOO=BAR
+ echo BAR
BAR

Script done on Mi 18 Mai 2011 01:05:29 CEST

script command exits bash script

First hit on google...

Bash script: Using "script" command from a bash script for logging a session

Script command opens a new shell, your script is still running in the background

Is there a way to know if the `script` command is running in the current bash session?

Instead of trying to figure out whether or not script is running, simply leave a breadcrumb for yourself:

if [ -z "$BEING_LOGGED" ]
then
export BEING_LOGGED="yes"
LOG_PATH="/var/log/terminal/$(date +'%Y%m%d')"
LOG_FILE="${LOG_PATH}/$(date +'%H%M%S').log"
mkdir -p "${LOG_PATH}"
exec script "${LOG_FILE}"
fi

The first time the file is sourced, BEING_LOGGED will be unset. When script is invoked and the file is sourced again, it'll be set, and you can skip logging.

script command - bash linux terminal

You need to terminate script operation by running 'exit' command. That wont exit your terminal as such. Then you can view your log file.

Here is the duplicate with more detailed info -> Bash script: Using "script" command from a bash script for logging a session

Write to custom log file from a Bash script

logger logs to syslog facilities. If you want the message to go to a particular file you have to modify the syslog configuration accordingly. You could add a line like this:

local7.*   -/var/log/mycustomlog

and restart syslog. Then you can log like this:

logger -p local7.info "information message"
logger -p local7.err "error message"

and the messages will appear in the desired logfile with the correct log level.

Without making changes to the syslog configuration you could use logger like this:

logger -s "foo bar" >> /var/log/mycustomlog

That would instruct logger to print the message to STDERR as well (in addition to logging it to syslog), so you could redirect STDERR to a file. However, it would be utterly pointless, because the message is already logged via syslog anyway (with the default priority user.notice).

Automating telnet session using bash scripts

Write an expect script.

Here is an example:

#!/usr/bin/expect

#If it all goes pear shaped the script will timeout after 20 seconds.
set timeout 20
#First argument is assigned to the variable name
set name [lindex $argv 0]
#Second argument is assigned to the variable user
set user [lindex $argv 1]
#Third argument is assigned to the variable password
set password [lindex $argv 2]
#This spawns the telnet program and connects it to the variable name
spawn telnet $name
#The script expects login
expect "login:"
#The script sends the user variable
send "$user "
#The script expects Password
expect "Password:"
#The script sends the password variable
send "$password "
#This hands control of the keyboard over to you (Nice expect feature!)
interact

To run:

./myscript.expect name user password

Get specific message from output and store info to a log file

expect utility has proc named log_file to record the logs.

In man expect:

log_file [args] [[-a] file]
If a filename is provided, log_file will record a transcript of the session (beginning at that point) in the file. log_file will stop recording if no argument is given. Any previous log file is
closed.

Instead of a filename, a Tcl file identifier may be provided by using the -open or -leaveopen flags. This is similar to the spawn command. (See spawn for more info.)

The -a flag forces output to be logged that was suppressed by the log_user command.

By default, the log_file command appends to old files rather than truncating them, for the convenience of being able to turn logging off and on multiple times in one session. To truncate files,
use the -noappend flag.

The -info flag causes log_file to return a description of the most recent non-info arguments given.`

You can just use like this:

#!/usr/bin/expect
set IP 192.168.5.100
catch {spawn -noecho telnet $IP}
set timeout 3
log_file your_log_file.log
---
---
---


Related Topics



Leave a reply



Submit