powershell ping continous for specific duration and save log - powershell-2.0

I am new to powershell
when searching for test my internet connection i found this simple code
ping google.com -t | Select-String "Reply" | foreach $_ { $a = Get-Date$a.ToString() + " " + $_ } | Out-File "C:\users\"your account"\Documents\pingLog$((get-date).tostring("HHmmss")).txt"
try it and success but i want to set ping for specific duration say about 30 minutes or 1 hour so i try modified the code with this
$stopTime = (Get-Date).AddMinutes(30)
$results = do {
$now = Get-Date
ping google.com -t | Select-String "Reply" | foreach $_ { $a = Get-Date
$a.ToString() + " " + $_ }
}
until ($now -ge $stopTime)
$results | Out-File "C:\users\"your account"\Documents\pingLog$((get-date).tostring("HHmmss")).txt"
but no result or output to txt.
i just want to ping for about 30 minutes or 1 hours and stop, save the result(not only reply but include rto and unreachable) to log and schedule it with task schedule. Any help would be greatly appreciated.

Remember always a golden rule: Be always careful with time! (comparing times/zones, looking into past/future, timestamps etc.).
Also it is wise not to use extraneous variables. The output can be redirected directly in the script (see the example).
Your script has an issue with the ping -t. That specifies that it should query the server till ctrl+break is specified. The default behaviour of ping command on windows is to spit out 4 replies. You can change that beaviour with -n parameter - the looping is done via powershell. There is no need to use -t for the internal ping loop.
I would use New-TimeSpan which gives you minutes difference if you use .minutes
Edit further simplification of the script
Now you don't need to add any time just check if current time is within the limit. I have added also the real time of running the script (you can get different time stamp from ping command).
# 1 minute
$limit_in_minutes = 1
# path to log
$log_file = '<path_to_log>\time.log'
# clear the log file before running again
fc > $log_file
$start_timer = Get-Date
Do {
$current_time = Get-Date
ping google.com -n 1 | Select-String "Reply" | foreach $_ { $a = Get-Date; $a.ToString() + " " + $_ } | Out-File -Append -Encoding UTF8 -FilePath $log_file
# normally the output of New-TimeSpan is a String but you need an Integer to be able to compare it
$running_minutes = [int]((New-TimeSpan –Start $start_timer –End $current_time).minutes)
$running_seconds = [int]((New-TimeSpan –Start $start_timer –End $current_time).seconds)
Write-Output "Running for: $($running_minutes)m:$($running_seconds)s" | Out-File -Append -Encoding UTF8 -FilePath $log_file
} Until ($running_minutes -ge $limit_in_minutes)
Here is shortened log file:
11.09.2018 16:10:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:0s
11.09.2018 16:10:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:0s
11.09.2018 16:10:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:0s
11.09.2018 16:10:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:0s
11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 0m:0s
11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:1s
11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:1s
11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:1s
11.09.2018 16:10:36 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:1s
...
Running for: 0m:58s
11.09.2018 16:11:33 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:58s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 0m:58s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:34 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:35 Reply from 172.217.168.78: bytes=32 time=48ms TTL=57
Running for: 0m:59s
11.09.2018 16:11:35 Reply from 172.217.168.78: bytes=32 time=47ms TTL=57
Running for: 1m:0s

Related

Connection error from jenkins server and agent

I'm connecting agent node to server.
When I try connecting with jenkins server, error occurs. Below are the log.
Failed to obtain
http://12.36.123.160:8080/computer/rsp18/jenkins-agent.jnlp?encrypt=true
java.io.IOException: Invalid Http response
at sun.reflect.GeneratedConstructorAccessor3.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1950)
at sun.net.www.protocol.http.HttpURLConnection$10.run(HttpURLConnection.java:1945)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1944)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1514)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at hudson.remoting.Launcher.parseJnlpArguments(Launcher.java:523)
at hudson.remoting.Launcher.run(Launcher.java:347)
at hudson.remoting.Launcher.main(Launcher.java:298) Caused by: java.io.IOException: Invalid Http response
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1612)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1498)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at hudson.remoting.Launcher.parseJnlpArguments(Launcher.java:513)
... 2 more Waiting 10 seconds before retry
I thought I lost connection to internet in agent machine, but ping operation seems okay.
> root#ubuntu:~# ping -c 5 12.36.123.160 PING 12.36.123.160
> (12.36.123.160) 56(84) bytes of data. 64 bytes from 12.36.123.160:
> icmp_seq=1 ttl=59 time=0.262 ms 64 bytes from 12.36.123.160:
> icmp_seq=2 ttl=59 time=0.245 ms 64 bytes from 12.36.123.160:
> icmp_seq=3 ttl=59 time=0.259 ms 64 bytes from 12.36.123.160:
> icmp_seq=4 ttl=59 time=0.249 ms 64 bytes from 12.36.123.160:
> icmp_seq=5 ttl=59 time=0.233 ms
> --- 12.36.123.160 ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4102ms rtt min/avg/max/mdev =
> 0.233/0.249/0.262/0.010 ms
connection to jenkins server is good, and my server is okay.
What's wrong with this problem?

Measure execution time of docker container

I have a docker image called my_image which launch a command and closes.
When running the image in a container using command docker run --rm my_image, is it possible to measure the execution time of the container ?
Edit :
I need to see those timing information after container execution, thus I can't use time command.
I somehow hoped to find some container execution history kept by docker even if --rm was used. But if it doesn't exist, then #tgogos' answer is suited.
The goal is to compare execution time of several images to draw a conclusion about the different tools used.
1st approach: time
time docker run --rm --name=test alpine ping -c 10 8.8.8.8
...
real 0m10.261s
user 0m0.228s
sys 0m0.044s
but this will also include the time for creating and removing the container.
2nd approach: container information
The information you are looking for is stored by docker and can be reached by docker container inspect.
docker run --name=test alpine ping -c 10 8.8.8.8
* notice that I didn't use --rm because the next step is to inpect the container. You will have to remove it afterwards. The timestamps you might be interested in are:
"Created": "2018-08-02T10:16:48.59705963Z",
"StartedAt": "2018-08-02T10:16:49.187187456Z",
"FinishedAt": "2018-08-02T10:16:58.27795818Z"
$ docker container inspect test
[
{
"Id": "96e469fdb437814817ee2e9ad2fcdbf468a88694fcc998339edd424f9689f71f",
"Created": "2018-08-02T10:16:48.59705963Z",
"Path": "ping",
"Args": [
"-c",
"10",
"8.8.8.8"
],
"State": {
"Status": "exited",
"Running": false,
"Paused": false,
"Restarting": false,
"OOMKilled": false,
"Dead": false,
"Pid": 0,
"ExitCode": 0,
"Error": "",
"StartedAt": "2018-08-02T10:16:49.187187456Z",
"FinishedAt": "2018-08-02T10:16:58.27795818Z"
}
...
Duration calculation example (with bash):
You can put these timestamps in bash variables with single commands like this:
START=$(docker inspect --format='{{.State.StartedAt}}' test)
STOP=$(docker inspect --format='{{.State.FinishedAt}}' test)
Then you can convert them to UNIX epoch timestamps (seconds since Jan 01 1970. (UTC))
START_TIMESTAMP=$(date --date=$START +%s)
STOP_TIMESTAMP=$(date --date=$STOP +%s)
and if you subtract these two, you get the duration in seconds...
echo $(($STOP_TIMESTAMP-$START_TIMESTAMP)) seconds
9 seconds
You have to consider a couple of things:
How to get time execution of a process given a PID.
Execute docker exec specifying PID=1 because container time running = entrypoint running.
Combining two things you get docker container time execution with:
docker exec -ti <container_id> ps -o etime= -p "1"
It gives you more accuracy than column STATUS of docker ps command.
If you're executing several processes in container and you need execution time for any of them, just replace "1" by its PID inside container.
Another possible approach may be to override the default entrypoint with the time command.
$ docker run --rm --name=test --entrypoint=time alpine ping -c 10 8.8.8.8
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=37 time=51.213 ms
64 bytes from 8.8.8.8: seq=1 ttl=37 time=7.844 ms
64 bytes from 8.8.8.8: seq=2 ttl=37 time=8.120 ms
64 bytes from 8.8.8.8: seq=3 ttl=37 time=10.859 ms
64 bytes from 8.8.8.8: seq=4 ttl=37 time=10.975 ms
64 bytes from 8.8.8.8: seq=5 ttl=37 time=12.520 ms
64 bytes from 8.8.8.8: seq=6 ttl=37 time=7.994 ms
64 bytes from 8.8.8.8: seq=7 ttl=37 time=8.904 ms
64 bytes from 8.8.8.8: seq=8 ttl=37 time=6.674 ms
64 bytes from 8.8.8.8: seq=9 ttl=37 time=7.132 ms
--- 8.8.8.8 ping statistics ---
10 packets transmitted, 10 packets received, 0% packet loss
round-trip min/avg/max = 6.674/13.223/51.213 ms
real 0m 9.02s
user 0m 0.00s
sys 0m 0.00s
Doing this won't include the container start up time. You can even do something like:
time docker run --rm --name=test --entrypoint=time alpine ping -c 10 8.8.8.8 to see how long just the container startup takes.

Docker for windows - Can't connect to default iis site

I have a windows container running the microsoft/iis base image, with Docker Desktop for Windows
I built the image using the following:
Command:
docker build -t test-iis-image .
File:
FROM microsoft/iis
EXPOSE 8080'
I then run a container using:
Command:
docker run -d -p 8080:8080 test-iis-image
I got the IP address of the container (e.g. 172.18.167.181)
added a route:
Command
route /P add 172.0.0.0 MASK 255.0.0.0 10.0.75.1
and then tried to connect to the container using the following url:
http://172.18.167.181:8080/
I am expecting to see the default IIS web page, but all I get is:
The following error was encountered while trying to retrieve the URL: > > > http://172.18.167.181:8080/
Connection to 172.18.167.181 failed.
The system returned: (110) Connection timed out
The remote host or network may be down. Please try the request again.
However, when i ping the ip address it seems to find the container:
ping 172.18.167.181
Pinging 172.18.167.181 with 32 bytes of data:
Reply from 172.18.167.181: bytes=32 time<1ms TTL=128
Reply from 172.18.167.181: bytes=32 time<1ms TTL=128
Reply from 172.18.167.181: bytes=32 time=1ms TTL=128
Reply from 172.18.167.181: bytes=32 time<1ms TTL=128
Ping statistics for 172.18.167.181:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 1ms, Average = 0ms
I think it not working because the default port inside the container is not 8080. It's 80. So change your
docker run -d -p 8080:8080 test-iis-image
to
docker run -d -p 8080:80 test-iis-image
For more details refer to https://hub.docker.com/r/microsoft/iis/
Also you can view one of the main Dockerfiles here
https://github.com/microsoft/iis-docker/blob/main/windowsservercore-ltsc2022/Dockerfile

sbt-native-packager docker: How to add entry in /etc/hosts

I'm using the docker plugin of sbt-native-packager to build a Docker image. I would like my image to have an additional entry in /etc/hosts.
I've tried the following:
dockerCommands in Docker := dockerCommands.value.flatMap {
case cmd#Cmd("FROM", _) =>
List(Cmd("FROM", "anapsix/alpine-java")) ++ List(
Cmd("ENV", "JAVA_MIN_MEM", "1024m"),
Cmd("RUN", "echo 8.8.8.8 foo >> /etc/hosts")
)
}
Unfortunately it doesn't seem to work. When I start a container based on this image, the /etc/hosts does not have the extra entry.
It looks like it's actually writing the file because I tried the following instead:
....
Cmd("RUN", "echo 8.8.8.8 foo >> /etc/hosts; ping -c 4 foo")
....
And I'm getting as output the following:
[info] Step 9/15 : RUN echo 8.8.8.8 foo >> /etc/hosts; ping -c 4 foo
[info] ---> Running in b6d7ba25f96f
[info] PING foo (8.8.8.8): 56 data bytes
[info] 64 bytes from 8.8.8.8: seq=0 ttl=37 time=5.521 ms
[info] 64 bytes from 8.8.8.8: seq=1 ttl=37 time=3.188 ms
[info] 64 bytes from 8.8.8.8: seq=2 ttl=37 time=6.012 ms
[info] 64 bytes from 8.8.8.8: seq=3 ttl=37 time=4.192 ms
So it looks like the modified /etc/hosts is being overridden!
What is the correct way to do this?
The file /etc/hosts is managed by Docker and cannot be customized as part of building an image.
As you already figured out, you can add a custom entry using RUN echo 8.8.8.8 foo > /etc/hosts; <some_command_requiring_custom_hosts_file>. But this modification is only available during the execution of this particular RUN command.
In case you need custom entries when running containers use the --add-host parameter of docker run (see docs).
In general it is a best practise to not include configuration details in a Docker images. Applying configuration only at the time you are running your containers helps to keep the images portable.
Yes, Docker overrides that file (in reality, it is a file present in the host system that is mounted in that location when the container starts), so any change you do there will be overriden.
One option would be to change the docker entrypoint to, instead of pointing to your application startup script, point to a script that does that change and then runs your app startup script.
So in plain docker (sorry I have never used sbt docker plugin), instead of having an entry to your app start script (using /usr/bin/myapp):
ENTRYPOINT /usr/bin/myapp
you would have
RUN echo "echo 8.8.8.8 foo > /etc/hosts" >> /startup.sh
RUN echo "/usr/bin/myapp" >> /startup.sh
RUN chmod +x /startup.sh
ENTRYPOINT /startup.sh

Is there a configuration setting to automatically display jenkins console output

When I manually execute a jenkins job, it would save an annoying step if, upon submitting the build button, the next screen was the console output and not the project page.
Is there a configuration setting that allows this to happen?
No native option of that kind I guess. Recent jenkins builds allow a shorter way to access console output - just click on the gray/blue point left to the currently running build:
If you still think one click is annoying extra step, then next option I'd suggest is using jenkins CLI. It needs some more one-time effort though.
Have java installed on your workstation
Download jenkins-cli.jar from http://your jenkins host/jnlpJars/jenkins-cli.jar
Then you can start builds like that:
java -jar jenkins-cli.jar -s http://jenkins-url build buildname -w -s -v -p parameterN=valueN
java -jar jenkins-cli.jar -noKeyAuth -s http://jet:8080 build tst-so -w -s -v -p host2ping=google.com
Started tst-so #17
Started from command line by anonymous
Building in workspace /var/lib/jenkins/jobs/tst-so/workspace
[workspace] $ /bin/sh -xe /tmp/hudson5079113569382475588.sh
+ echo Hello from tst-so job
Hello from tst-so job
+ ping -c 6 google.com
PING google.com (216.58.209.206) 56(84) bytes of data.
64 bytes from bud02s22-in-f14.1e100.net (216.58.209.206): icmp_seq=1 ttl=54 time=51.6 ms
64 bytes from bud02s22-in-f14.1e100.net (216.58.209.206): icmp_seq=2 ttl=54 time=51.9 ms
64 bytes from bud02s22-in-f14.1e100.net (216.58.209.206): icmp_seq=3 ttl=54 time=51.8 ms
64 bytes from bud02s22-in-f14.1e100.net (216.58.209.206): icmp_seq=4 ttl=54 time=51.8 ms
64 bytes from bud02s22-in-f14.1e100.net (216.58.209.206): icmp_seq=5 ttl=54 time=51.8 ms
64 bytes from bud02s22-in-f14.1e100.net (216.58.209.206): icmp_seq=6 ttl=54 time=51.7 ms
--- google.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5009ms
rtt min/avg/max/mdev = 51.684/51.815/51.900/0.306 ms
Finished: SUCCESS
Completed tst-so #17 : SUCCESS
In my example jenkins is setup with no authentication for simplicity reason.

Resources