Monday, February 20, 2017

sftp in Linux and Unix with examples

In modern world information is really precious and and transfer of information ( and data containing this information ) is really very important , today i am going to discuss sftp which is basic building block of data transfer and used widely. sftp stands for "secure file transfer protocol "

lets try to understand sftp with examples . I am logged in to one of my server with IP address 192.168.246.137 with root account. I want to make an sftp connection to another server with IP address 192.168.92.129

First requirement of making an sftp connection is to make sure that target server is reachable from source server



in order to make an sftp connection , i have to use "sftp Target-Server-IP-address"
Since i was making this connection first time it asked me a question "Are you sure you want to continue connecting (yes/no)?" to which i replied yes , and as result 192.168.92.129 was permanently added to the list of know hosts

One thing must be noticed here i did not specify any user name for while making sftp connection"

But still i was able to make a successful sftp connection . Reason ? when i did not specify username for sftp it defaulted to current user on current machine ( which was root ) . now obliviously  root account also exists on remote machine and the password i entered was of root account of remote machine and it worked

 if you want to use an specific account ( on remote machine ) , you must specify it explicitly , like below




once you are able to sftp successfully, you get sftp prompt, you can change directory using cd command,
you can collect a file from remote server using "get" command on sftp prompt ( here i used "get sftp-linux-example.txt" ) , if you want to collect multiple files at one go you need to use something mightier called "mget".  similarly sftp has two more useful weapons  in its arsenal  one is "put" to send a file on remote server and its elder sibling "mput" to send multiple files at once ( hint : you can you use wild card pattern with the help of * )

 


 To list files on remote server you can use ls command on sftp prompt, pwd command shows the current working directory on sftp  prompt, you can even know the current working directory of  source host using "lpwd" command on sftp prompt, refer below examples



To See all available command on sftp prompt type "help"



 We all know that True power of  Unix and Linux systems lies in ability to get things automated  using shell scripts.In order to use sftp efficiently inside shell script we have to provide predefined set of instructions to sftp. sftp has something called "batch mode" or -b option , which is quite useful for this purpose

 but it should be used with password less authentication or better we call it non interactive ssh authentication . Now in order to setup this type of authentication to you need to follow certian simple steps ( though cryptography is huge topic in itself, i will be just explaining basis steps for ssh key generation for now )
1. login to host system which you will be using to  make sftp connection, you need to run "ssh-keygen -t rsa"
command  to generate a public-private key pair, once  you run this command it will ask you for passphrase, for a basic authentication less connection , you can provide blank passphrase ( though there is a more secure alternative with non blank passphrase - The SSH agent  , also note that i was already having existing key pair, which i have overwritten in order to show you the process , you may need to choose your options more wisely )




2. Now go to .ssh directory and you can see a private key and public key, copy the public key to your target server




 

 3.  login to target server and append the pubic key to .ssh/authorized_keys file.
you may also need to provide appropriate permissions to .ssh directory and authorized_keys, but remember "Dont use 777"



i have a file MBATCH.txt on my server which will connect to target ( remote server )  which contains some instructions ( one mput and one bye commands )



So i used it with sftp -b, as shown in following example ,



See i was able to transfer the files only by specifying batch file name having instruction

 One more important thing to remember, by default sftp uses TCP port number 22



if you want to specify some other port number you can specify it using "-oPort" option

I hope you all enjoyed this post, thanks for reading,

Visit other posts - 

 

if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com






Saturday, February 11, 2017

grep command with examples part -2

welcome back friends, today i am going to show you some more features of grep command
so lets begin, some times you may to search one of the multiple patterns in a file, thanks to "-e" options
lets have a look :
i easily searched "Canada" and "Japan" in file "Beautiful_places.txt" using -e option
( this one is easy to remember as well "-e" for extended search pattern ☺) 



it is also possible to combine two different options , for example if you want to see the count of occurrence of either of two different patterns ( for instance total count of occurrence of either Canada or Japan in this case )
you can combine "-c" with "-e"


see this is where Unix starts getting powerful, you can combine different commands and also different options.and you may get the amazing results.

grep comes with one more useful option "-f" . this option is particularly useful while using grep command inside a shell script. since you can specify  multiple patterns using  a file

lets see following file parts.txt , this file contains two patterns "Trump" and "Ball"

  

my target file where i am going to search my pattern has been shown below :



lets see following what happens when you try to supply your search patterns using the file "parts.txt"



So it did produce the desired result ,

Wait more magic is coming with grep command  as we are getting more demanding

Now i demand grep to search the lines with pattern "ia", but "ia" should be prefixed by  either "s" or "d" nothing else. In short i am telling grep to search lines containing "sia" or "dia" but not "lia"
so output should contain "Asia" and "India" but not "Australia", but wait being stubborn kid i am also telling grep that it should not use "-e" or "-f" option . 




Well grep is smarter than i  could imagine , it has got many ways to achieve its goal, look below
it just used rectangular brackets [] and ruled like a boss



moral of the story : Never mess with grep !!!

last but not the least ( for today ). you can use "-E" option and specify an "logical OR" condition
check it out -


Thank you, Danke, Je vous remercie, Дякую, धन्यवाद for reading this post, have a nice day

Visit Other posts -


if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com

Thursday, February 9, 2017

grep command with examples - Part - 1

grep is one of most widely used filter in Unix and Linux operating systems, its very simple to use for beginners unlike advanced filters such as sed and awk, though it is less powerful it still comes with lot of inbuilt options, lets explore this good old friend of every Unix student and professional

i have one file "Beautiful_places.txt" . look what does it contain

Australia, country
United States, country
Canada, country
North America, continent
Europe, continent
Japan, country
Asia, continent
India, country
England, country




I have another file "games_and_sports.txt" with following content -
Trump, cards
Ball, cricket
Queen, chess




Now if you want lines only with word "country" from the file Beautiful_places.txt  use following



To see count of matches use "-c" options



 To exclude lines containing a pattern you can use grep with "-v" option



 To see all the lines containing both capital and lower case "a"  ( to make your search case insensitive ) from the file Beautiful_places.txt, use "-i" option with grep command



sometime you may need to  list files which contains certain pattern , "-l" option of grep command proves really helpful in such cases. See below example only one of the file was listed as only this file contained the your search pattern , 





 also see carefully i have used wild card for the list of files being scanned ( *txt ) , so that all the file ending with "txt" will be scanned for this search

 you may also need to see the line numbers of lines containing your search pattern
grep provides a solution for that too , its "-n" option  ( easy to remember as well :  n for number ☺)



grep has lot of other features , its is used frequently inside Unix shell scripts as well,grep regex is also one of the important topic, i will cover some of the advanced features of grep in my next post, you can visit grep man pages as well, i hope you all really enjoyed this short but useful introduction of grep command ,



if you want learn to Basic Unix Commands in 1 Hour, here is the link

Basic Unix Commands in 1 Hour

if you want learn Unix/Linux Commands in detail, here is the link

Learn Unix/Linux Commands in detail

Also keep visiting my blog to learn more

unixtechworld.blogspot.com