Monthly Archives: September 2013

Send Commands or Data to Other Terminals

Somehow I need to send commands from one terminal to another. Luckily I found this post which saves my time. Here is a set of steps that one need to follow:

Copy the code below to a C file (e.g. ttyecho.c)

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>

void print_help(char *prog_name) {
        printf("Usage: %s [-n] DEVNAME COMMAND\n", prog_name);
        printf("Usage: '-n' is an optional argument if you want to push a new line at the end of the text\n");
        printf("Usage: Will require 'sudo' to run if the executable is not setuid root\n");
        exit(1);
}

int main (int argc, char *argv[]) {
    char *cmd, *nl = "\n";
    int i, fd;
    int devno, commandno, newline;
    int mem_len;
    devno = 1; commandno = 2; newline = 0;
    if (argc < 3) {
        print_help(argv[0]);
    }
    if (argc > 3 && argv[1][0] == '-' && argv[1][1] == 'n') {
        devno = 2; commandno = 3; newline=1;
    } else if (argc > 3 && argv[1][0] == '-' && argv[1][1] != 'n') {
        printf("Invalid Option\n");
        print_help(argv[0]);
    }
    fd = open(argv[devno],O_RDWR);
    if(fd == -1) {
        perror("open DEVICE");
        exit(1);
    }
    mem_len = 0;
    for ( i = commandno; i < argc; i++ ) {
        mem_len += strlen(argv[i]) + 2;
        if ( i > commandno ) {
            cmd = (char *)realloc((void *)cmd, mem_len);
        } else { //i == commandno
            cmd = (char *)malloc(mem_len);
        }

        strcat(cmd, argv[i]);
        strcat(cmd, " ");
    }
  if (newline == 0)
        usleep(225000);
    for (i = 0; cmd[i]; i++)
        ioctl (fd, TIOCSTI, cmd+i);
    if (newline == 1)
        ioctl (fd, TIOCSTI, nl);
    close(fd);
    free((void *)cmd);
    exit (0);
}

Run the following command in the directory where the C file (ttyecho.c) has been created

make ttyecho

The above command will create an executable file named ttyecho Copy this file to the bin directory under your Home Directory. In my case it is /home/pi/bin . Create the bin directory if it doesn’t exist. It is a good practice to keep all custom binaries/executables in this bin directory.

Now change the owner/group to root using the following commands

sudo chown root:root ttyecho
sudo chmod u+s ttyecho

The above two commands change the owner/group to root and set the setuid bit for the executable which will allow you to run the utility with root permissions.

Start another terminal or switch to another existing terminal that you wish to control and execute the command tty in order to determine the terminal. You can see a sample output below.

@~$ tty
/dev/pts/5

Now to execute a command on /dev/pts/5, run the following command in the controlling/original terminal.

ttyecho -n /dev/pts/5 ls

You will see that the ls command is executed in /dev/pts/5. The -n option makes ttyecho send a newline after the command, so that the command gets executed and not just inserted.

 

 

Raspberry Pi – hiding boot messages on screen

There may be a time when one would want to hide boot messages that show up on the screen (or monitor or TV). The trick is to edit /boot/cmdline.txt file.

One may use the following command

sudo nano /boot/cmdline.txt

Then make the following changes to the file:

- Replace "console=tty1" by "console=tty3" to redirect boot messages to the third console.
- Add "loglevel=3" to disable non-critical kernel log messages.

After changing, to get to the console simply press Alt + F3 during or after boot. However, if text cursor needs to be hidden also then add “vt.global_cursor_default=0” to the /boot/cmdline.txt file. In my case, I prefer to have text cursor as an indicator that it is actually booting.

Here is the content of my /boot/cmdline.txt

dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 console=tty3 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait logo.nologo loglevel=3

Information in this page is taken from http://www.raspberrypi.org/phpBB3/viewtopic.php?t=13807&p=153177

Setting up Wifi (EDIMAX) on Raspberry Pi

This post is adapted from http://kerneldriver.wordpress.com/2012/10/21/configuring-wpa2-using-wpa_supplicant-on-the-raspberry-pi/

First you need to see if the EDIMAX usb Wifi is recognized by using command

lsusb

Once you see the device connected, edit the file /etc/network/interfaces (requires root)

....

auto wlan0
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

iface default inet dhcp

Then edit /etc/wpa_supplicant/wpa_supplicant.conf (requires root):

network={
    ssid="Your SSID Here"
    proto=RSN
    key_mgmt=WPA-PSK
    pairwise=CCMP TKIP
    group=CCMP TKIP
    psk="YourPresharedKeyHere"
}

After that, you will need to bring up the “wlan0″ interface using “ifup” (requires root):

$ sudo ifup wlan0

You may see the following messages like this:

ioctl[SIOCSIWAP]: Operation not permitted
ioctl[SIOCSIWENCODEEXT]: Invalid argument
ioctl[SIOCSIWENCODEEXT]: Invalid argument

You may ignore the messages but the adapter should be usable now