SSH script

I’m trying to make changes to several computers at once in a private network. I have ssh keys setup so that I can ssh from the main computer to all the other computers without having to enter my password. So I wrote a bash script like
for computers in cat computer list
do
ssh $computers
#type changes here
exit
done

but instead of executing the commands on every computer, it opens a shell on every computer for me to type into, just as if I had used ssh myself and not in a script. Can anyone tell me how to get this to work? I want to be able to add lines at the comment like “echo “test” > /testFile.txt” and be able to see the changes on each machine, in this case see testFile.txt on every machine.

Try something like this


#!/bin/bash

for computers in $(cat computer_list)
do
   ssh ${computers} "echo \"test\" > testFile.txt"
done

Good luck,
Hiatt

I already tried that and it didn’t work. I tried it in a shell, and not a script, but I don’t think that should make a difference.
Although, when testing my ssh keys I did run ssh $computer ls -l ~/.ssh to make sure the key was working and that the permissions were right, but I did it individually and not in a script. So I’m not sure why I could list a folder but not make other changes.
Also, I’m doing this as root, so there shouldn’t be any permission problems.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You cannot do that on separate lines like that… this is going to behave
EXACTLY as if you were running the commands yourself. Put the commands
either in a script to be sent to the remote boxes and then called via ssh
(you can do scp and ssh calls using the keys you already have setup) or
make sure you’re doing something like this:

ssh $computers ‘your commands; go here’

Good luck.

On 05/17/2011 01:06 PM, blank888 wrote:
>
> I’m trying to make changes to several computers at once in a private
> network. I have ssh keys setup so that I can ssh from the main computer
> to all the other computers without having to enter my password. So I
> wrote a bash script like
> for computers in cat computer list
> do
> ssh $computers
> #type changes here
> exit
> done
>
> but instead of executing the commands on every computer, it opens a
> shell on every computer for me to type into, just as if I had used ssh
> myself and not in a script. Can anyone tell me how to get this to work?
> I want to be able to add lines at the comment like “echo “test” >
> /testFile.txt” and be able to see the changes on each machine, in this
> case see testFile.txt on every machine.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.15 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQIcBAEBAgAGBQJN0s0sAAoJEF+XTK08PnB5xWQQANUtKn3Jrvy/5OFXF5SX58MV
VWclRdZddlzMHIuO+MNqGtNKgScAVMFAk9mhq4qcvEaI04y+QxQ651Y86JXdkyvd
5WBSyTOnf+jb4yvyvf5btHZfTe1m8+wPE+16Qci71NmHYZoLSbWgVURbbBF67tMw
TVGubyZysqXCyXAOztID6KbfI/jDZeg1a0dSiUghZJ278ZTRmbU34FR++fv3TnsT
3aJI1aXkSmwqYFLx5Zj4j0dch4qm9eUzbNcdb3wN0r+x3zqd3+0U+B8MBxj2cMgJ
7ttk2BXuiR/2LrlpUPX3ikZMV+v7iro1gFdFMnAgrZlt5pCuQgzbOyZVHiYXimSy
7s4wvN9BjzeM8lT6+fFyviTf9DfM7TTMWqmfmbOyYPjS71uflxDPfXX/T3cYlcOU
83sUEtqPIVG6ps8lXapPfqsAJQ7FCRFSBHKTo5DkBOYcEUwPpGVJPwHLiismvZWH
7FVW5kywuqZqMQnREeozm/DiYAJIxuvPLg32o5zL335VbnPCcuxeqNL76PDK4bKW
eHDqq2liApH1sqBhECj1gOCHe0jF04618OBbOwdbL0NjMrZOolMx4JUiKK/0ljHM
B/jMUi+BqQ0UYuoCcXTwQaeHI3gC/t0KZ1F5AWl82HNaiJgLdrtTbbyjtydL4GZX
GXWIkWNGpAj9mkR4Zg1h
=+hK2
-----END PGP SIGNATURE-----

Put you command in quotes like I did in post #2.


ssh $computer "ls -l ~/.ssh"

If you don’t like this method you may want to look at expect


man expect

Good luck,
Hiatt

On 05/17/2011 11:06 AM, blank888 wrote:
>
> I’m trying to make changes to several computers at once in a private
> network. I have ssh keys setup so that I can ssh from the main computer
> to all the other computers without having to enter my password. So I
> wrote a bash script like
> for computers in cat computer list
> do
> ssh $computers
> #type changes here
> exit
> done
>
> but instead of executing the commands on every computer, it opens a
> shell on every computer for me to type into, just as if I had used ssh
> myself and not in a script. Can anyone tell me how to get this to work?
> I want to be able to add lines at the comment like “echo “test”>
> /testFile.txt” and be able to see the changes on each machine, in this
> case see testFile.txt on every machine.

This article should give you some options:
http://www.linux.com/archive/feature/151340


Kevin Miller - http://www.alaska.net/~atftb
Juneau, Alaska
In a recent survey, 7 out of 10 hard drives preferred Linux
Registered Linux User No: 307357, http://counter.li.org

from c-line:

 for computers in $(cat computer_list); do ssh user@$computers  <remote_command> ; done

example:

 for computers in `cat computer_list` ; do ssh -i user@$computers  echo test > testFile.txt ; done

or

 for computers in $(cat computer_list) ; do ssh -i user@$computers echo test > testFile.txt ; done

you may have to escape the > operation, using a &gt; instead of.
Redirects get nasty on remote shells.

BTW: you can’t write /testfile if you don’t login as root or don’t have permission to do so.