LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2010, 07:11 AM   #1
mafkees80
LQ Newbie
 
Registered: Mar 2010
Posts: 22

Rep: Reputation: 0
ssh comand on multiple hosts


Hello,

Is there a way to do the following:

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!

greetings,
Mafkees80
 
Old 04-13-2010, 09:52 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.

Last edited by theNbomr; 04-13-2010 at 09:54 AM.
 
Old 04-13-2010, 11:21 PM   #3
mafkees80
LQ Newbie
 
Registered: Mar 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Hi rod,

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

Thanks

Greets
 
Old 04-14-2010, 12:34 AM   #4
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Quote:
Originally Posted by mafkees80 View Post
#!/bin/bash
NODES="server1 server2"
for n in $NODES
do
ssh $n hostname >> mounts.txt
ssh $n cat /etc/fstab >> mounts.txt
done

That seems to work fine for me. If you wanted to you could put both commands into one statement:

ssh $n "hostame; cat /etc/fstab" >> mounts.txt

If you want to read input from a file:


Code:
#!/bin/bash
while read n;
do
  	ssh $n "hostname ; cat /etc/fstab" >> mounts.txt
done < input.file
 
Old 04-14-2010, 01:59 AM   #5
mafkees80
LQ Newbie
 
Registered: Mar 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Hello,

bakdong thanks for your input, but how can i pass the password in the script for each server? I've read it can be done bij expect commands?
 
Old 04-14-2010, 02:46 AM   #6
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
Noooooooooooooooo



Use a key pair. Keep the private key private and put the public key on each server you're accessing.

http://sial.org/howto/openssh/publickey-auth/
 
Old 04-14-2010, 11:03 PM   #7
mafkees80
LQ Newbie
 
Registered: Mar 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Goodmorning bakdong,

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.

thanks for your help!

Greets
Mafkees
 
Old 04-14-2010, 11:25 PM   #8
bakdong
Member
 
Registered: Apr 2009
Posts: 214

Rep: Reputation: 44
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.

Have a look at:

http://e-articles.info/e/a/title/Usi...ile-Transfers/

It's three steps: 2, 3 and 4, that's generate a key pair, copy the public key to the remote server, and test.

Really shouldn't take more than 15 mins to do.

(Also should have said, you only need to generate one pair of keys - you can use the same set over multiple remote servers)

Last edited by bakdong; 04-14-2010 at 11:26 PM.
 
Old 04-15-2010, 12:50 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Btw, there's no need to send people elsewhere ;}

Public key authentication with SSH is part of our tutorial section;
funnily enough neither our not your walk through mention
Code:
ssh-copy-id -i ~/.ssh/id_dsa <target host>

Cheers,
Tink
 
Old 04-15-2010, 01:19 AM   #10
mafkees80
LQ Newbie
 
Registered: Mar 2010
Posts: 22

Original Poster
Rep: Reputation: 0
This is my script, is this oke?


#!/bin/bash
stty -echo;
read -p "Input password:" A;
stty echo;
echo;
while read n;
do
echo "Connecting to $n"
expect -c "set timeout -1;\
spawn ssh $n -l root \"hostname ; cat /etc/fstab" >> mounts.txt;\";\
match_max 100000;\
expect *mypassword:*;\
send -- $A\r;\
interact;"
echo "Finished job on $n"
done < input.file
 
Old 04-15-2010, 01:21 AM   #11
mafkees80
LQ Newbie
 
Registered: Mar 2010
Posts: 22

Original Poster
Rep: Reputation: 0
Ow i didn't saw the reply's, it seems to be easier as i thought. I gonna take a look at the tutorial section. Thnx
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ssh to multiple hosts behind remote nat keex Linux - Networking 3 01-05-2006 10:34 AM
using dd comand to write to multiple destinations dln2k5 Linux - Newbie 1 10-28-2004 05:17 PM
PGP across multiple hosts mr666white Linux - Security 4 08-22-2004 10:14 AM
remote ssh commands on multiple hosts evilchild Linux - Software 6 08-12-2004 10:48 PM
multiple virtual hosts iquadri1 Linux - Networking 1 09-25-2001 10:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration