How to stop an actively running for loop - bash

This might be a silly question. I wanted to copy thousands of files from a remote server to my local machine using scp. I ran the below command directly on the command line of the remote server (after logging in with ssh)
for file in $(ls <SOURCE_DIR>)
do scp $file <LOCAL_ID>#<LOCAL_IP_ADDRESS>:<TARGET_DIR>
done
But after running this I realized that <TARGET_DIR> is owned by root, so the for loop requests a password, and if I enter the password it throws a Permission denied error message. This is repeating over and over again every loop cycle. Is there any way to get out of the for loop without pressing Ctrl + C thousands (the number of files in <SOURCE_DIR>) of times? Both the server and local use Ubuntu 18.04.

Bash itself will not let you access remote host , but you can 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 and paste it in .ssh/authorized_keys
Step 3: Use scp to copy files:
$ scp -ir .ssh/sshfile username#host:/sourcedirectory targetdirectory
The above command will copy all the files and folders in the respective source directory in the server

Related

Copy file to multiple hosts from a shared file server with password

I have about 20 Macs on my network that always need fonts installed.
I have a folder location where I ask them to put the fonts they need synced to every machine (as to save time i will install the font on every machine so that if they move machines, i don't need to do it again)
at the moment I am just manually rsyncing the fonts from this server location to all the machines one by one using
rsync -avrP /server/fonts/ /Library/Fonts/
this requires me to ssh into every machine
is there a way i can script this using a hosts.txt file with the ips? the password is the same for every machine and i'd rather not type it 20 times. Security isn't an issue.
something that allows me to call the script and point it at a font i.e.
./install-font font.ttf
I've looked into scp but I don't see any example of specifying a password anywhere in the script.
cscp.sh
#!/bin/bash
while read host; do
scp $1 ${host}:
done
project-prod-web1
project-prod-web2
project-prod-web3
Usage
Copy file to multiple hosts:
cscp.sh file < hosts
But this asks me to type a password every time and doesn't specify the target location on the host.
I don't see any example of specifying a password anywhere in the script.
Use ssh-copy-id command to install your public key to each of these hosts. After that ssh and scp will use public-private key authentication without requiring you to enter the password.

FTP using Unix shell script and then triggering a task

I have a requirement at hand in Unix where I need to build a shell script.
The requirement is below:
I need to SFTP a file (let's say CSV file) from my dev server to uat server.
After the SFTP is done to that server, as soon as the file comes there and the exit code of the previous SFTP is 0, I need to trigger a task (this task I can take care of).
I have the basic idea on SFTP but I am not aware of how to trigger the next task as soon as the file comes to the uat server.
Please need a pseudo code to start my exploration.
If you want to copy from somewhere to your local machine and run a command locally
If you have access to ssh then it can be done easily which I am doing it usually.
For example I have a backup file from one of my server. We can get a copy this way using scp
scp root#server:/home/weekly.sql.zip .
. means put the file with its name here on this directory I am in now
the problem with this command is that it has an interaction for getting password so to eliminate this we can install sshpass and use it this way:
sshpass -p'your-password' scp root#server:/home/weekly.sql.zip .
Since we are using bash and it take care of exiting code if you add and && operator then you can add a second command so to be triggered after first command has successfully done.
sshpass -p'your-password' scp root#server:/home/weekly.sql.zip . && unzip weekly.sql.zip
First task is copying a file and second is to unzip it.
installing sshpass:
sudo apt install -y sshpass
If you want to copy from your local machine to somewhere and run a command remotely
sshpass -p'your-password' scp test.txt root#address:/home/ && sshpass -p'your-password' ssh root#address cat /home/test.txt
Which does this:
copy file test.txt to the server
then read it by cat command

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 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/

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