Results 1 to 7 of 7

Thread: SSH script

  1. #1

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

  2. #2
    Join Date
    Aug 2008
    Location
    Behind the 8 ball
    Posts
    116

    Default Re: SSH script

    Try something like this

    Code:
    #!/bin/bash
    
    for computers in $(cat computer_list)
    do
       ssh ${computers} "echo \"test\" > testFile.txt"
    done
    Good luck,
    Hiatt

  3. #3

    Default Re: SSH script

    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.

  4. #4

    Default Re: SSH script

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

  5. #5
    Join Date
    Aug 2008
    Location
    Behind the 8 ball
    Posts
    116

    Default Re: SSH script

    Quote Originally Posted by blank888 View Post
    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.
    Put you command in quotes like I did in post #2.

    Code:
    ssh $computer "ls -l ~/.ssh"
    If you don't like this method you may want to look at expect

    Code:
    man expect
    Good luck,
    Hiatt

  6. #6

    Default Re: SSH script

    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

  7. #7
    Join Date
    Nov 2010
    Location
    Youngstown, OH
    Posts
    155

    Default Re: SSH script

    from c-line:
    Code:
     for computers in $(cat computer_list); do ssh user@$computers  <remote_command> ; done
    example:
    Code:
     for computers in `cat computer_list` ; do ssh -i user@$computers  echo test > testFile.txt ; done
    or
    Code:
     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 \> 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.
    Lenovo: OpenSUSE 11.4/32 | Xfce 4.8.1 | Intel Core2 Duo T7200 | Intel 945GM/GMS | 4G RAM | Firefox 8.0 | Thunderbird 3.1.16

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •