PuTTY scripts to enter the host

I use PuTTY to log in remotely on my host. When entering the system, we need to perform the following steps:

  • enter username
  • enter password
  • add oracle command
  • sqlplus command
  • enter username
  • enter password

I will be registering on this host a lot during this semester, and I was hoping to create a script that would eliminate the redundancy of the above steps. Ignoring the obvious security issues of my password in a script, how could I achieve this? I have no experience with scripts, so your feedback is greatly appreciated. Thanks!

Edit: I was playing with command line options for Putty, and I was able to get around steps 1-2 using:

putty -load "host" -l username -pw password 

I also created a shell file that looks like this:

 #!/bin/bash add oracle10g sqlplus username password 

When I try to add this option to the command line using the -m option, it looks like PuTTY registers with the host and then exits immediately. Is there a way to open my session after running the shell file, or am I using the -m option incorrectly? Here is a link to the PuTTY manual that I follow: http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter3.html .

Here is the general command that I am trying to run from the command line:

 putty -load "host" -l username -pw password -mc:\test.sh 
+7
source share
7 answers

Thought about it with the help of a friend. The -m PuTTY option ends the session immediately after the shell file is executed. Instead, I created a script package called putty.bat with this content on my Windows machine:

 @echo off putty -load "host" -l username -pw password 

This removes me remotely to the Linux host. On the host side, I created a shell file called sql with this content:

 #!/bin/tcsh add oracle10g sqlplus username password 

My Linux build host is using tcsh . Other Linux builds may use bash , so just replace tcsh with bash and everything will be fine.

To summarize, the automation of these steps is done in two simple steps:

  • Double-click putty.bat . This opens up PuTTY and registers me with the host.
  • Run the tcsh sql command. This adds the oracle tool to my host and writes me to the sql database.
+7
source

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 
+5
source

When you use the -m option, putty does not highlight tty, it runs the command and exits. If you want to run an interactive script (such as the sql client), you need to tell it to allocate tty with -t , see 3.8.3.12 -t and -T: control pseudo-terminal allocation . You will avoid using a script on the server, and also call it after connecting.

Here is what I use to connect to mysql from a batch file:

#mysql.bat start putty -t -load "sessionname" -l username -pw password -mc:\mysql.sh

#mysql.sh mysql -h localhost -u username --password="foo" mydb

https://superuser.com/questions/587629/putty-run-a-remote-command-after-login-keep-the-shell-running

+2
source

I want to offer a general solution for these requirements, maybe it will be useful for you: AutoIt . With this program, you can write scripts on top of any window, such as Putty, and execute all the commands you want (for example, clicking a button or clicking in text windows or buttons).

This way you can emulate all the steps you always take with Putty.

0
source

entering a command after logging in can be done by going through the SSH section at the bottom of the putty, and you should have an option. The remote command (data to send to the server) separates the two teams;

0
source

mputty can do this, but it does not always work. (if this waiting period is too slow)

mputty uses putty, and it expands the putty. It is possible to run a script. If this does not work, make sure that the wait period for input is a large value or increases this value. See Putty Sessions, Session Name, Right-Click, Properties / script.

0
source

You can use -i privatekeyfilelocation if you use a private key instead of a password.

-one
source