How do I save several running ping thread feedback/results to each related file output? - linux

I have written below script in linux shell script, for pinging several routers in parallel and save output to files and other script analysis for packet lost on links. as you can see all pings run in background and simulate parralelism or multithreading.
for ips in 100.28.139.5 100.20.12.90 100.23.13.74 100.25.131.10
do
ping $ips -s 500 -c 500 &> ${ips}.500.text &
ping $ips -s 1500 -c 500 &> ${ips}.1500.text &
ping $ips -s 4500 -c 500 &> ${ips}.4500.text &
done
I have tried rewrite it by java but I find it so big(>100 lines) and I didn't able to save the thread results to related ping file output.
I need dedicate logger for each thread, to save outputs.
How do I save several running ping thread feedback/results to each related file output?

When you create your thread, using the costructor you will pass him certain data: suppose the url to be pinged. Using such a information you create your own file on disk where to output data coming from ping feedback.

Related

How to log the live output of a running process

I want to run a game server inside my Ubuntu machine. I want to run it in the background and write the live output of that process inside a log file. I tried using nohup and running the game server using "&" at the end but I couldn't make it work the way I wanted.
Then I started reading about named pipes and actually gave it a go. I made a simple script that in theory should work. But, of course I am missing something.
First, I made a pipe using the mkfifo command.
mkfifo testpipe
Then I created a small script:
#!/bin/bash
./mta-server64 > pipe &
pid=$!
echo $pid // so I know the pid of the process
cat < pipe > log.txt &
(Note: I wrote this code from memory.)
The code works only when there is an error and the process stops. It actually records the game console error. But when the game server is running I get no output in the log file.
I want to read the output (stdout and stderr if I am not mistaken) of a process running in background and record it those inside a log file.
I also thought about using screen as it logs everything inside a file but I would prefer not using it if there is a better solution.
EDIT:
First of all: thank you for the interest you had in helping me. In the same way, I have to apologize for only giving scarce details about what I intend to do with this small project and for my limited understanding of stdout and stderr.
Let's go to the first base.
I want to run a game server named Multi Theft Auto (https://multitheftauto.com/). This is GTA San Andreas but multiplayer.
I can easily run this game server in my Ubuntu server by calling the executable ./mta-server-64. After calling it the game server console appears:
[|] MTA: San Andreas :: 0/32 players :: 196 resources :: 125 fps (25)
MTA:BLUE Server for MTA:SA
==================================================================
= Multi Theft Auto: San Andreas v1.5.6 [64 bit]
==================================================================
= Server name : Default MTA Server
= Server IP address: auto
= Server port : 22884
=
= Log file : /root/mta/mods/deathmatch/logs/server.log
= Maximum players : 32
= HTTP port : 22564
= Voice Chat : Disabled
= Bandwidth saving : Medium
==================================================================
[09:49:07] Resource 'mapmanager' requests some acl rights. Use the command 'aclrequest list mapmanager'
[09:49:07] Resources: 196 loaded, 0 failed
[09:49:07] Starting resources...
[09:49:07] Server minclientversion is now 1.5.6-9.16588.0
[09:49:07] INFO: MAPMANAGER: Some important ACL permissions are missing. To ensure the correct functioning of Mapmanager, please write: aclrequest allow mapmanager all
[09:49:07] Gamemode 'play' started.
[09:49:07] Authorized serial account protection is enabled for the ACL group(s): `Admin` See http://mtasa.com/authserial
[09:49:07] WARNING: <owner_email_address> not set
[09:49:07] Server started and is ready to accept connections!
[09:49:07] To stop the server, type 'shutdown' or press Ctrl-C
[09:49:07] Type 'help' for a list of commands.
[09:49:07] Querying MTA master server... success! (Auto detected IP:xxx.xxx.xxx.xxx)
I am using the following script to run the process in the background and (try to) get the live output from:
#!/bin/bash
newport=$(shuf -i 22003-22900 -n 1)
newip=$(shuf -i 22003-22900 -n 1)
rm -rf ~/server/*
cp -r /home/user*/ftp/server/mtaserver/serverfiles/* ~/server
sed -i "s/<httpport>[0-9][0-9][0-9][0-9][0-9]<\/httpport>/<httpport>$newport<\/httpport>/g" ~/server/mods/deathmatch/mtaserver.conf
sed -i "s/<serverport>[0-9][0-9][0-9][0-9][0-9]<\/serverport>/<serverport>$newip<\/serverport>/g" ~/server/mods/deathmatch/mtaserver.conf
~/server/mta-server64 2>&1 | tee -a outfile &
mta_pid=$!
echo $mta_pid
sleep 6
pkill $mta_pid
(Note: Because of some technical problems I had to add the first few lines of script which automatically replace the game files with new ones and also replace the existing ports with random ones.)
This script starts the server and tries to log the output of the process. The process is automatically killed after few seconds so there is only one instance of the game server at any given time.
THE ISSUE:
This script only logs the output if there is an error. I still cannot get the live output of the process when it is still running. Maybe this is an issue with the game server but truly believe there should be a way to make it work the way I intend.
I believe you want to use tee command to split the pipe output to log file.
I suggest you read this article and these answers 1 2.
Usually this is enough nohup somecommand > somecommand.log 2>&1 & then, tail -F somecommand.log to follow the logs.
After 2 days I finally figured out a way to make it work (the way I intended to work, without taking in consideration any major security/performance risks).
Reading the comments made me realize I was attacking the wrong point. The stdout of the game server is buffered, thus making it impossible to log it into a log file using the methods I tried when I posted my question At least this is what I came to understand).
I did some research on how to run the application without having the stdout buffered: https://serverfault.com/questions/294218/is-there-a-way-to-redirect-output-to-a-file-without-buffering-on-unix-linux
My code now:
stdbuf -o0 ~/server/mta-server64 >> pipe &
cat < pipe | tee -a outfile &
After creating the named pipe it executes the game server inside that pipe and then appends the stdout into the log file.
The stdbug -o0 command disables the stdout buffering (as noted in the link above).
This works for me and I cannot guarantee it will work for anybody else. I am still not aware if disabling the buffering is a safe approach to my issue but for now it is what I need.

Writing a linux script for tcpdump for stopping and running again but save the info in another file

I am new to writing script and not sure whether I am correct in writing such script for tcpdump to collect pcap info.
tcpdump -s 0 port ftp or ssh or http or https -i eth0 -w mycap.pcap
#run the tcpdump and store all the info in mycap.pcap
sudo kill -2 #for exit purpose
This enables me to run tcpdump which is good, however, I wish to stop this (due to the space for mycap.pcap meet the max capacity of 3GB per file) automatically via the same script and run again but this time round, I will store it in another file (eg. mycap1.pcap)
Then the cycle goes again until I stop the process by pressing crtl+c
Can this be done?
You don't need to write a script for that.
tcpdump -C <filesize> -s 0 port ftp or ssh or http or https -i eth0 -w mycap.pcap
Have a look at the man-page for tcpdump.

Send Control signals to another machine

I am attempting to automate throughput measurement of a WLAN using iperf ,so i have a script that will initiate the server (iperf -s -i1 -w2M -t300) and save the contents of the output,send an email and place file in a corporate directory. However, i still have to execute the client side remote command manually (iperf -c 192.168.0.150 -w2M -i1 -t300) .I want to execute one script for both task either by :-
a) Start an SSH process in the script and execute the command.
b) Pass an iperf application signal to the remote PC that will induce a process execution.
Please note that by SSH, it will achieve my goal but it will defeat the purpose of my exercise, i want to do functions calling between two machines
I want to have a running application on both sides and the process executed on one.
#!/usr/bin/bash
cwd=$(pwd)
export PATH=$PATH:$cwd
read -p "Name of the file" FXX
read -p "Number of minutes" MMM
iperf -s -i1 -w2M -t"$MMM" | tee "$FXX.txt"
source iperfserver
source /usr/bin/iperfserver
trap exit INT
sshpass -p 'hello' scp "$FXX.txt" bharat#hello:
echo "Hello"|mailx -s "Hello " -a "$FXX.txt" bharat#hello.com
Here is the iperfserver script, so i am trying to coordinate the efforts between the scripts but i am not able to so as soon as i start iperf -s the script stops and i cannot create a simultaneous process. I will try to research on this but if you can help me i will be grateful.
#!/usr/bin/bash
cwd=$(pwd)
export PATH=$PATH:$cwd
sshpass -p 'hello' ssh 192.168.0.101
iperf -c 192.168.0.150 -w2M -i1 -t300
Thanks
Bharat C P

Passing Arguments to Running Bash Script

I have a bash script that takes a list of IP Addresses, and pings them every 15 seconds to test connectivity. Some of these IP Addresses are servers and computers as to which I have the ability to control. I would like to be able to do something of the following:
Run The Bash File
It pings non-controlled IP Addresses
It will list the controlled Computers
When a computer turns off, it sends my script a response saying it turned off
The script outputs accordingly
I have the code all set up that pings these computers every 15 seconds and displays. What I wish to achieve is to NOT ping my controlled computers. They will send a command to the bash script. I know this can be done by writing a file and reading such file, but I would like a way that changes the display AS IT HAPPENS. Would mkfifo be an viable option?
Yes, mkfifo is ok for this task. For instance, this:
mkfifo ./commandlist
while read f < ./commandlist; do
# Actions here
echo $f
done
will wait until a new line can be read from FIFO commandlist, read it into $f and execute the body.
From the outside, write to the FIFO with:
echo 42 > ./commandlist
But, why not let the remote server call this script, perhaps via SSH or even CGI? You can setup a /notify-disconnect CGI script with no parameters and get the IP address of the peer from the REMOTE_ADDR environment variable.

Linux Bash script to ping multiple hosts simultaneously

I have a text file with list of 500 server names. I need to ping all of them simultaneously instead of one by one in a loop, and put the pingable ones in one file and unpingable ones in another file.
Can I run each ping in background or spawn a new process for each ping? What is the quickest and most efficient way to achieve this?
You can control the parallelism by using xargs:
cat file-of-ips | xargs -n 1 -I ^ -P 50 ping ^
Here we're keeping at most 50 pings going at a time. The ip itself is inserted at the ^; you can put arguments before and after.

Resources