ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I want to create a script to insert the fstab and hostname in a textfile of multiple servers on the network without a password. The servers are situated in a text file. So i want to read the text file line by line and write the output into another textfile.
Is this possible?
thnx for your help!
To restate your question by generalizing the problem, you want to perform edits on files located on multiple hosts, each file edited in the same way, and accessed via SSH.
To accomplish this, it is necessary to understand the distinction between the execution of the script on the local host, and the execution of any script on the remote host. The execution of whatever editing you want to occur must take place on the same host as the file being edited. You cannot start a script on host 'A', which tries to directly edit a file on host 'B' (unless the file on host 'B' is exported by some network file share like NFS or SMB/CIFS).
To overcome this there are a couple of options. One has already been mentioned: share the respective files via a network file share, so that the file is available as part of the local filesystem. This allows the local host to perform the editing, while the file exists on a remote host. An alternative scenario that permits the local host to execute the edits would by to transfer the file form the remote host to the local host (scp, sftp, etc.), perform the edits locally, and then transfer the file back to the remote host.
A third scenario could be that the ssh connection invokes a script on the remote host which performs the edit. This will require overcoming two obstacles, in your scenario. Firstly, the script to invoke on the remote host must actually exist there, so you would have to create it and transfer it to each remote host in some known location where it can be invoked through the SSH connection. The second obstacle is that the edit must use data that originates as a file on the local host. This means another file transfer from the local host to the remote host, and probably some kind of cleanup to remove the file once the edit has been performed.
The mechanics of performing the actual edit will depend on what resources are available as well as your own capabilities and knowledge, but a simple substitution edit can be scripted in sed, perl, or awk, or other scripting language. You have not provided enough detail to create a suggestion/example.
The scenario you describe suggests a completely different approach. Since there are multiple hosts apparently wanting to access what seems to be an identical resource (the file you want to update), why not put that file on a single host, and share it across the network? That way, only a single edit on a local host is required.
thanks for your explanation. Maybe my question was wrong, but i only want to list all network mountpoints of all the Linux servers and insert them in a textfile. So i thought to cat /etc/fstab >> mounts.txt or something like that. Is there a easier way to do this? This is my script:
#!/bin/bash
NODES="server1 server2"
for n in $NODES
do
ssh $n hostname >> mounts.txt
ssh $n cat /etc/fstab >> mounts.txt
done
For the NODES variable i want to use al text file with the list of servers as the input file....
I've read the instructions and i think this is a lot of work. I don't mind for now that my passwords are hardcoded in a script, because we wil change them afterwards.
Hi,
It might look a bit daunting at first, but really it doesn't take long to set up. There are several good guides around that step you through each stage. Key authorisation is very useful, specially in conjunction with ssh-agent (that holds your passwords for the current session) so your normal password protected keys you only have to enter the password once when you log on and you can connect seamlessly from then on.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.