Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

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

  1. #11
    Join Date
    Jan 2012
    Beans
    753

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

    Quote Originally Posted by The Cog View Post
    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.
    Using screen isn't allowed to prevent abuse of the free account.

  2. #12
    Join Date
    Jan 2012
    Beans
    753

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

    Ok, so this is exactly what I'm running, word for word:
    Code:
    alex@kubuntu:/tmp$ cat test.sh
    #!/bin/bash
    
    while true; do
        expect << EOD
    spawn ssh stonecold@shell.cjb.net
    expect 'stonecold@shell'
    send "cd gelbooru; ./scrape_deleted.sh\n"
    expect 'stonecold@shell'
    send "logout\n"
    EOD
    echo "Done, sleeping 1 hour"
    sleep 1h
    echo "Restarting"
    done
    alex@kubuntu:/tmp$ ./test.sh
    spawn ssh stonecold@shell.cjb.net
    Last login: Sat Mar 30 05:58:58 2013 from 46.165.196.138
    [stonecold@shell ~]$ cd gelbooru; ./scrape_deleted.sh
    Started Sat Mar 30 05:59:29 MDT 2013
    Scraping post 51303
    Scraping post 51304
    Scraping post 51305
    Scraping post 51306
    Scraping post 51307
    Scraping post 51308
    Scraping post 51309
    Scraping post 51310
    Scraping post 51311
    Scraping post 51312
    Scraping post 51313
    Scraping post 51314
    Scraping post 51315
    Scraping post 51316
    Scraping post 51317
    Scraping post 51318
    Scraping post 51319
    Done, sleeping 1 hour
    ^C
    I had to control+c because it only exits and says "Done, sleeping for 1 hour" when ssh (and expect) closes.

    So the shell server is disconnecting very soon after I connect, and I need the script to be able to run a little while longer.

    When I connect normally, run "cd gelbooru" and "./scrape_deleted.sh", I remain connected for a full hour if I have to. When I connect either using expect or " ssh stonecold@shell.cjb.net 'cd gelbooru; ./scrape_deleted.sh' ", it disconnects in several seconds. HOW to I prevent this specifically?
    Last edited by Stonecold1995; March 31st, 2013 at 02:29 AM. Reason: typo and link

  3. #13
    Join Date
    Mar 2013
    Beans
    30

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

    Those links should answer your question, but I've never needed to do that so I've not tested it out.
    http://lists.samba.org/archive/linux...il/029884.html
    http://www.linuxquestions.org/questi...script-770340/

  4. #14
    Join Date
    Jan 2012
    Beans
    753

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

    Quote Originally Posted by mharv View Post
    Those links should answer your question, but I've never needed to do that so I've not tested it out.
    http://lists.samba.org/archive/linux...il/029884.html
    http://www.linuxquestions.org/questi...script-770340/
    Those don't really tell me anything...

    Is it "expect eof"? I have no idea what that does.

  5. #15
    Join Date
    Jan 2012
    Beans
    753

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

    I think I've found a way to get it to work, sort of.

    I just created .bash_profile with contents that would run the script automatically. Then all I have to do is ssh into it to run the script, without needing expect.

    Let's see how this goes.

  6. #16
    Join Date
    Mar 2013
    Beans
    30

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

    Best of luck, hope it works.

  7. #17
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

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

    Quote Originally Posted by Stonecold1995 View Post
    I just created .bash_profile with contents that would run the script automatically. Then all I have to do is ssh into it to run the script, without needing expect.
    Since you already have public key authentication set up it might work to put command="somecommand" in your ~/.ssh/authorized_keys. The details are in the man page for sshd in the section "AUTHORIZED_KEYS FILE FORMAT" That will allow you to run your script yet still have shell access when you need it.

  8. #18
    Join Date
    Jan 2012
    Beans
    753

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

    Quote Originally Posted by Lars Noodén View Post
    Since you already have public key authentication set up it might work to put command="somecommand" in your ~/.ssh/authorized_keys. The details are in the man page for sshd in the section "AUTHORIZED_KEYS FILE FORMAT" That will allow you to run your script yet still have shell access when you need it.
    I just have this in my .bash_profile:
    Code:
    #!/bin/bash
    
    echo "Starting scraper in 10 seconds.  Press ctrl+c to cancel."
    if sleep 10; then
        cd gelbooru
        ./scrape_deleted.sh
        cd "$OLDPWD"
    fi
    echo
    So I'll still be able to work if I want to, otherwise if it's pseudo-interactive it'll just go on and run the script.

    Looks like problem solved!

Page 2 of 2 FirstFirst 12

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
  •