Find the answer to your Linux question:
Results 1 to 7 of 7
Anyone have a script that would allow a persistent ssh connection to a server, then reading in a line from a local file, sending it with a return to the ...
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    45

    ssh input/output pipe

    Anyone have a script that would allow a persistent ssh connection to a server, then reading in a line from a local file, sending it with a return to the remote, reading the next line, sending it, etc, until the file is done.

    Thanks so much if you can help.

    Mike

  2. #2
    Just Joined!
    Join Date
    Mar 2008
    Posts
    26
    Quote Originally Posted by mprog View Post
    Anyone have a script that would allow a persistent ssh connection to a server, then reading in a line from a local file, sending it with a return to the remote, reading the next line, sending it, etc, until the file is done.

    Thanks so much if you can help.

    Mike
    So you mean like ssh into a box and then read (for example)
    "cd /home/"
    "grep hello" etc etc from a file? Hmm. TBH, it seems like the kind of thing that expect does. I've had trouble getting SSH to do things automatically, as really, it's not designed for that (it's designed for security more than anything). But then maybe I was doing something different.

    I would probably try using awk to make a shellscript from the files contents. If no-one has any better ideas, I'll pop back and give you some tips if you're interested.

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    45
    >So you mean like ssh into a box and then read (for example)
    >"cd /home/"
    >"grep hello" etc etc from a file?

    >I would probably try using awk to make a shellscript from the files contents. If >no-one has any better ideas, I'll pop back and give you some tips if you're >interested.

    Sure I would be very interested!

    I'm not a programmer but once I can build a little script that does certain things, I can use it and learn from it about scripting.

    Here's what I've got which sort of works so far;

    sed -e 's:$:\n:' filename | ssh -l 192.168.1.235

    This seems to work but it's more complicated than what I need and I have two small problems with it.

    For one, the machine won't accept any more than 30 entries at a time so if my file has more than 30 entries, it just bombs.
    I need to find a way of sending 30 entries (lines from the file), sleep x, continue with the next 30, etc, until entire file is done, then close connection. It's possible that I might have to logout and log back in for the next 30, I won't know until I try it.

    The second part is that I have to put the command in the file since I've not found a way of; reading the next line, sending a command at the start of the line, repeating. Instead, I've had to edit the file and enter the command at the start of each line... messy.

    These little tools are so useful sometimes though, it's so cool to know these things. Learning a little bit from each experience leads to a nice set of tools over the years.

    Mike

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by mprog View Post
    Anyone have a script that would allow a persistent ssh connection to a server, then reading in a line from a local file, sending it with a return to the remote, reading the next line, sending it, etc, until the file is done.

    Thanks so much if you can help.

    Mike
    Mmmm, if you have no real reason to use a persisten connection, you can always use scp to bring the file, modify it locally, and the scp back it. That will require a password unless you setup ssh keys or something (but ssh would require the same thing nonetheless anyway).

    If sshfs is available, then you can mount the remote folder locally as well, and operate on it as if it was a local file.

    Doing so in ssh is more difficult, because once you are logged in remotely the script will be stalled, and you will need to run *another* script remotelly...

    I see no easy way to do so.

  5. #5
    Just Joined!
    Join Date
    Apr 2008
    Posts
    45
    >scp back it. That will require a password unless you setup ssh keys or >something (but ssh would require the same thing nonetheless anyway).

    Yes, if only the remote had scp .

    Does not seem to be that difficult, just playing around, I've already found a few basic ways of doing this. Now I need to know how to add a command into my string.

    Mike

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by mprog View Post
    >scp back it. That will require a password unless you setup ssh keys or >something (but ssh would require the same thing nonetheless anyway).

    Yes, if only the remote had scp .
    You don't need scp in the remote box, only in the local one. But you need the ssh daemon running on it. but you need ssh running on it anyway if you intend to login with ssh anyway...

  7. #7
    Just Joined!
    Join Date
    Apr 2008
    Posts
    45
    Quote Originally Posted by i92guboj View Post
    You don't need scp in the remote box, only in the local one. But you need the ssh daemon running on it. but you need ssh running on it anyway if you intend to login with ssh anyway...
    It's a one way street into the server. The server I need to connect to has an ssh server but nothing more. It cannot accept scp files or connections because it is an embedded system.

    This is why I need to read the local file, send the text, read again, etc.

Posting Permissions

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