I'm not sure why the previous answers did not suggest that the original poster set up a shell profile (bashrc, .tcshrc, etc.) that would automatically execute its commands every time it logged into the server.
The quest that brought me to this page for reference was a little different - I need several PuTTY shortcuts for the same host that would execute different launch commands.
I came up with two solutions, both of which worked:
(background) I have a folder with many PuTTY shortcuts, each of which has the “target” property on the shortcut tab, which looks something like this:
"C:\Program Files (x86)\PuTTY\putty.exe" -load host01
with every download that matches the PuTTY profile that I saved (with different hosts on the Session tab). (Basically, they differ only in color schemes. I like it when each group of related tasks shares a color scheme in a terminal window, with critical tasks, such as logging in as root in a production system, performed only in clearly colored windows.)
The properties of the folder in the folder are set to very clean and stripped down - it works like a small console with shortcuts for each of my frequent remote PuTTY and RDP connections.
(solution 1) As mentioned in other answers, the -m switch is used to configure the script on the Windows side to run, the -t switch is used to stay in touch, but I found that it is sensitive to order if I want to get it to run without exit
That I finally started working after many trial and error:
(target field label):
"C:\Program Files (x86)\PuTTY\putty.exe" -t -load "SSH Proxy" -m "C:\Users\[me]\Documents\hello-world-bash.txt"
where the executable looks like
echo "Hello, World!" echo "" export PUTTYVAR=PROXY /usr/local/bin/bash
(no semicolons needed)
This runs the script command (in my case, it simply prints “Hello, world” on the terminal) and sets a variable that my remote session can interact with.
Note for debugging: when starting PuTTY, it loads the -m script, if you are editing the script, you need to restart PuTTY, and not just restart the session.
(solution 2) This method feels a lot cleaner since the brain is on the remote Unix side and not on the local side of Windows:
From the Putty master session (not “editing settings” from an existing session), load the saved configuration, and on the SSH tab, delete the command:
export PUTTYVAR=GREEN; bash -l
Then in my .bashrc I have a section that performs different actions based on this 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 not easy being green" ;; "GRAY") echo "" echo "The gray ghost" ;; *) echo ""; echo "Unknown PUTTYVAR setting ${PUTTYVAR}" ;; esac
(decision 3, not verified)
It is also possible to have bash skip my .bashrc and execute another script run by putting it in the PuTTY SSH command field:
bash --rcfile .bashrc_variant -l