PuTTY scripting to log onto host

前端 未结 8 899
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 11:53

I\'m using PuTTY to remotely log onto my school\'s host. Upon logging in, we are required to do these steps:

  1. enter username
  2. enter password
  3. comman
8条回答
  •  长发绾君心
    2021-02-14 12:33

    I'm not sure why previous answers haven't suggested that the original poster set up a shell profile (bashrc, .tcshrc, etc.) that executed their commands automatically every time they log in on the server side.

    The quest that brought me to this page for help was a bit different -- I wanted multiple PuTTY shortcuts for the same host that would execute different startup commands.

    I came up with two solutions, both of which worked:

    (background) I have a folder with a variety of PuTTY shortcuts, each with the "target" property in the shortcut tab looking something like:

    "C:\Program Files (x86)\PuTTY\putty.exe" -load host01
    

    with each load corresponding to a PuTTY profile I'd saved (with different hosts in the "Session" tab). (Mostly they only differ in color schemes -- I like to have each group of related tasks share a color scheme in the terminal window, with critical tasks, like logging in as root on a production system, performed only in distinctly colored windows.)

    The folder's Windows properties are set to very clean and stripped down -- it functions as a small console with shortcut icons for each of my frequent remote PuTTY and RDP connections.

    (solution 1) As mentioned in other answers the -m switch is used to configure a script on the Windows side to run, the -t switch is used to stay connected, but I found that it was order-sensitive if I wanted to get it to run without exiting

    What I finally got to work after a lot of trial and error was:

    (shortcut target field):

    "C:\Program Files (x86)\PuTTY\putty.exe" -t -load "SSH Proxy" -m "C:\Users\[me]\Documents\hello-world-bash.txt"
    

    where the file being executed looked like

    echo "Hello, World!"
    echo ""
    export PUTTYVAR=PROXY
    /usr/local/bin/bash
    

    (no semicolons needed)

    This runs the scripted command (in my case just printing "Hello, world" on the terminal) and sets a variable that my remote session can interact with.

    Note for debugging: when you run PuTTY it loads the -m script, if you edit the script you need to re-launch PuTTY instead of just restarting the session.

    (solution 2) This method feels a lot cleaner, as the brains are on the remote Unix side instead of the local Windows side:

    From Putty master session (not "edit settings" from existing session) load a saved config and in the SSH tab set remote command to:

    export PUTTYVAR=GREEN; bash -l
    

    Then, in my .bashrc, I have a section that performs different actions based on that variable:

    case ${PUTTYVAR} in
      "")
        echo "" 
        ;;
      "PROXY")
        # this is the session config with all the SSH tunnels defined in it
        echo "";
        echo "Special window just for holding tunnels open." ;
        echo "";
        PROMPT_COMMAND='echo -ne "\033]0;Proxy Session @master01\$\007"'
        alias temppass="ssh keyholder.example.com makeonetimepassword"
        alias | grep temppass
        ;;
      "GREEN")
        echo "";
        echo "It's not easy being green"
        ;;
      "GRAY")
        echo ""
        echo "The gray ghost"
        ;;
      *)
        echo "";
        echo "Unknown PUTTYVAR setting ${PUTTYVAR}"
        ;;
    esac
    

    (solution 3, untried)

    It should also be possible to have bash skip my .bashrc and execute a different startup script, by putting this in the PuTTY SSH command field:

    bash --rcfile .bashrc_variant -l 
    

提交回复
热议问题