I'm using PuTTY to remotely log onto my school's host. Upon logging in, we are required to do these steps:
- enter username
- enter password
- command "add oracle"
- command "sqlplus"
- enter username
- enter password
I will be logging into this host a lot over the course of this semester and I was hoping to create a script that would eliminate the redundancy of the above steps. Ignoring the obvious security oversights of having my password in the script, how would I achieve this? I have zero experience with scripting, so your feedback is greatly appreciated. Thanks!
Edit: I played around with the command-line options for Putty and I was able to bypass steps 1-2 using:
putty -load "host" -l username -pw password
I've also created a shell file that looks like so:
#!/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 logs into the host and then immediately exits. Is there a way to keep my session open after running the shell file or am I using the -m option wrongly? Here is a link to a PuTTY guide that I have been following: http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter3.html.
Here is the total command that I am trying to run from the command-line:
putty -load "host" -l username -pw password -m c:\test.sh
Figured this out with the help of a friend. The
-m
PuTTY option will end your session immediately after it executes the shell file. What I've done instead is I've created a batch script calledputty.bat
with these contents on my Windows machine:This logs me in remotely to the Linux host. On the host side, I created a shell file called
sql
with these contents:My host's Linux build used
tcsh
. Other Linux builds might usebash
, so simply replace tcsh with bash and you should be fine.To summarize, automating these steps are now done in two easy steps:
putty.bat
. This opens PuTTY and logs me into the host.tcsh sql
. This adds the oracle tool to my host, and logs me into the sql database.You can use the
-i privatekeyfilelocation
in case you are using a private key instead of password based.I want to suggest a common solution for those requirements, maybe it is a use for you: AutoIt. With that program, you can write scripts on top of any window like Putty and execute all commands you want to (like button pressing or mouse clicking in textboxes or buttons).
This way you can emulate all steps you are always doing with Putty.
When you use the
-m
option putty does not allocate a tty, it runs the command and quits. If you want to run an interactive script (such as a sql client), you need to tell it to allocate a tty with-t
, see3.8.3.12 -t and -T: control pseudo-terminal allocation
. You'll avoid keeping a script on the server, as well as having to invoke it once you're connected.Here's what I'm using to connect to mysql from a batch file:
#mysql.bat start putty -t -load "sessionname" -l username -pw password -m c:\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
entering a command after you logged in can be done by going through SSH section at the bottom of putty and you should have an option Remote command (data to send to the server) separate the two commands with ;
mputty can do that but it does not seem to work always. (if that wait period is too slow)
mputty uses putty and it extends putty. There is an option to run a script. If it does not work, make sure that wait period before typing is a high value or increase that value. See putty sessions , then name of session, right mouse button,properties/script page.