Find the answer to your Linux question:
Results 1 to 8 of 8
Hi all, 1)We need to do a clean up a folder in a remote unix server . What is the best option to connect to remote server? how do we ...
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    16

    Connect to remote unix server using Linux script

    Hi all,

    1)We need to do a clean up a folder in a remote unix server . What is the best option to connect to remote server? how do we use ssh ? Is there any prerequisite to use ssh?

    2) We have to SCP few files to another folder in a remote unix server .

    I formed the below command to use in script. Is it of correct usage?

    scp -r $POS_HOME/posctl [login nameip address:/home/username/directory]

    I have seen public key/private key generation part for SCP. How do we handle this? Does this need to be done as unix admin?


    Thanks
    Maya

  2. #2
    Trusted Penguin Roxoff's Avatar
    Join Date
    Aug 2005
    Location
    Nottingham, England
    Posts
    3,392
    scp uses the same keys that ssh uses. Set the keys up for the user the script will run as, you'll want to do this for both the ssh login and the scp file copy.
    Linux user #126863 - see http://linuxcounter.net/

  3. #3
    RDU
    RDU is offline
    Just Joined!
    Join Date
    Aug 2010
    Posts
    89
    If you want to cleanup, the scp to the same directory, you could look at rsync. It will work over ssh (with the same keys as before). You just need the rsync package installed (quite standard in many distro) and sshd running.

    something like :

    $ rsync -av --delete localdir user@server:/destdir

    --delete : remove file in destdir wich are no more in localdir (do your cleanup)
    -av : a = a set of option like recursive, preserve perm, .... (see man)

    An best, rsync transfer only the difference (scp override all files even if they are the same), so you can even sync over internet huge directory (after a first sync).

  4. #4
    Just Joined!
    Join Date
    Jul 2010
    Posts
    16
    Thank you Roxoff and RDU!

    Here is my script for SCP

    Code:
    scp -r $POSCNTL [login name@ip address:/home/username/directory]
    SCP_RETURN=$?
    if [[ $SCP_RETURN -eq 0 ]]
    then
    
            echo "Control files copied to landing zone \n | tee -a $logfile
            echo "SCP Return =${SCP_RETURN}
    
    else
            echo "Control files copy error to landing zone .. Exiting\n | tee -a $logfile
            echo "SCP Return =${SCP_RETURN}
            exit $error_exit
    
    fi
    The below one is for SSH

    Code:
    ssh loginname@ip address
    cd remotehostfolder
    rm *

    Are these two scripts correct ?

    I still have few doubts on key generation part. For the above code to work without asking for a password what all steps I need to take care? (Sorry if it looks so basic..)

    Thanks
    Maya

  5. #5
    RDU
    RDU is offline
    Just Joined!
    Join Date
    Aug 2010
    Posts
    89
    second script is wrong. You've to pass the cmd as argument of ssh

    e.g.

    ssh loginname@remotehost rm -f /path/remotefolder/*

    -f (force) or -rf if you want to remove all under that path recursively (be carefull).

    DON'T cd then rm. If cd fail, you'll rm on a wrong directory. Do directly "rm pathname/*"

    e.g. in your wrong script it will ssh, then when you exit, it will try to change to remotehostfolder wich probably don't exist on your computer, so fail, then rm * all your current directory (probably your script itself and other).

  6. #6
    Just Joined!
    Join Date
    Jul 2010
    Posts
    16
    Thank you so much RDU!

    Can you pls answer this also?

    I still have few doubts on key generation part. For the this code to work without asking for a password what all steps I need to take care?

    Maya

  7. #7
    RDU
    RDU is offline
    Just Joined!
    Join Date
    Aug 2010
    Posts
    89
    e.g. : user1 runing the script on computer client. This script ssh to computer server using user2 :

    1.You have to have a set of key on client side (the one who run the script) using the user that will run the script (user1)
    [user1@client] $ ssh-keygen -t dsa
    It will generate two files in ~/.ssh -> id_dsa and id_dsa.pub (keep the first confidential, it's your private key). The second could be public.

    2.You have to add the public key to the server side into the destination user home directory / .ssh / authorized_keys
    You can copy / paste or use :
    [user1@client] $ ssh-copy-id user2@server

    3.Check the right on this file on the server (Important, if right are wrong, ssh will not accept the cnx).
    [user2@server] $ chmod 600 ~/.ssh/authorized_keys

    4.Now you can connect
    [user1@client] $ ssh user2@server uname -a
    Linux server .....

  8. #8
    Just Joined!
    Join Date
    Jul 2010
    Posts
    16
    Very Clear!! Thank you very much!!

    Maya

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...