Spawn subshell for SSH and continue with program flow

前端 未结 7 1164
别那么骄傲
别那么骄傲 2020-12-19 16:28

I\'m trying to write a shell script that automates certain startup tasks based on my location (home/campusA/campusB). I go to University and take classes in two different ca

7条回答
  •  没有蜡笔的小新
    2020-12-19 16:37

    Having a ssh with pseudy tty on background shell

    In addition to @shellter's answer, I would like make some precision:

    where @shelter said:

    The man page on our system gave no hint that such a thing was possible

    On my system (Debian 7 GNU/Linux), if I hit:

    man -Pcol\ -b ssh| grep -A3 '^ *-t '
    

    I could read:

         -t      Force pseudo-tty allocation.  This can be used to execute arbi‐
                 trary screen-based programs on a remote machine, which can be
                 very useful, e.g. when implementing menu services.  Multiple -t
                 options force tty allocation, even if ssh has no local tty.
    

    Yes: Multiple -t options force tty allocation, even if ssh has no local tty.

    This mean: If you remotely run a tool that require access to pseudo terminal ( pty like /dev/pts/0), you could run them by using -t switch.

    But this would work only if ssh is run from a shell console (aka having his own pty). If you plan to run them is shell session without console, like background scripts, you may use Multiple -t to enforce pseudo tty allocation from ssh.

    Multiple ssh shell on one ssh connection

    In addition to answers from @tommy and @geekosaur, I would make some precision:

    @tommy point to a very intersting feature of ssh. Not sure this have a lot to do with answer, but speaking around long time connection, this feature has to be clearly understood.

    Once a connection is established, ssh could (and know how to) use them to drive a lot of thing in this one connection:

    • -L let you drive remote TCP connections to local machines/network. (full syntax is: -L localip:localport:distip:distport) where localip could be specified to permit other hosts from same local domain to access same tcp bind, and distip could by any host from distant network ( not only localhost ) sample: -L192.168.1.31:8443:google.com:443 permit any host from local domain to reach google through your host: http://192.168.1.31:8443

    • -R Same remarks in reverse way!

    • -M Tell ssh to open a local unix socket for bindind next ssh consoles. Simply open two terminal window. First in both window, hit: ssh somewhere than hit netstat -tan | grep :22 or netstat -tan | grep 192.168.1.31:22 (assuming 192.168.1.31 is your onw host's ip)

      Than compare close all your ssh session and in first terminal, hit: ssh -M somewhere and in second, simply ssh somewhere. you may see in second terminal:

      $ ssh somewhere
      + ssh somewhere
      Last login: Mon Feb  3 08:58:01 2014 from elsewhere
      

      If now you hit netstat -tan | grep 192.168.1.31:22 (on any of two oppened ssh session;) you must see that there is only one tcp connection.

      This kind of features could be used in combination with -L and maybe some sleep 86399...

      To work around a tcp killer router that close every inactive TCP connection from more than 120 seconds, I run:

      ssh -M somewhere 'while :;do uptime;sleep 60;done'
      

      This ensure connection stay up even if I dont hit a key for more than two minutes.

提交回复
热议问题