Monday, June 10, 2013

Shell script to FTP to a server by passing authentication through the script

Ever struggled on writing shell script for uploading or downloading files from FTP server. Here you go with a simple and tidy shell script.


#!/bin/sh
HOST='ftp.linuxperlscripting.blogspot.in'           #FTP server to be connected
FTPUSER='Username'           #Username for the FTP account
FTPPASSWD='Password'           #Password for the FTP account
UPLOADFILE='Uploadfile.txt'           #File to be uploaded
DOWNLOADFILE='Downloadfile.txt'           #File to be downloaded
ftp -n $HOST <<END_SCRIPT           #Connect to FTP server -n option Restrains ftp from attempting auto-login upon initial connection.
quote USER $FTPUSER           #Passing ftp Username
quote PASS $FTPPASSWD            #Passing FTP Password
put $UPLOADFILE            #put is a FTP command to upload the file
get $DOWNLOADFILE           #put is a FTP command to download the file
quit #Quits the FTP session
END_SCRIPT
exit 0




Another method you can use is making use of .netrc which would be explained later

Wednesday, December 7, 2011

How websites can be redirected?


Introduction


We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways can do the website redirection.

Redirect using "Meta Refresh Tag" method

Create a simple html file with the below code. This will redirect the URL to http://newwebpage.com

HTML Syntax: <META http-equiv=”refresh” content=”<time>[;URL=<new url>"]>


<head>
...head section stuff (Title, Description,etc.)...
<meta http-equiv="refresh" content="0;url=http://newwebpage.com">
</head>

Meta-tag method is on of easiest way to achieve the redirect the web pages. But it has some disadvantages. Crawlers used in search engines will not keep the old rank in the new web page. Disadvantage in "Meta Refresh Tag" method can be solved using 301 redirect.

What is 301 redirect.

301 redirect, interpreted as “moved permanently”, is a method used to redirect webpages while preserving your search engine rankings. There are different ways to achieve this. 

Please note my examples are based on Apache web server.

Redirect with .htaccess and Mod_Rewrite

The easiest way to achieve redirection is using .htaccess, since it doesn’t require access to server. We can make use of FTP access and upload the .htaccess file.

Scenario 1.

Simple 301 redirect accessing a page to a newwebpage.

RewriteEngine On
Redirect 301 /mypage.htm http://newwebpage.com
  
Save the file and upload the .htaccess file to the root of the domain that hosts the old page. This will do a permanent redirect the page to the newwebpage.com and get the advantage of old web site ranking as well.
  
Scenario 2

www and non-www are not same for search engines

There are two ways to view most sites, that’s the www and non-www version www.domain.com and it basically became the standard way to access a site via a browser. However it’s not the only way, some sites can be browsed using http://domain.com which in the non-www version.

This has led to problems with search engines like Google indexing both versions resulting in duplicate content and sharing link benefit problems. Some webmasters link to the www version and others the non-www, search engine spiders follow the links and spider the site twice.

.htaccess file to redirect non-www site to www

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST}   !^www\.domain\.com [NC]
RewriteRule ^/(.*)         http://www.domain.com:%{SERVER_PORT}/$1 [L,R]

Scenario 3

Redirect Homepage of a site according to the ``User-Agent:'' header of the request

You might have noticed some websites will load different pages when browsed from different browsers. The simple logic is to have multiple websites and have the below in .htaccess file.

RewriteCond  %{HTTP_USER_AGENT}  ^Mozilla
RewriteRule  ^/$                 /homepage.max.html  [L]

RewriteCond  %{HTTP_USER_AGENT}  ^Lynx
RewriteRule  ^/$                 /homepage.min.html  [L]

RewriteRule  ^/$                 /homepage.std.html  [L]


Scenario 4

Protecting your images and files from linking

If you do not want other webmasters linking to your website files and images as inline-images on their pages use the below in .htaccess file. 

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$ [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com [NC]
RewriteCond %{HTTP_REFERER} !^http://201.192.1.168 [NC]
RewriteRule ^.*$ http://www.domain.com/ [R,L]




Wednesday, November 16, 2011

How to have password prompts in perl script

The below code will help your scripts to have password prompts without echoing it to console. This will help you in hiding the password while running the scripts.



Subroutine

sub getadmininfo() {
        print "Enter username: ";
        chomp (my $adminusername = <STDIN>);
        print "Enter password: ";
        system('stty','-echo'); #Hide console input for what we type
        chomp(my $adminpassword=<STDIN>);
        system('stty','echo'); #Unhide console input for what we type
        print "\n";
        return (\$adminusername,\$adminpassword);
}



Call subroutine using

 my ($adminusername,$adminpassword)= &getadmininfo();


Tuesday, November 15, 2011

Proxy server - Different modes


What’s a web proxy server?
A proxy server is a server that goes between clients and web servers, used in corporate to enforce corporate browsing policy and ensure security. Proxy servers are commonly used in three modes.

      A)    Forward proxy

Forward proxy is the most commonly used type of proxy servers. Clients from intranet request access the web server in internet through a forward proxy server. Proxy can be used to filter clients from accessing illegitimate and malicious web contents.

Advantages of forward proxies are

     1)     Enhanced privacy and security
     2)     Increased performance because of caching
     3)     Reduce internet bandwidth usage
     4)     Enforce corporate internet policies using ACL’s
     5)     Anti-malware and Anti-virus infections




     B)    Reverse proxy

Reverse proxy is commonly used in web farms. The response to the client is returned as if it came directly from the proxy server. Advantages of reverse proxy in server farm are as below.

     1)     Load balancing of web servers
     2)     Caching
     3)     Compression
     4)     Firewall
     5)     SSL offloading







     C)    Transparent proxy

A transparent proxy server is a proxy server configured in such a way that no client side (browser side) configuration is required. The www requests are intercepted at network level by making used of WCCP protocol or route map in network switches and forward the browsing request to proxy server.  Transparent proxies are mostly used in ISP’s to reduce the bandwidth usage by making use of content caching feature in proxy.

Script for Resetting Root Password in Multiple Linux servers

OS: CentOS, Redhat
Scripting language: Perl
Perl module required: Net::SSH::Expect


What you need to do? 

In server where the perl script will be executed, create a file "serverlist.txt" and add the server ip address line by line.

Eg:
192.168.0.1
192.168.0.2

Script is as below


#!/usr/bin/perl
use Net::SSH::Expect;

$oldpass = $ARGV[0];
$newpass = $ARGV[1];
chomp $newpass;

sub connect($$)
        {
        $password=shift;
        $server=shift;
        chomp $server;
        chomp $password;
        $ssh = Net::SSH::Expect->new (
                host => $server,
                password=> $password,
                user => 'root',
                raw_pty => 1,
                timeout => 30
        );
        &login($ssh)
}


sub login($)
        {
        eval {  $login_output = $ssh->login(); };  if ($@) {print "$server not found\n"; next; };
                if ($login_output !~ /Last/) {
                        print "Login password error for server $server , please enter the password: ";
                        $inputpass = <STDIN>;
                        &connect($inputpass,$ip)
                }
                else {
        #               &resetpass();
        $ssh->send("passwd");
        $ssh->waitfor(':\s*\z', 10) or warn "$server - Error 'New password:' prompt not found\n";
        $ssh->send("$newpass");
        $ssh->waitfor(':\s*\z', 10) or warn "$server - Error 'Confirm new password:' prompt not found\n";
        $ssh->send("$newpass");
        $ssh->waitfor('#\s*\z', 10) or warn "$server - Error 'Prompt not found after reset:'\n";
        print "$server new password is \t $newpass\n";
        $ssh->close();
                }
}

open (SERV,"serverlist.txt") || die "ERROR: Unable to open Serverlist file $! \n";
while ( $ip = <SERV> ) {
sleep 2;
&connect($oldpass,$ip);
}

Running the script

./passwordreset.pl <Oldpassword> <Newpassword> 

The script will automatically login to the listed servers and reset the root password