Scripts, road to PUMPS 2013

Purpose.

I want to left here a couple of Python scripts for connecting to a remote server or synchronizing folders between host and server without using an “smart client”, all from terminal.

Connecting with the server.

This small script connect your pc via SSH to the server. Pexpect carries out the login process, you should install it -very easy step- before continuing.

import pexpect
import os as os

'''
 Login.py scrip.

 Change only #fields
 @ #server_direction: something@something.es
 @ #password : your password
'''

def screen_logo ():
 print " "
 print "--------------------------------------------------------------------"
 print " Login message... write something hacker' style here! "
 print "--------------------------------------------------------------------"
 print " "

screen_logo()
child = pexpect.spawn ('ssh #server_direction -p 2222')
#this will be the server' answer, so could be totally different!
child.expect ("#server_direction's password:")
print "... Login, please wait a few seconds."
child.sendline ("#password")
child.interact()

Synchronization part.

This one is sightly more complicated due to the huge range of options  that rsync offers, but the minimal script is the following;
import pexpect
import os as os

'''
 Sync.py scrip.

 Complete these fields bellow
 @ server_direction: something@something.es
 @ password : your password
 @ server_path : destiny path
'''

def screen_logo ():

 print " "

 print "----------------------------------------------------------------------"
 print " Synchronizing files "
 print "----------------------------------------------------------------------"

 print " "

def close_transfer():
 print " "

 print "----------------------------------------------------------------------"
 print " Files synchronized! "
 print "----------------------------------------------------------------------"

 print " "

screen_logo()

child = pexpect.spawn ('rsync -rvuh -e "ssh -p 2222"
 --stats --progress --exclude "/sync_folder/."
 --exclude "/#optional_argument/.."
 /#host_path/ #server_direction:/#path')

#this will be the server' answer, so could be totally different!
child.expect ("#server_direction's password:")

print "... synchronizing, please wait a few seconds."

child.sendline ("password")
child.interact()

close_transfer()

You probably don’t need more complicated options out of these ones (excluding folders and sync directories recursively).

Cuda snippets for Vim (cooking right now!).

I’m working on that. This evening I’ll have five free hours in the train so this will be finished. Tonight I’ll post for all old school code-writers or modern nostalgics. ,)
Answer me here as usual!
Esta entrada fue publicada en Sin categoría. Guarda el enlace permanente.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s