I want to add the timestamp and user, who executed the command to my history command. Is this even possible?
By adding this line to the ~/.bashrc, you're able to add the timestamps to the history command:
HISTTIMEFORMAT="[ %Y/%m/%d %T ] "
This results in something like this by running history:
1841 [ 2016/08/25 10:57:54 ] ls
1842 [ 2016/08/25 10:57:56 ] who
1843 [ 2016/08/25 10:57:59 ] last
1844 [ 2016/08/25 10:58:10 ] uptime
1845 [ 2016/08/25 10:58:13 ] history
Now, I want an additional info: Which real user has executed these commands?
Yes, the history is saved and written for each individual user to the $HISTFILE (~/.bash_history), but usually has every user his own login like "firstname-name" or anything else.
An example user called "max-mustermann" logs in via SSH and switches to the user "root" for example. The user "max-mustermann" is now acting as "root".
I want to log the "real" user (in this case "max-mustermann") and maybe some additional information like the used IP address, to be able to identify, who has executed the command "uptime" as user "root". I thought, I've found a solution, but it didn't really work. Add the following lines to your ~/.bashrc to test it:
TTY_CON=$(tty | cut -d '/' -f 3-);
HIST_WHO_INFO=$(who | grep "${TTY_CON}" | cut -d ' ' -f 1,12);
HISTTIMEFORMAT="[ %Y/%m/%d %T by ${HIST_WHO_INFO} ] "
This returns something like this:
1848 [ 2016/08/25 11:06:47 by max-mustermann (10.50.1.42:S.0) ] ls
1849 [ 2016/08/25 11:06:48 by max-mustermann (10.50.1.42:S.0) ] who
1850 [ 2016/08/25 11:06:49 by max-mustermann (10.50.1.42:S.0) ] last
1851 [ 2016/08/25 11:06:51 by max-mustermann (10.50.1.42:S.0) ] uptime
1852 [ 2016/08/25 11:06:52 by max-mustermann (10.50.1.42:S.0) ] history
That's perfect, but unfortunately, it doesn't keep / save the real user with the history. If somebody else logs in, all commands, which were executed as user "max-mustermann" in this example would get updated to the new user, who has logged in:
1848 [ 2016/08/25 11:06:47 by somebody-else (10.50.1.18) ] ls
1849 [ 2016/08/25 11:06:48 by somebody-else (10.50.1.18) ] who
1850 [ 2016/08/25 11:06:49 by somebody-else (10.50.1.18) ] last
1851 [ 2016/08/25 11:06:51 by somebody-else (10.50.1.18) ] uptime
1852 [ 2016/08/25 11:06:52 by somebody-else (10.50.1.18) ] history
1853 [ 2016/08/25 11:08:13 by somebody-else (10.50.1.18) ] history
How can I solve this problem?
This is not always possible, depending on your server configuration and user behaviour, but can be if you "educate" your users properly. There are 2 ways (that I know of) you can get this information.
Just let your users only use sudo whenever they actually need it. Don't "switch" to root and start running commands. Just run seperate commands with a sudo prefix whenever you actually need root access. That way the history is still saved per user (and a sudo log will be written to the /var/log/auth.log file on most systems).
Make them switch to root using sudo su and not sudo su -, the - will reset their environment variables. But if you omit that -, the root enviroment variables should have a SUDO_USER variable holding the user that switched to the account. For example:
oldskool#server:~$ sudo su
root#server:/home/oldskool# echo $SUDO_USER
oldskool
You could use that SUDO_USER in your history timeformat.
HISTTIMEFORMAT will only save epoch to HISTFILE. You can't add any other text. Instead, I create different files per user:
echo "export HISTSIZE=10000" >> ~/.bash_profile
echo "export HISTTIMEFORMAT=\"%F %T \"" >> ~/.bash_profile
echo "export HISTFILE=\"/root/.bash_history_\$(who am i | awk '{print $1}')\"" >> ~/.bash_profile
echo "export PROMPT_COMMAND='history -a'" >> ~/.bash_profile
Related
I need to register many thousands of users in ejabberd from csv file. For this, I wrote a simple script.
#!/bin/sh
OLDIFS=$IFS
IFS=','
[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }
while read username domain pass p1 p2 p3 p4
do
echo "ejabberdctl register $username $domain $pass"
ejabberdctl register $username $domain $pass
done < users.csv
IFS=$OLDIFS
But in the end, the answer is: Error: cannot_register
If I just run the line copied from the output, everything is ok. The user is created normally.
This is just a little trick for later: once you get your loop working, if you consider ejabberdctl is too slow, you can try using the ReST API. That should be a lot faster when doing many requests.
Configure temporarily something like this (remember to remove this when you finished):
listen:
-
port: 5280
module: ejabberd_http
tls: false
request_handlers:
/api: mod_http_api
api_permissions:
"console commands":
from:
- ejabberd_ctl
- mod_http_api
who: all
what: "*"
modules:
mod_http_api: {}
Then execute this in a shell to register an account:
curl 'localhost:5280/api/register?user=user2&host=localhost&password=somepass123'
I have this command that scan a file and returns a summary.
For example on running this command
omsCmdLineUtil.exe process C:\test.exe Default
the result output is:
Ticket:[ 2214271306 ]
Process Details
---------------
File: [ C:\test.exe ]
MD5: [ D41D8CD98F00B204E9800998ECF8427E ]
SHA1: [ DA39A3EE5E6B4B0D3255BFEF95601890AFD80709 ]
SHA256: [ E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855 ]
File Size: [ 0 bytes ]
File Type Category: [ O ]
File Type: [ - ]
File Type Description: [ empty ]
[ Clean ] Ahnlab scan engine [ 1 ms ]
[ Clean ] ClamAV scan engine [ 1 ms ]
[ Clean ] BitDefender scan engine [ 1 ms ]
[ Clean ] Avira scan engine [ 1 ms ]
[ Clean ] Quick Heal scan engine [ 1 ms ]
[ Clean ] ThreatTrack scan engine [ 1 ms ]
[ Clean ] ESET scan engine [ 1 ms ]
[ Clean ] Total Defense scan engine [ 1 ms ]
Scan Completion
---------------
[ Clean ]
Ticket: [ 2214271306 ]
File path: C:\test.exe
Scan time: 1 ms [12/20/2015 13:00:06:791]
Process Completion
------------------
Ticket: [ 2214271306 ]
User agent: Default
Profile: Default
Result: [ Allowed ]
File processed: C:\test.exe
I want to create a batch file that parses this result by searching for the output line Result:, check if it's [ Allowed ] or [ Blocked ] and return 0 for allowed and 1 for blocked.
I tried something like this, but its not really working:
omsCmdLineUtil.exe process C:\test.exe Default | set ts = findstr /C:"Result: [ Allowed ]"
if %ts% == "Result: [ Allowed ]" return 0
else return 1
Which modification on code is necessary to get the expected result?
there is no return in Batch. I think, you want exit /b <errorlevel>
omsCmdLineUtil.exe process C:\test.exe Default | find "Result: [ Allowed ]" >nul && Exit /b 0 || Exit /b 1
Instead of Exit 0 you can of Course also use set ts=0 and use that. Or use echo instead.
Some explanations:
>nul redirects the output to nirvana, keeping your screen clean.
&& acts as "If previous command was successfull, then..." (string was found)
|| acts as "if previous command was not successfull, then...` (string was not found)
I prefer using find when possible because of it's simpler syntax, but of course findstr /C:"Result: [ Allowed ]"will also work
I have a bash script called sr_run_batch.sh which does super resolution of images. Now I want to do testing on different servers in parallel at the same time. ie. 1 Virtual machine at one given point of time. then 2 virtual machines at one point of time , 3 and then 4.
I tried writing into it the commands
for host in $(cat hosts.txt); do ssh "$host" "$command" >"output.$host"; done
ssh-keygen && for host in $(cat hosts.txt); do ssh-copy-id $host; done
where the file hosts.txt contains the list of servers: username#ip(format) but when I run this, it gives me substitution error
Hence, I tried pssh (parallel-ssh)
pssh -h hosts-file -l username -P $command
command being ./sr_run_batch.sh
but it didn't run, so I modified this to
pssh -h hosts-file -l ben -P -I<./sr_run_batch.sh
But, for some unknown reason, it just prints the echo statements in the code.
here is the code :
NList=(5)
VList=(1)
FList=("input/flower1.jpg" "input/flower2.jpg" "input/flower3.jpg" "input/flower4.jpg")
IList=("320X240" "640X480" "1280X960" "1920X1200")
SList=(2 3)
for VM in ${VList[#]}; do
for ((index=0; index < ${#FList};)) do
file=$FList[$index]
image_size=$IList[$index]
width=`echo $image_size|cut -d "X" -f1`
height=`echo $image_size|cut -d "X" -f2`
for scale_factor in ${SList[#]}; do
for users in ${NList[#]}; do
echo "V: $VM, " "F: $file, " "S: $scale_factor, " "I: $width $height , " "N: $users"
for i in `seq 1 $users` ; do
./sr_run_once.sh $file $width $height $scale_factor &
done
wait
done # for users
done # for scale_factor
done # for index
done # for VM
exit 0
Have you also tried to use pssh with a simple bash-script so see if the communication is set up ok?
$ pssh -h hosts.txt -A -l ben -P -I<./uptime.sh
Warning: do not enter your password if anyone else has superuser
privileges or access to your account.
Password:
10.0.0.67: 11:06:50 up 28 min, 2 users, load average: 0.00, 0.00, 0.00
[1] 11:06:50 [SUCCESS] 10.0.0.67
10.0.0.218: 11:06:50 up 24 min, 2 users, load average: 0.00, 0.05, 0.20
[2] 11:06:50 [SUCCESS] 10.0.0.218
I am trying to get my Capistrano deploy script working, but it is not doing the symlinking as it is configured to do as shown below.
set :linked_files, %w{config/database.yml}
set :linked_dirs, %w{log tmp vendor/bundle public/system}
When it runs the related command, I get the following:
WARN [SKIPPING] No Matching Host for /usr/bin/env [ -f /path/to/shared/config/database.yml ]
If I run this command on the server, either through ssh or through logging onto the server and running the command, I get no response from the command.
user: ~
$ [ -f /path/to/shared/config/database.yml ]
user: ~
$
The file does exist in the specified location and has permissions.
user: ~
$ ll /path/to/shared/config/
total 4.0K
drwxrwxr-x 2 user group 33 Nov 30 10:58 .
drwxrwxr-x 7 user group 89 Nov 30 10:58 ..
-rwxrwxr-x 1 user group 805 Nov 30 10:58 database.yml
user: ~
Shouldn't this return a true or a false, instead of nothing? Is there a configuration I may have changed that suppresses the output? I get no response at all whether the file exists or not.
In your response to the actual question you ask, test (which is what [ is an alias for) does in fact not return output to stdout. It returns an exit code.
user: ~
$ [ -f /path/to/shared/config/database.yml ] # if the file exists
user: ~
$ echo $?
0
user: ~
$ [ -f /path/to/shared/config/database.yml ] # if the file does not exist
user: ~
$ echo $?
1
test -f /path/to/file (or [ -f /path/to/file ]) yields an exit code of 0 if the file exists or 1 if it does not. If you want to check that a file is there and echo the path to it, try:
[ -f /path/to/file ] && echo "/path/to/file"
I have created bash scirpt that takes jstat metrics of my jvm instances!
Here is the output example :
demo.server1.sms.jstat.eden 24.34 0
demo.server1.lcms.jstat.eden 54.92 0
demo.server1.lms.jstat.eden 89.49 0
demo.server1.tms.jstat.eden 86.05 0
But when the Sensu-client runs this script it returns
Could not attach to 8584
Could not attach to 8588
Could not attach to 17141
Could not attach to 8628
demo.server1.sms.jstat.eden 0
demo.server1.lcms.jstat.eden 0
demo.server1.lms.jstat.eden 0
demo.server1.tms.jstat.eden 0
Here is the example of check_cron.json
{
"checks": {
"jstat_metrics": {
"type": "metric",
"handlers": ["graphite"],
"command": "/etc/sensu/plugins/jstat-metrics.sh",
"interval": 5,
"subscribers": [ "webservers" ]
}
}
}
And piece of my bash script
jvm_list=("sms:$sms" "lcms:$lcms" "lms:$lms" "tms:$tms" "ums:$ums")
for jvm_instance in ${jvm_list[#]}; do
project=${jvm_instance%%:*}
pid=${jvm_instance#*:}
if [ "$pid" ]; then
metric=`jstat -gc $pid|tail -n 1`
output=$output$'\n'"demo.server1.$project.jstat.eden"$'\t'`echo $metric |awk '{ print $3}'`$'\t0'
fi
done
echo "$output"
I find out that problem is with jstat and i tried to write full jstat path like /usr/lib/jvm/java-1.7.0-openjdk.x86_64/bin/jstat -gc $pid|tail -n 1 but it didn't help!
By the way if i will comment this row the output like "Could not attach to 8584" disappears!
I'm not a Java or Sensu user, but I can guess what happens.
Most likely, sensu-client runs your script as a user different from the one you use when testing manually, which doesn't have permissions to "attach" (whatever that means) to your jvm instances.
To verify this you can add invocation of "whoami" to your script, run it from sensu-client again, see what user it runs your script under and, if it is different, try to run your script as that user.
Yes you're right sensu runs all script as sensu user. To use jstat you have to add sensu to a sudoers.
just add file /etc/sudoers.d/sensu
Example:
Defaults:sensu !requiretty
Defaults:sensu secure_path =
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
sensu ALL = NOPASSWD: /etc/sensu/plugins/jsat-metrics.rb