How to do it...

Let us first set up the Fabric environment variables and then create two functions, one for downloading files and the other for uploading files.

Listing 6.6 gives the code for transferring files to a remote machine over SSH as follows:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition -- Chapter - 6 
# This program is optimized for Python 2.7.12 and Python 3.5.2. 
# It may run on any other version with/without modifications. 
 
 
from getpass import getpass 
from fabric.api import local, run, env, get, put, prompt, open_shell 
 
def remote_server(): 
    env.hosts = ['127.0.0.1'] 
    env.password = getpass('Enter your system password: ') 
    env.home_folder = '/tmp' 
 
def login(): 
    open_shell(command="cd %s" %env.home_folder) 
 
 
def download_file(): 
    print ("Checking local disk space...") 
    local("df -h") 
    remote_path = prompt("Enter the remote file path:") 
    local_path = prompt("Enter the local file path:") 
    get(remote_path=remote_path, local_path=local_path) 
    local("ls %s" %local_path) 
 
 
def upload_file(): 
    print ("Checking remote disk space...") 
    run("df -h") 
    local_path = prompt("Enter the local file path:") 
    remote_path = prompt("Enter the remote file path:") 
    put(remote_path=remote_path, local_path=local_path) 
    run("ls %s" %remote_path) 

In order to run this script, you should create a shortcut, fabfile.py. From the command line, you can do this by typing the following command:

$ ln -sfn 6_6_transfer_file_over_ssh.py fabfile.py
  

Then, you can call the fab executable in various forms.

First, to log on to a remote server using your script, you can run the following Fabric function:

$ fab remote_server login
  

This will give you a minimum shell-like environment. Then, you can download a file from a remote server to your local machine using the following command:

$ fab remote_server download_file  

Similarly, to upload a file, you can use the following command:

$ fab remote_server upload_file  

In this example, the local machine is used via SSH. So, you have to install the SSH server locally to run these scripts. Otherwise, you can modify the remote_server() function and point it to a remote server, as follows:

$ fab remote_server login
[127.0.0.1] Executing task 'login'
Linux debian6 2.6.32-5-686 #1 SMP Mon Feb 25 01:04:36 UTC 2013 i686
    
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright.
    
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
You have new mail.
Last login: Wed Aug 21 15:08:45 2013 from localhost
cd /tmp
faruq@debian6:~$ cd /tmp
faruq@debian6:/tmp$ 
    
<CTRL+D>
faruq@debian6:/tmp$ logout
    
Done.
Disconnecting from 127.0.0.1... done.
    
$ fab remote_server download_file
[127.0.0.1] Executing task 'download_file'
Checking local disk space...
[localhost] local: df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              62G   47G   12G  81% /
tmpfs                 506M     0  506M   0% /lib/init/rw
udev                  501M  160K  501M   1% /dev
tmpfs                 506M  408K  505M   1% /dev/shm
Z_DRIVE              1012G  944G   69G  94% /media/z
C_DRIVE               466G  248G  218G  54% /media/c
Enter the remote file path: /tmp/op.txt
Enter the local file path: .
[127.0.0.1] download: chapter7/op.txt <- /tmp/op.txt
[localhost] local: ls .
6_1_execute_remote_telnet_cmd.py   6_3_print_remote_cpu_info.py 6_5_run_mysql_command_remotely.py  6_7_configure_Apache_for_hosting_website_remotely.py  fabfile.pyc  __init__.py  test.txt
6_2_copy_remote_file_over_sftp.py  6_4_install_python_package_remotely.py  6_6_transfer_file_over_ssh.py fabfile.py                        index.html     op.txt       vhost.conf
    
Done.
Disconnecting from 127.0.0.1... done.
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.15.100