Can I ssh somewhere, run some commands, and then leave myself a prompt?

前端 未结 5 1305
予麋鹿
予麋鹿 2020-11-29 04:56

I find myself needing to log into various servers, set environment variables, and then work interactively.

e.g.

$ ssh anvil
jla@anvil$ export V=hello         


        
相关标签:
5条回答
  • 2020-11-29 05:31

    It is worth to note that ssh -t can actually be used to connect to one host via another host.

    So for example if you want to execute a command on anvil, but anvil is only accessible from host gateway (by firewall etc.), you can do like this:

    ssh gateway -t 'ssh anvil -t "export V=hello; export W=world;bash -l";'
    

    Exiting the anvil, will also log you out of gateway (if you want to stay on gatway after leaving anvil than just add another bash -l before closing the command.

    0 讨论(0)
  • 2020-11-29 05:32

    You could also use the following expect script:

    #!/usr/bin/expect -f
    spawn ssh $argv
    send "export V=hello\n"
    send "export W=world\n"
    send "echo \$V \$W\n"
    interact
    
    0 讨论(0)
  • 2020-11-29 05:42

    Turns out this is answered by this question:

    How can I ssh directly to a particular directory?

    to ssh:

    ssh -t anvil "export V=hello; export W=world; bash"
    

    followed by:

    jla@anvil$ echo $V $W
    hello world
    
    0 讨论(0)
  • 2020-11-29 05:49

    Another approach is to execute this beast (also gives me a colored shell):

    ssh host -t "echo 'rm /tmp/initfile; source ~/.bashrc; cd foo/; git status' > /tmp/initfile; bash --init-file /tmp/initfile"

    0 讨论(0)
  • 2020-11-29 05:53

    Probably the simplest thing is:

    $ ssh -t host 'cmd1; cmd2; sh -i'
    

    If you want to set variables, do:

    $ ssh -t host 'cmd1; cmd2; FOO=hello sh -i'
    

    Note that this is a terrible hack, and you would be much better off putting your desired initial commands in a script and doing:

    $ scp setup host:~
    $ ssh host
    host$ . setup
    
    0 讨论(0)
提交回复
热议问题