Reputation: 767
My ask is to copy a .py file from my client machine to a remote server and then running the .py script into the remote server.
Note: I need to perform the same for n number of servers so basically I am here looking to create a kind of automation script which will do the same.
From my colleagues I came to know that in order to achieve automation, password less login is a must (kind of a prerequisite).
In order to achieve the same, I first copied a public key into my remote server (manually), then created a script which does scp to copy the shell script from client to remote. And the shell script is responsible for running the .py file.
Adding the public key and scp worked for me.
Kindly do let me know if adding the public key is actually needed for achieving the automation for this task .
I am a beginner in LINUX world, so if possible please elaborate the answer while replying.
My script:
#!/bin/bash
# Linux/UNIX box with ssh key based login
Script=/home/linuxadmin/installer_linux.py
Destination=/home/linuxadmin/script
time=`date`
output=/home/linuxadmin/output_mainscript/
[email protected]
PASSWORD=12345
#Loop For Installing software on multimple servers
for f in `cat host.txt`;
do
scp $Script $f:$Destination ### Copying file from source to destination
echo "-------- start time "$time"echo "----------"" | tee -a $output/mainscript_log.txt
sshpass ssh $f "sudo /usr/bin/python $Destination/installer_linux.py --no-prompt -u $USERNAME -p $PASSWORD " | tee -a $output/mainscript_log.txt ### Executing files on Destination
echo "-------- End time "$time"echo "----------"" | tee -a $output/mainscript_log.txt
done
Upvotes: 1
Views: 130
Reputation: 19282
Kindly do let me know if adding the public key is actually needed for achieving the automation for this task .
Yes, you need to copy the public key of the main/controller node (the one you intend to execute scp from) to all the other nodes/servers.
Suggestion 1: I am assuming, you want some solution for not a just one time task, but a long term management (correct me, if I get it wrong).
Use a configuration management tool - as per your case a single management node, and merely deploying .py scripts,
I'd recommend using Ansible. With Anisble you can start with a controller node, making an inventory (list of hosts/machines), and share the controller's public key on all the nodes/machines.
And all you need to do (once the setup is there, which is pretty simple) - is update the playbook, or even execute a command for all the servers from the controller/main server.
Here's few very plain English blogs to get started with:
Disclaimer: I am the author of above blog posts.
Upvotes: 1