viernes, mayo 19, 2017

Paramiko - Python

pip install paramiko

>>> import paramiko
>>> ssh = paramiko.SSHClient()
>>> ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> ssh.connect('', username='usuario', password='clave')
>>> stdin, stdout, stderr = ssh.exec_command("ping -c 3")
>>> stdout.readlines()
[u'PING ( 56(84) bytes of data.\n', u'64 bytes from ( icmp_seq=1 ttl=49 time=203 ms\n', u'64 bytes from ( icmp_seq=2 ttl=49 time=204 ms\n', u'64 bytes from ( icmp_seq=3 ttl=49 time=203 ms\n', u'\n', u'--- ping statistics ---\n', u'3 packets transmitted, 3 received, 0% packet loss, time 6220ms\n', u'rtt min/avg/max/mdev = 203.043/203.732/204.230/0.503 ms\n']
>>> stdin.close()
>>> ssh.close()


viernes, mayo 12, 2017

Libreria Rpi.GPIO para Raspberry pi 3

sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio

import RPi.GPIO as GPIO
import time
GPIO.setup(17, GPIO.OUT) ## GPIO 17 como salida
GPIO.setup(27, GPIO.OUT) ## GPIO 27 como salida
def blink():
        print "Ejecucion iniciada..."
        iteracion = 0
        while iteracion < 30: ## Segundos que durara la funcion
                GPIO.output(17, True) ## Enciendo el 17
                GPIO.output(27, False) ## Apago el 27
                time.sleep(1) ## Esperamos 1 segundo
                GPIO.output(17, False) ## Apago el 17
                GPIO.output(27, True) ## Enciendo el 27
                time.sleep(1) ## Esperamos 1 segundo
                iteracion = iteracion + 2 ## Sumo 2 hay dos parpadeos
        print "Ejecucion finalizada"
        GPIO.cleanup() ## Hago una limpieza de los GPIO 


lunes, marzo 06, 2017

Covert FLAC to MP3 from the terminal

how to convert flac to mp3 audio format on linux mint using the command console

first be sure to remove any space in the files name, the follow command will remove spaces and put instance an underscore character:
find -name "* *" -type f | rename 's/ /_/g'

now be sure you have installed flac and lame
sudo apt-get install flac
sudo apt-get install lame

now we are ready to convert all the flac files on teh folder to mp3:
for f in *.flac;do flac -cd $f |lame -b 192 - $f.mp3;done

lunes, febrero 20, 2017

Control SSH Session with Python

$ pip install pexpect

the script:
from pexpect import pxssh
import getpass
    s = pxssh.pxssh()
    hostname = raw_input('hostname: ')
    username = raw_input('username: ')
    password = getpass.getpass('password: ')
    s.login(hostname, username, password)
    s.sendline('uptime')   # run a command
    s.prompt()             # match the prompt
    print(s.before)        # print everything before the prompt.
    s.sendline('ls -l')
except pxssh.ExceptionPxssh as e:
    print("pxssh failed on login.")

domingo, enero 01, 2017

Sending command from one terminal to another

to get the terminals open:

$ who

to get current terminal:
$ tty

to send command to terminal /dev/pts/11

$ echo test >& /dev/pts/11