Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Get SSH to simulate an interactive session, non-interactively

  1. #1
    Join Date
    Jan 2012
    Location
    /a/
    Beans
    753
    Distro
    Kubuntu 13.04 Raring Ringtail

    Get SSH to simulate an interactive session, non-interactively

    I'm trying to connect to a shell server via ssh non-interactively, however it seems to treat sessions quite differently based on whether or not it is an interactive or non-interactive session. Basically, I want to be able to send this command:
    Code:
    ssh me@shell.example.com "cd directory; ./script.sh"
    AS IF I had, as separate commands, ran:
    Code:
    me@computer:~$ ssh me@shell.example.com
    [me@shell ~]$ cd directory
    [me@shell ~/directory]$ ./script.sh

    How do I do this so the shell server thinks I am running an interactive session even if I'm not?
    The whole thing is so patently infantile, so foreign to reality, that to anyone with a friendly attitude to humanity it is painful to think that the great majority of mortals will never be able to rise above this view of life.
    ~Sigmund Freud

  2. #2
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,527
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Get SSH to simulate an interactive session, non-interactively

    There's nothing wrong with the command you gave, though I would use a full path to cd, e.g., "cd /path/to/some/directory". Alternatively you can just put the script in a location in your PATH, or invoke the script with a full path like "/path/to/some/directory/script.sh". It depends on whether the script expects to find other files it needs to work with in the same directory as itself. If the script uses full paths, then the "cd directory" command is probably unnecessary.

    If you want to send commands via SSH, you should set up public-key authentication to avoid being prompted for a password.
    Last edited by SeijiSensei; March 29th, 2013 at 05:55 PM.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  3. #3
    Join Date
    Jan 2012
    Location
    /a/
    Beans
    753
    Distro
    Kubuntu 13.04 Raring Ringtail

    Re: Get SSH to simulate an interactive session, non-interactively

    Quote Originally Posted by SeijiSensei View Post
    There's nothing wrong with the command you gave, though I would use a full path to cd, e.g., "cd /path/to/some/directory". Alternatively you can just put the script in a location in your PATH, or invoke the script with a full path like "/path/to/some/directory/script.sh". It depends on whether the script expects to find other files it needs to work with in the same directory as itself. If the script uses full paths, then the "cd directory" command is probably unnecessary.

    If you want to send commands via SSH, you should set up public-key authentication to avoid being prompted for a password.
    I don't think you understand my question. I'm not asking how to run a script non-interactively. I can already do that and it works.

    Yes the script uses relative paths, so I have to use cd before running it. And I already have public-key authentication set up.

    The issue though, is that the server will "treat" me differently if I'm in an interactive session vs non-interactive. I want to "trick" the server into thinking it's interactive.

    Specifically, the shell account will disconnect me after one hour in an interactive session to prevent using too much bandwidth (i.e. someone forgets they're connected and stay on for too long). However if it's non-interactive the server disconnects after about 40 seconds, probably to prevent abuse. I need a little over 40 seconds of non-interactive connection, so I need a way to tell the server it is interactive when it's not.

    Would "expect" help me with this? If so how could I use it for this purpose?
    Last edited by Stonecold1995; March 30th, 2013 at 01:33 AM.
    The whole thing is so patently infantile, so foreign to reality, that to anyone with a friendly attitude to humanity it is painful to think that the great majority of mortals will never be able to rise above this view of life.
    ~Sigmund Freud

  4. #4
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,527
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Get SSH to simulate an interactive session, non-interactively

    I leave SSH sessions open in a terminal for days without disconnecting. Do you have control over both ends of the connection? It doesn't sound like it.

    I don't know about expect. Forty seconds is a long time for running a script. (Mine usually complete in a couple of seconds or less.) What is it waiting for?

    What if the script spawns a job in the background that pings your end of the session once per second (the default) and ends after a minute like this:

    Code:
    ping -c 60 ip.address.of.client > /dev/null &
    Last edited by SeijiSensei; March 30th, 2013 at 03:42 AM.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  5. #5
    Join Date
    Jan 2012
    Location
    /a/
    Beans
    753
    Distro
    Kubuntu 13.04 Raring Ringtail

    Re: Get SSH to simulate an interactive session, non-interactively

    I do not have control over both ends. It is a free shell account.

    The script scrapes a few URLs, saves it to a file, and exits. But the server uses Tor, so it is much slower than normal, making it take slightly too long.

    What would pinging my client do?

    I cannot run any processes unless I'm connected. The main limitations per account are 1) no processes can run in the background while you're away, 2) only two simultaneous connections can be established, and 3) only 16 processes (including bash, sshd, etc) can be run at once.
    The whole thing is so patently infantile, so foreign to reality, that to anyone with a friendly attitude to humanity it is painful to think that the great majority of mortals will never be able to rise above this view of life.
    ~Sigmund Freud

  6. #6
    Join Date
    Mar 2013
    Beans
    30

    Re: Get SSH to simulate an interactive session, non-interactively


  7. #7
    Join Date
    Jan 2012
    Location
    /a/
    Beans
    753
    Distro
    Kubuntu 13.04 Raring Ringtail

    Re: Get SSH to simulate an interactive session, non-interactively

    Quote Originally Posted by mharv View Post
    How can I use expect to do this? The syntax is very confusing...

    It has to do this:
    1) ssh into a server
    2) cd into a directory
    3) run a script (the script closes the ssh connection automatically when it's finished)
    4) wait a certain amount of time
    5) goto step 1

    Currently I have:
    Code:
    expect << EOD
    spawn ssh me@shell.example.com
    expect 'me@shell'
    send "cd directory; ./script.sh\n"
    expect 'me@shell'
    send "logout\n"
    EOD
    However it seems to be randomly exiting in the middle, as in expect itself closes, which kills its child (ssh). How can I get it to actually wait before dying on me?
    Last edited by Stonecold1995; March 30th, 2013 at 12:16 PM.
    The whole thing is so patently infantile, so foreign to reality, that to anyone with a friendly attitude to humanity it is painful to think that the great majority of mortals will never be able to rise above this view of life.
    ~Sigmund Freud

  8. #8
    Join Date
    Sep 2006
    Beans
    7,198
    Distro
    Lubuntu Development Release

    Re: Get SSH to simulate an interactive session, non-interactively

    You might be able to keep the connection open by using the options ServerAliveInterval and ServerAliveCountMax on your client. They can be specified in the shell as runtime arguments or added to ~/.ssh/config once you find settings that you want to keep and make permanent.

  9. #9
    Join Date
    Jan 2012
    Location
    /a/
    Beans
    753
    Distro
    Kubuntu 13.04 Raring Ringtail

    Re: Get SSH to simulate an interactive session, non-interactively

    Quote Originally Posted by Lars Noodén View Post
    You might be able to keep the connection open by using the options ServerAliveInterval and ServerAliveCountMax on your client. They can be specified in the shell as runtime arguments or added to ~/.ssh/config once you find settings that you want to keep and make permanent.
    Uh, it has nothing to do with ssh closing the connection... ssh dies because expect closes, and expect is ssh's parent, so naturally ssh is killed.

    I want expect to wait until the script has finished running (and the normal prompt is again displayed), at which point it should send "logout\n". How can I get it to do that?
    The whole thing is so patently infantile, so foreign to reality, that to anyone with a friendly attitude to humanity it is painful to think that the great majority of mortals will never be able to rise above this view of life.
    ~Sigmund Freud

  10. #10
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    5,429
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: Get SSH to simulate an interactive session, non-interactively

    I know that running a command under ssh gets treated differently to running it in an ssh shell login. For example, top works in a normal ssh shell login, but fails when run as "ssh hostname top" with error "TERM environment variable not set.".

    But for what you are trying to do, can I suggest using screen? Screen acts as a screen-buffer between you and the commands you run. You can set a program running in screen, disconnect, reconnect later and see the program still running. You can even run several programs, hot-key between the virtual screens. I use it (for instance) I do an upgrade that must not be interrupted but I'm doing it by ssh over a flakey network. If the network fails and disconnects me, I just reconnect and see where the update has got to.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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