Showing posts with label Linux. Show all posts
Showing posts with label Linux. Show all posts

Friday, July 15, 2011

Automating Remote File Copy

When you need to move files around inside a secure environment, rcp and ftp scripts can be a real time saver.

rcp

The easiest way to copy a file from any machine to a destination host without using password is to use the .rhosts file. This file should reside in a user's home directory. For example, for user "foo" on host "dest", this file will contain this line:


+ foo


Now you can remote copy files from anywhere to host "dest" without typing a password:

rcp file foo@dest:/home/foo/.

Scripting ftp

The trick to script ftp command is to turn off the interactive mode by using the "-n" option. This allows you to put password in the script. Here is an example to ftp a file from foo's home directory:


#!/bin/sh
ftp -n dest  << whatever
user foo foopass
bin
get file
bye


These two little commands can do wonders in a heterogeneous but secure environment with mixed Linux/Unix/Windows boxes. Use them to your heart's content.

Sunday, March 20, 2011

Socket and O_NONBLOCK

It is often stated that a socket is created in blocking mode by default. However, not all implementations of the socket calls adhere to that behavior. Specialized operating systems can be particularly tricky when it comes to blocking and non-blocking mode . One should always test the O_NONBLOCK flag on a socket to be sure if it is in the blocking or nonblocking mode operation before proceeding to send/recv. Here are some example c code to demonstrate how to check and manipulate the O_NONBLOCK flag on a streaming socket.

/* !!! Error checking is omited for brevity !!! */
/* Create a streaming socket */
int sock = socket(AF_INET, SOCK_STREAM, 0);

/* Get the file descriptor flag */
int flags = fcntl(sock, F_GETFL, 0);

/* Set O_NONBLOCK if it is set
 * or unset it if it is not
 */
if ((flags & O_NONBLOCK ) == O_NONBLOCK) {
   fcntl(sock, F_SETFL, flags | O_NONBLOCK);  /* Set */
} else {
   fcntl(sock, F_SETFL, flags & (~O_NONBLOCK));  /* Unset */
}

Tuesday, February 22, 2011

How to Check the Version of a Linux Release

The tool uname is usually used to print the OS version on a Unix or Unix-like machine. But it does not always reveal the actual Linux distribution. For example:

myhost:/home/user$ uname -a
myhost:/home/user $ Linux myhost 2.6.32-24-generic-pae #43-Ubuntu SMP Thu Sep 16 15:30:27 UTC 2010 i686 GNU/Linux


If this Linux distribution is LSB compliant, we can use lsb_release to get what we need:

myhost: /home/user$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 10.04.1 LTS
Release:        10.04
Codename:       lucid