Shell script to automate scp command - shell

I have to move file from one server to the other server for which i am using
scp user#sourceservername:sourcefilepath user#destservername:destdirectory
when i execute this script it asks for password of both the source and destination. Can this scp command be included in a shell script such that the password is picked dynamically ? alongwith file name and file path ?

You can solve it by generating private/public keys for both servers.
Use ssh-keygen and ssh-copy-id later to copy your public key to both servers.
Steps:
ssh-keygen
ssh-copy-id user#sourceservername
ssh-copy-id user#destservername
scp -3 user#sourceservername:sourcefilepath user#destservername:destdirectory
Notice the -3 option, this will transfer the file from sourceservername to your local server and then to destservername. This way you will have only single pair of private/public keys.

Related

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 to Copy file from one server to another

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.

how to pass password to sftp connection in shell script

I have an sftp connection to a server in Unix.
Without password, I use the syntax to connect and execute command
sftp -b $user#$server_name
Can anyone suggest me how can I write a shell script to connect a remote server non interactively using a password
Try with this below option,
lftp -u $user,$pass sftp://$host << --EOF--
cd $directory
put $srcfile
quit
--EOF--
You could use ~/.ssh/config file.
#
# ~/.ssh/config
#
Host servername
Hostname 127.127.127.127
Port 22
User root
#EOF: config
Then simply connect with "ssh servername" and if you don't want to use password you can use SSH key. Here is good tutorial on how to do that > http://www.cyberciti.biz/tips/linux-multiple-ssh-key-based-authentication.html
If you just want to pass user/server from terminal, you can do this.
#!/bin/bash
sftp -b "$1"#"$2"
then use it like this './sftp.sh user server'
use SCP like this;
scp -P 22 user#server:/dir/file.tgz ~/Desktop/
use SFTP like this;
sftp user#server:/dir/file.tgz ~/Desktop/file.tgz
You can also try this;
sftp user#host <<EOF
get /dir/file.tgz
rm /dir/file.tgz
EOF
The best way to do this would be to create a key pair on the client, and add the key to the target user's ~/.ssh/authorized_keys.
To create a key pair, run ssh-keygen and when it asks for a password, just hit return to indicate "no password". Then either run ssh-copy-id $user#$server_name or manually create a ~/.ssh/authorized_keys on the server and copy the contents of the ~/.ssh/id_rsa.pub from the client into it (ssh-copy-id isn't available on all machines, so on some you'll have to do it manually).
Now you should be able to run ssh or scp without a password, as it should use your key instead. If it doesn't work, make sure that the permissions on your ~/.ssh/ directory and contents are correct on both machines; the directory should be 0700 (drwx------) and the files should be 600 (-rw-------). Also check that key authentication is enabled on both the client and the server.

How to copy files to remote computer using Bash script?

I have a script that needs to copy a file to a remote computer:
cp -R "${DEST_FOLDER}" "${SRC_FOLDER}"
How can I do it when the remote computer requires user and password for access?
How do I login to this computer with a bash script?
Thanks
Bash itself will not let you access remote host (obviously), but you could use SSH:
Step 1: On your local PC generate key to perform password-free authentication later
$ ssh-keygen
It will ask you to enter passphrase. If you want your bash script to be fully non-interactive, you can opt not to use any password.
Step 2: Copy you public key to the remote host:
$ ssh-copy-id -i ~/.ssh/id_rsa.pub user#remote-host
Step 3: Use scp to copy files:
$ scp -r local_file user#remote-host:/remote_dest_dir/

Shell script which copy the file from another server to my box using SFTP without prompting for a password

I want to write a shell script which copy the file from another server to my box using SFTP without prompting for a password as I have already made private keygen for password on server. Can you provide me the example script to do it.
You can use scp which works on ssh protocol and read private keys as well:
scp -r path/to/src/folder1 username#server:/path/to/dst/folder
The -r switch is to copy recursively
If you need to specify port, use -p
scp -r -P 2222 path/to/src/folder1 username#server:/path/to/dst/folder

Resources