Shell Script to Copy file from one server to another - shell

I have 2 solaris servers. I want to write a shell script which will copy a file from one server to the other.
scp /tmp/test/a.war tomcat#202.203.204.44:/tmp/
The above command when executed in PUTTY will ask me to enter a password for the destination. This is fine when using PUTTY.
How can iI enter the password while running the scp command through shell script?
Thanks in advance

You have to setup SSH private/public key.
Once generated place the public key line entry on the target server's and user's ~/.ssh/authorized_keys file.
Make sure the file on the source machine (for the user which will run the scp/ssh command) will have file permission (400) recommended.
https://unix.stackexchange.com/questions/182483/scp-without-password-prompt-using-different-username or
http://docs.oracle.com/cd/E19253-01/816-4557/sshuser-33/index.html or similar online help can help you.

Related

SFTP file from one remote server to other remote server using shell script without expect package

I'm trying to automate the transfer of files from one remote server to other remoter server using shell script. I cannot install expect package. Could anyone help me out in doing this?
Thanks.
Setup a password less login between remote server so it won't ask for password in shell script and it will avoid passing password in shell script for security reason
Please refer the below link to setup password less ssh
http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
Then write a shell script to sftp the files
refer the SFTP command in the below link to do the needful
http://www.tecmint.com/sftp-command-examples/

Shell script to copy one directory from one server to another without asking for password

I want to write a shell script and put it in a cron. This shell script will copy one particular directory from my server to another server everyday once. So, I don't want it to prompt for passwords. Is there something that I can add in my script so that it wont ask for passwords everyday?
You need to have a password less SSH Login in your Unix Boxes
Below link describe how to set password less SSH login
http://www.tecmint.com/ssh-passwordless-login-using-ssh-keygen-in-5-easy-steps/
you can use FTP or NDM to transfer the Files
In this way you can achieve your requirement.
Using the below script, I am able to achieve what I mentioned :
#!/bin/bash
com="sshpass -p Password0 scp arul#172.25.184.93:/home/arul/test.sh ."
eval $com
You can use RSA key option also for this. Using RSA key you can authorized your second server in first server. This is one time operation.
ssh-copy-id -i ~/.ssh/id_rsa.pub [Your 2nd server IP]
Example:-
[root#vasmon home]# ssh-copy-id -i ~/.ssh/id_rsa.pub xxx.xxx.xxx.xxx
root#xxx.xxx.xxx.xxx's password:
Now try logging into the machine, with "ssh 'xxx.xxx.xxx.xxx'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root#vasmon home]#

Shell Script program to download files from linux remote server

I am very new in shell scripting , i want to download some files from linux remote server ,so how can i proceed for that.That remote server is ssh based .
first of all, ftp service is better choice to get files from remote server.
If only sshd service is available, then you may use ssh based command sftp or scp.
However, using sftp or scp commands will invoke an interactive password prompt, which is a problem in shell script --> You have to ask for help to expect command. see Automate scp file transfer using a shell script .
Besides expect, you may also set up trust relationship between two servers, then you may use scp without password. See http://www.linuxproblem.org/art_9.html

Pause for password sftp bash script file

I am trying to write a script to automatically upload files to a sftp server. My problem is authentication.
I know it is not possible to store a password in a bash script for sftp.
I can't use keys because the admin of the server won't allow me.
I don't want to use any extras (sshpass/expect) because I can't
guarantee they will be on the machine I'm using (the script are wanted so that the processes are not tied down to a particular machine).
Manual entry of the password is not a problem I just need to get the script to wait for the user to put the password in. At the minute when I run the script it opens terminal, prompts for the password, but when this is entered nothing else happens. If I enter the lines of code manual after it uploads everything correctly.
#!bin/bash/
cd /remote_directory
lcd /local_directory
put some_file.txt
After months of looking for an answer I have finally found the solution. It was in a comment on an answer in some other thread I can't even remember. Hope this can help others out there.
Your bash script should look like this and will connect to the sftp server, prompt the user for the password, and then execute the remaining commands.
#!/bin/bash
sftp user#server <<!
cd /the/remote/directory
lcd /your/local/directory
put/get some.file
!

bash script to sftp files with a password from remote directories to local folders

How to write a bash script using sftp command to download files?
I can use the following command to login to the remote machine, however I need to type in the password manually.
bash-3.2$ sftp -o "Port 22022" mike#s-edm-ssh.local.files.stack.com
mike#s-edm-ssh.local.files.stack.com's password:
Connected to s-edm-ssh.local.files.stack.com.
sftp>
How to do the sftp without the password prompt?
If I like to download aaa.txt file at /remote/mike/files to my local directory /local/mike/downloaded, how to build a script to do all of these work?
Since sftp runs over SSH, you can place your public key on the remote server.
If for some reason you can't place your key on the server, then you can write an Expect script to send your password when the prompt appears. See this example.

Resources