Category: Software

  • Ping with timestamp

    I’m having issues with my internet connection lately. My modem seems to reset it self from time to time. It can work days without issues and then it starts to reset a couple of times without any warning.

    The internet provider sees nothing wrong with the modem. A mechanic needs to come by, but until then i want to be able to monitor the device.

    I’ve installed LibreNMS to monitor my connection. Every 5 minutes it checks by sending a ping to the router and another test is to ping 8.8.8.8 (one of the dns servers of google). It works like a charm. But sometimes the modem is restarted within 5 minutes. When i’m behind my PC i’ve started to run the ping command, and that works fine, only one thing annoyed me: I can’t see when the ping command tries to send an ping to the devices.

    Thanks to the blog of: https://www.raymond.cc/blog/timestamp-ping-with-hrping/, i’m running a ping command preceded with a time stamp:
    Ping From PowerShell:

    Ping.exe -t 192.168.178.1 | ForEach {“{0} – {1}” -f (Get-Date),$_}
    Ping.exe -t 8.8.8.8 | ForEach {“{0} – {1}” -f (Get-Date),$_}
    

    With this you get something like this:

    1/10/2020 8:51:05 PM – Reply from 8.8.8.8: bytes=32 time=19ms TTL=55
    1/10/2020 8:51:06 PM – Reply from 8.8.8.8: bytes=32 time=11ms TTL=55
    1/10/2020 8:51:07 PM – Reply from 8.8.8.8: bytes=32 time=12ms TTL=55
    1/10/2020 8:51:08 PM – Reply from 8.8.8.8: bytes=32 time=12ms TTL=55
    1/10/2020 8:51:09 PM – Reply from 8.8.8.8: bytes=32 time=21ms TTL=55
    1/10/2020 8:51:10 PM – Reply from 8.8.8.8: bytes=32 time=28ms TTL=55
    1/10/2020 8:51:11 PM – Reply from 8.8.8.8: bytes=32 time=11ms TTL=55

    That works like a charm!!

  • Retrieve FQDN from IP numbers in Nginx access log

    I wanted to write a script for expanding my Python knowledge. I ussually create small scripts to accommodate my “problems” or wishes.

    If you want use it:

    import socket
    
    FQDN = []
    ACC = []
    
    ACC_FILE = open('/var/log/nginx/access.log')
    for LINE in ACC_FILE:
            ACC.append(LINE.split(' ')[0])
    
    ACC.sort()
    ACC = set(ACC)
    for LINE in ACC:
            ADDR = socket.getfqdn(LINE)
            FQDN.append(ADDR)
    
    FQDN.sort()
    for SERVER in FQDN:
            print(SERVER)
    
  • Thanks to my birthday gift i created my first python script

    A couple of weeks back was my birthday and although i don’t really celebrate, my daughter wanted to get me a gift. Because we’ve gotten a new second hand car with an old tape deck that doesn’t support aux or something, i saw a so-called FM Transmitter. This is a device you place in you cigarette lighter in the car and you can connect a phone (over Bluetooth or AUX cable) or a simple USB stick. The AUX connection is crap! A lot of noise, but the BT and USB connection are oké. Main thing that i found as a disadvantage is that when you play music over the USB stick the music isn’t random. If you don’t like an album, you have to skip through al the songs of the album and hope you get a nice record after going through.

    To solve this (and to learn the Python scripting language) i’ve created the next script:

    import os
    from shutil import copyfile
    from random import randint
    import pathlib
    
    MP3_FOLDER=r'Source  folder'
    DEST_FOLDER = 'Destination Folder'
    FileCount = 1
    for root,dirs,files in os.walk(MP3_FOLDER):
        for file in files:
            if file.endswith('.mp3'):
                print(FileCount)
                FileCount = FileCount + 1
                rndFolder = DEST_FOLDER+str(randint(100, 999))
                rndFile = randint(10000, 99999)
                pathlib.Path(rndFolder).mkdir(parents=True, exist_ok=True)
                srcfile = os.path.join(root,file)
                dstfile = rndFolder+'\\'+str(rndFile)+'.mp3'
                copyfile(srcfile,dstfile)</pre>

    It scans the source folder and for every file that ends with ‘.mp3’ it generates a random number between 100 and 999, and creates a folder of it. Then it generates a random number between 10000 and 99999 and makes a file out of it with the extension ‘.mp3’. In my situation there are 5000+ mp3 files, so there will be a descent amount of directories, with different filenames in them. When the files are done, i can copy them from the destination folder to my USB stick. Of course the destination folder can already be the USB stick.

    If you are interested in the FM Transmitter, search for ‘BC06’. This is a very generic FM transmitter. I think you can buy them in China and sell them under your own brand. I’ve seen them from MFEEL, Kebidumei, Kebidu etc etc etc.

  • Print MSSQL TSQL output immediately

    Sometimes when you make a SQL script, that loops through a lot of records, you want to print a status. When you do this using print (or select), this will only display the content when the buffer is full. To immidiatly print everything to the screen use:

    RAISERROR ('here', 0, 1) WITH NOWAIT

    This will flush the buffers to screen and so you will see the output immediately.

  • MongoDB Restore

    This weekend our standby had issues with our backup shares, and mainly with the backups from MongoDB. These weren’t compressed or something. When we started using MongoDB we were at version 3.0, so compression (–gzip) wasn’t implemented yet. Now we are at version 3.2, compression is possible so we started implementing it. Because we wanted to test the backup AND restore. Backup wasn’t really an issue, changed the script and added –gzip and executed it.

    Restoring was another issue. When starting the command it gave the error “Failed: no reachable servers”. Strang thing was connecting to the server using the mongo command didn’t give an error. After searching a bit i found that removing the parameter “–replSet” should fix the issue. After removing the setting in the service (we are running MongoDB under Windows) and restarting the service, issues were solved. The restore went fine. After the test i added the parameter again and restarted the service.

  • Zeal and CodeLite a perfect couple….. finally/again

    When i script (i don’t program), i do this in CodeLite. This is a nice bit of software (open source) that does the trick for me. Only the last couple of months i couldn’t get Zeal working. The strange thing was that when i pressed F1 it just started Zeal and it didn’t search the part i had selected. This is easy when you don’t know all possible options for a HTML tag, or a PHP command. Had tried several things, even searched online. Eventually i got it working again. And if you want to know how, here it is:

    First check in Zeal if you don’t have the following option set (especially to F1):
    Start Zeal, go to General, under the head “Global Shortcuts“. This should be anything except F1. (It shouldn’t be a lot more, but for this example it shouldn’t be F1), click apply and OK.

    Next part:
    Find the directory that Zeal is installed. Easiest way is to click start, enter Zeal in the “Search programs and files” (W7) and then right click on the Zeal application icon, and the properties. In the new little screen there is a “Start in” box, copy the contents and click cancel. Click on the start button again and type “cmd” (without the “) and hit enter. In the black box type in “cd ” (including the space). Right click in the black screen and select in the menu “Paste” and hit enter. (Please note CTRL+V doesn’t work here). If everything is correct you are in the working directory from Zeal.
    type the following commands:
    “zeal.exe –unregister” <enter>
    “zeal.exe –register” <enter>
    if everything went correctly Zeal and CodeLite should be playing nice again. In the black box type in “exit” <enter>. The black box closes again, start CodeLite again and happy scripting/programming/whatever you do in CodeLite.

    Hopefully this can help you further, and “if no one else can help you, maybe you can hire the A-team!” 😉