Saturday, 20 November 2021

Python convert hex to ascii

I've been having trouble with this simple task of converting a string of hexadecimal digits to text. My research and testing were insufficient. While the characters are not from extended ascii table, this is a easy task. But my strings can and will include letters like "áðéíóþæ". The sting comes from snmp and in this example I will be using the word "Ísland" for this demo hex "cd 73 6c 61 6e 64" Here is one of the most promising code snipped

 
import binascii

def hex_to_ascii(hex_str):
    hex_str = hex_str.replace(' ', '').replace('0x', '').replace('\t', '').replace('\n', '')
    ascii_str = binascii.unhexlify(hex_str)
    return ascii_str
 
hex_input = 'cd 73 6c 61 6e 64'
ascii_output = hex_to_ascii(hex_input)

print(format(ascii_output))

The output was b'\xcdsland' so it does not encode the first letter like all me previous tempts. Finally I found this snipped:
 
>>> L = [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100]
>>> ''.join(chr(i) for i in L)
'hello, world'

Next step was to fit join(chr) in my snipped like so..
   
import binascii

def hex_to_ascii(hex_str):
    hex_str = hex_str.replace(' ', '').replace('0x', '').replace('\t', '').replace('\n', '')
    ascii_str = binascii.unhexlify(hex_str)
    return ascii_str
 
hex_input = 'cd 73 6c 61 6e 64'
ascii_output = hex_to_ascii(hex_input)
ascii_output = ''.join(chr(i) for i in ascii_output)

print(format(ascii_output))

--cheers
Wednesday, 10 November 2021

Command line upload to Dropbox

Dropbox OAuth Guide
It is sometimes useful to upload a file or files to Dropbox from the terminal or even by script. This snippet uploads a file to the directory's root.
 
curl -X POST https://content.dropboxapi.com/2/files/upload --header "Authorization: Bearer <Auth code> " --header "Dropbox-API-Arg: {\"path\": \"/filename.png\",\"mode\": \"add\",\"autorename\": true,\"mute\": false,\"strict_conflict\": false}" --header "Content-Type: application/octet-stream" --data-binary @filename.png
Monday, 8 November 2021

Convert an image and audio file to a video with ffmpeg

I needed a command line script to merge an image file with an audio file and generate a video file video.mp4 The first attempt was basic.
ffmpeg -loop 1 -i image.jpg -i audio.mp3 -c:a copy -c:v libx264 -shortest video.mp4
In most cases, this would do, but the image dimensions were not divisible by 2 resulting in an error.
[libx264 @ 0x56450842d500] height not divisible by 2 (640x427)
Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height
Conversion failed!
The current working version of the command line is as follows:
ffmpeg -loop 1 -i image.jpg -i audio.mp3 -vf "scale=2*trunc(iw/2):2*trunc(ih/2),setsar=1" -c:a copy -c:v libx264 -shortest video.mp4

Example:


Tuesday, 29 September 2020

How to disable the Caps Lock key on Ubuntu

Keyboard

I dont like Caps Lock, it's the the most unnecessary key on the keyboard in my opinion. I hardly ever write more than couple of words in capitals, but often accidently hit the key; suddenly finding myself TYPING IN ALL CAPS LIKE A SCREAMING MONKEY. Here’s an easy way to disable your Ubuntu’s Caps Lock key.

1. Install dconf

I already had this installed
$ sudo apt install dconf-tools

2. Disable caps lock (reenable by pressing both shift keys at once)

$ setxkbmap -option "caps:none"
$ setxkbmap -option "shift:both_capslock"
I did only disable for now.

-- UPDATE
This works but running this automatically is of course what was missing in this post so I added step 3 

3. Add this to your .bashrc file

vim .bashrc

Wednesday, 2 September 2020

Connecting to MS SQL server with pyodbc


Small project I'm working on at work requires connection to MS SQL server. For the task I'm using python3 with pyodbc library and I got stuck for a while trying to connect. looks like it has to do with the ODBC Driver 17 for SQL Server. After some time searching for solution, I found FreeTDS.

First install FreeTDS on your Linux:
apt install tdsodbc freetds-bin
Next configure FreeTDS by adding this to /etc/odbcinst.ini
[FreeTDS]
Description=FreeTDS
Driver=/usr/lib/x86_64-linux-gnu/odbc/libtdsodbc.so
Setup=/usr/lib/x86_64-linux-gnu/odbc/libtdsS.so
Finally activate
odbcinst -i -d -f /etc/odbcinst.ini
And now I can run pyodbc in my code
import pyodbc
connection = pyodbc.connect('DRIVER={FreeTDS};'
	'Server='+dbhost+';'
	'Database='+dbname+';'
	'UID='+dbuser+';'
	'PWD='+dbpass+';'
	'TDS_Version=8.0;'
	'Port=1433;')
    
cursor = connection.cursor()
cursor.execute("SELECT * FROM tablename; ")

for row in cursor:
	print(row)
Thursday, 23 April 2020

How to fix a "cURL error 28: Connection timed out" in WordPress?



My Wordpress Site Health Status check showed me some critical issues that I needed to take further look at. "The REST API request failed due to an error."

The REST API request failed due to an error

Error: cURL error 28: Operation timed out after 10000 milliseconds
 with 0 bytes received (http_request_failed)

After a lot of search results that did NOT lead me to the right direction I finally stumbled up on a short article about this subject. A few items in a list on "How to Fix" this. The most interesting line in the article mentioned the Query Monitor plugin to check the status of the HTTP API Calls in the admin page where the error is displayed. And that vas the single most helpful plugin I ever found. Within a minute I had identified the problem the problem which was I my case the WP Social Avatar plugin.

wp-social-avatar error

This Query Monitor plugin saved my day, but mostly a lot of time.

*** EDIT *** It turned out I had second plugin causing the same error. "PHP Code For Posts" that has has been closed and is no longer available for download.

--cheers
Monday, 7 October 2019

No default realm defined for Kerberos


When you connect to some old cisco routers using iterm2 on an OS X, it might takes some time before it prompts for a username. And it might also promt you with this message.
Kerberos: No default realm defined for Kerberos! 
To disable this message you need to create or edit the file .telnetrc in your home directory and add default unset autologin
$ vim ~/.telnetrc
  default unset autologin
esc : wq