Let's make the world a better place using digital technologies!

Category: Linux

Samsung TV Remote Control over LAN/WLAN using Python

For quite some time, I have been looking for a method to control my Samsung UE55C8000 TV using Linux scripts. Finally, I found this forum post on SamyGo that provides an example using Perl. As I like to do scripting in Python, so I have translated the script to Python with some minor changes. As you can see in the following Python code, the script contains Key names for all the remote control keys and even some extra. You should be able to easily modify the script to your needs. The script can also be downloaded on my download page.

#!  /usr/bin/python
#   Title: samsungremote.py
#   Author: Asif Iqbal
#   Date: 05APR2012
#   Info: To send remote control commands to the Samsung tv over LAN
#   TODO:

import socket
import base64
import time, datetime

#IP Address of TV
tvip = ""
#IP Address of TV
myip = ""
#Used for the access control/validation, but not after that AFAIK
mymac = "00-0c-29-3e-b1-4f"
#What the iPhone app reports
appstring = "iphone..iapp.samsung"
#Might need changing to match your TV type
tvappstring = "iphone.UE55C8000.iapp.samsung"
#What gets reported when it asks for permission
remotename = "Python Samsung Remote" 

# Function to send keys
def sendKey(skey, dataSock, appstring):
 messagepart3 = chr(0x00) + chr(0x00) + chr(0x00) + chr(len(
base64.b64encode(skey))) + chr(0x00) + base64.b64encode(skey);
 part3 = chr(0x00) + chr(len(appstring)) + chr(0x00) \
+ appstring + chr(len(messagepart3)) + chr(0x00) + messagepart3

# Open Socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((tvip, 55000))

# Key Reference
# Normal remote keys
 #KEY_W_LINK #Media P
 #KEY_RSS #Internet
 #KEY_MTS #Dual
# Bonus buttons not on the normal remote:
#Don't work/wrong codes:

# First configure the connection
ipencoded = base64.b64encode(myip)
macencoded = base64.b64encode(mymac)
messagepart1 = chr(0x64) + chr(0x00) + chr(len(ipencoded)) \
+ chr(0x00) + ipencoded + chr(len(macencoded)) + chr(0x00) \
+ macencoded + chr(len(base64.b64encode(remotename))) + chr(0x00) \
+ base64.b64encode(remotename)

part1 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
+ chr(len(messagepart1)) + chr(0x00) + messagepart1

messagepart2 = chr(0xc8) + chr(0x00)
part2 = chr(0x00) + chr(len(appstring)) + chr(0x00) + appstring \
+ chr(len(messagepart2)) + chr(0x00) + messagepart2

# Now send the keys as you like, e.g.,

# Close the socket when done


Continue Reading

Remote Desktop to a Virtual Machine on VirtualBox running on a Headless Server

I have followed the excellent directions given by Falko Timme at howtoforge. I am copying his directions here with some changes to install a Windows 7 system.

First you need to add the VirtualBox repo to the Ubuntu resources.lst:
emacs /etc/apt/sources.list
Add the following line to it:
deb http://download.virtualbox.org/virtualbox/debian oneiric contrib

Then download and install the VirtualBox public key:
wget -q http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
Update our package database:
apt-get update
Afterwards, install the VirtualBox 4.1 as follows:
apt-get install linux-headers-$(uname -r) build-essential virtualbox-4.1 dkms

Now install the VirtualBox extensions.

cd /tmp
wget http://download.virtualbox.org/virtualbox/4.1.10/Oracle_VM_VirtualBox_Extension_Pack-4.1.10-76795.vbox-extpack
VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.1.8-75467.vbox-extpack

Now add the user who will run VirtualBox to the vboxusers group:
adduser root vboxusers

Now get a Windows 7 iso and do the following steps to install it to a virtual machine:

VBoxManage createvm --name "Windows 7" --register
VBoxManage modifyvm "Windows 7" --memory 2048 --acpi on --boot1 dvd --nic1 bridged --bridgeadapter1 eth0
VBoxManage createhd --filename windows7.vdi --size 50000
VBoxManage storagectl "Windows 7" --name "IDE Controller" --add ide
VBoxManage storageattach "Windows 7" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium /mnt/Windows/windows7.vdi
VBoxManage storageattach "Windows 7" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /mnt/Windows/Windows7Ultimate.iso
VBoxHeadless --startvm "Windows 7"

You can now connect using Remote Desktop to this virtual machine.

Continue Reading

Useful tmux Commands

tmux is a powerful console window manager. The most valuable feature of tmux is the ability to detach from a running tmux session, exit the ssh session, relogin to ssh again and resume the tmux session. This is simply beautiful. Here I list some handy tmux commands.

  • tmux
    starts the tmux.
  • ^b d
    detaches from a running tmux session.
  • tmux attach
    attaches to a running tmux session.
  • ^b ?
    lists all the key-bindings. Great help.
  • ^b %
    splits the window into two vertical panes.
  • ^b c
    creates a new window.
    switches to right/left panes.
    scrolls the buffer up/down.
  • ^b d
    detaches the current session.
  • tmux attach -d
    attaches to a tmux session, detaching the other clients.

Thank you tmux-team for developing such a useful program.

Continue Reading

Useful rsync Commands

rsync -avn /pathtofolder/'Source Folder' /pathtofolder/'Destination Folder'
dry-runs a mirror of Source Folder to the Destination Folder.

rsync -av /pathtofolder/'Source Folder' /pathtofolder/'Destination Folder'
mirrors Source Folder to the Destination Folder.

rsync -avt --progress /pathtofolder/'Source Folder' /pathtofolder/'Destination Folder'
does the same as above, in addition maintaining time-stamps and printing progress.

Continue Reading

MTS/M2TS to MKV (Especially for Sony HDR-SR12)

After spending many frustrating hours, I finally found a solution to easily re-mux m2ts/mts files produced by Sony HDR-SR12 or other similar camcoders in order to stream them smoothly over DLNA to Samsung C-Series (55C8000) TV in full HD quality. But the solution is generic and can be used for any rendered.


First the problem: Above mentioned Sony camcoder produces 1920×1080, 60i video. However, the time-stamps on the frames in each interlaced field are wrong, both frames of a filed having same time-stamp. This is very problematic for almost all rendering devices.
My first approach was to re-encode these mts files using Handbrake to convert to m4v. This worked though at a heavy computing cost. The conversion is very intensive and for 50GB of videos could easily take more than 12 hours on a core-2 processor. One day, I was browsing Ubuntu forums, I came across this thread. This actually produced the re-muxed file in a fraction of a second as opposed to re-encoding with Handbrake that took very long time.
But there are two problems in the above solution:

  • It is using mencoder + lavf, a known broken combination in presence of B-frames.
  • It does not allow to copy the subtitle track (PGS) that Sony camcoder produces. This subtitle track is very important as it contains the recording date/time
    information which will be valuable when using these videos in future.


The following solution overcomes all the above-mentioned problems, is rock-stable and preserves the sub-title stream as well. I use ffmpeg to simply copy all the streams and output them in an mkv container. No information is lost/re-encoded. Here is the simple answer (as often observed in scientific research, the useful answer to a complex problem can often be stated very simply ):
ffmpeg -i input.mt2s -scodec copy -acodec copy -vcodec copy -f matroska input.mkv

I have written a script that converts all the m2ts files in a subfolder to mkv. The script is as below:
# m2tstomkv_subs (Fix the faulty fps (dual frames in the interlaced fields with same time-stamps) and repackage in matroska mkv container)
time {
# remux m2ts movies into mkv with ffmpeg
for i in *.m2ts; do
ffmpeg -i $i -scodec copy -acodec copy -vcodec copy -f matroska `basename $i .m2ts`.mkv;
echo "Conversion done";

The conversion is lightening fast. Finally I am able to watch the videos in their true quality. I will be glad if this information helps someone facing similar issues.

Continue Reading

Horde Keeps Rejecting the Login

If you unable to login to your horde (groupware webmail edition, or other versions as well) installation and receive messages like:

Login failed because your username or password was entered incorrectly.

or in /tmp/horde.log file you have something similar to:

Oct 29 12:18:42 HORDE [error] [imp] FAILED LOGIN for iqa [100.xx.xx.xx] to {localhost:143 [imap/notls]} [pid xxxxx on line 139 of “/var/www/horde/imp/lib/Auth/imp.php”]

then before digging deeper into horde internals, it is worthwhile to check whether your imap daemon (dovecot-imapd in my case) is running because if you have configured in many installations imap is the default authentication mechanism that horde uses. It can be checked quite easily by issuing:
telnet localhost 143
And if the response is like the following:
telnet: Unable to connect to remote host: Connection refused

then you do not have an imap daemon running. And that is preventing the horde to authenticate. In this case, you could install dovecot-imapd which will serve as the imap daemon and would provide the desired authentication mechanism for horde.

Continue Reading

Access Samba share over SSH Tunnel

  1. Add the Loopback Adapter to the Windows client machine.
  2. Configure this adapter as following:
    • Disable ‘File and Printer Sharing for Microsoft Networks’.
    • Choose appripriate IP address and netmask, such as: and
    • Under ‘Advanced’ settings, check ‘Enable LMHOSTS Lookup’ and ‘Disable NetBIOS over TCP/IP’
  3. Under Putty, configure the SSH Tunnel as:
    Source port:
    Destination: localhost:139
  4. Test the connection by entering \\ at Run prompt in Windows.
Continue Reading

Samba Print Server Problem

I tried connecting to my samba print server from my xp notebook. The printer was always visible, connectable and accepted documents for printing but never led to a physical print. I could solve it by visiting:
and adding the printer to CUPS. It worked immediately!

Continue Reading

Putting Hard Disks to Sleep

In Ubuntu server, you can put any disk that is not used for a give period of time to sleep using hdparm command. Simply do something like:
hdparm -S 20 /dev/sdb
hdparm -S 20 /dev/sda

to put the disks to sleep after 100 seconds (20 x 5)

To have it programmed automatically on bootup, write the same lines into /etc/rc.local.

Continue Reading

Configuring PC-39 Remote Control from ASUS MyCinema P7131 for XBMC

You have to understand the mechanism first:

1. The xbmc recognizes lircd as a generic remote control.
2. lirc further can connect to various remotes based on configuration in /etc/lircd.conf and present them as lirc remote to xbmc.

Now, first of all, you have to know what is the name of your remote device in linux. For this, you need to install input-utils:
apt-get install input-utils

Now when you say:
it shows you all the input devices on your system. In my case, among other, I see:
bustype : BUS_PCI
vendor : 0x1043
product : 0x4876
version : 1
name : "saa7134 IR (ASUSTeK P7131 Hybri"
phys : "pci-0000:05:00.0/ir0"
bits ev : EV_SYN EV_KEY EV_REP

This shows that the remote is indeed connected to the system and recognized as /dev/input/event6 device. This device number may change among different boots. We shall fix it by a trick in the lirc configuration.

Now if you want to see whether the events from your remote are being received by the system or not, issue the following:
input-events -g -t5 6
This shows the events received from event6 device with a 6s timeout. Press the buttons on your remote and you should see the codes received on the console.

If you want to see how the remote is mapping keys to keycodes, then isssue the following command:
input-kbd 6 > keyMapIR.txt
where 6 indicates the device id. You might have a different one.

In case, you decide to change this encoding then you can change the keycodes in the keyMapIR.txt file and upload it back to the remote as:
input-kbd -f keyMapIR.txt 6
But normally this will not be required.

Now that you know that your device is connected and working properly, we can proceed to install lirc.
apt-get install lircd
During its installation, please choose the appropriate remote make. In my case, I chose ASUS MyCinema. That sets it. Now lirc waits for a connection on a given address. We do not go in this detail. If you look in the /etc/lirc/lircd.conf, you would see something like:
After tweaking, I have this file as following:
begin remote
name lirc.conf
bits 16
eps 30
aeps 100
one 0 0
zero 0 0
pre_data_bits 16
pre_data 0x8001
gap 135991
toggle_bit_mask 0x80010188
begin codes
Rec 0x00A7
Close 0x0074
Prev 0x019C
Stop 0x0080
Next 0x0197
Rew 0x00A8
PlayPause 0x0077
Fwd 0x00D0
Up 0x0067
Down 0x006C
Enter 0x8001001C
Left 0x0069
Right 0x006A
Vol+ 0x0073
Vol- 0x0072
Ch+ 0x0192
Ch- 0x0193
Back 0x00AE
1 0x0002
2 0x0003
3 0x0004
4 0x0005
5 0x0006
6 0x0007
7 0x0008
8 0x0009
9 0x000A
FullScreen 0x0174
0 0x000B
Recall 0x0070
Tv 0x0179
Video 0x0189
Home 0x0066
Dvd 0x0185
Picture 0x016E
DvdMenu 0x008B
Radio 0x0181
Music 0x0188
Mute 0x0071
end codes
end remote

Please note that the names of the buttons have been changed a bit to match with the xbmc configuration. Now after changing this file and having restarted lircd, you should issue irw command and press buttons on the remote to check the proper encoding of the keys by lirc. If everything is perfect, then we can now proceed to the xbmc configuration.

The file that we want to edit is /usr/share/xbmc/system/Lircmap.xml. Here you should add a section like the following:
<remote device="lirc.conf">

The device name “lirc.conf” is the same that you see when you run irw command. You can see that the keys correspond to the keys in lircd config file.

Finally, we want lircd to always connect to the right device even if its device number changes after reboot. To this end, we need to change the /etc/init.d/lirc as follows:

build_remote_args ()

#For remote only detection support, we need
if [ -z "$REMOTE_DEVICE" ] && [ -z "$TRANSMITTER_DEVICE" ] && [ -c $dev ]; then

#If we have a REMOTE_DEVICE or REMOTE_DRIVER defined (either because no devices
#were defined, OR if we explicitly did), then populate REMOTE_ARGS
if [ ! -z "$REMOTE_DEVICE" ] || [ ! -z "$REMOTE_DRIVER" ]; then
if [ -n "$REMOTE_DEVICE" ] && [ "$REMOTE_DEVICE" != "none" ]; then
if [ -n "$REMOTE_DRIVER" ] && [ "$REMOTE_DRIVER" != "none" ]; then

#Now, if we ALSO have a transmitter defined, add some args
#To make the first lircd listen up
if [ ! -z "$TRANSMITTER_DEVICE" ] || [ ! -z "$TRANSMITTER_DRIVER" ]; then

whereas $REMOTE_DEVICE is defined as REMOTE_DEVICE=’saa7134*’ in /etc/lirc/hardware.conf. This step makes sure that after a reboot, the lircd always connects to the IR device regardless of its device number assigned by linux kernel.

Just in case it is of help to someone, here is my /etc/lirc/hardware.conf file:

# /etc/lirc/hardware.conf
#Chosen Remote Control
REMOTE=”Asus MyCinema P7131″

#Chosen IR Transmitter

#Enable lircd

#Don’t start lircmd even if there seems to be a good config file

#Try to load appropriate kernel modules

# Default configuration files for your hardware if any

#Forcing noninteractive reconfiguration
#If lirc is to be reconfigured by an external application
#that doesn’t have a debconf frontend available, the noninteractive
#frontend can be invoked and set to parse REMOTE and TRANSMITTER
#It will then populate all other variables without any user input
#If you would like to configure lirc via standard methods, be sure
#to leave this set to “false”

Continue Reading