PuTTY scripting to log onto host

2019-03-25 14:57发布

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. command "add oracle"
  4. command "sqlplus"
  5. enter username
  6. 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

7条回答
我命由我不由天
2楼-- · 2019-03-25 15:06

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 called putty.bat with these contents on my Windows machine:

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

This logs me in remotely to the Linux host. On the host side, I created a shell file called sql with these contents:

#!/bin/tcsh

add oracle10g
sqlplus username password

My host's Linux build used tcsh. Other Linux builds might use bash, so simply replace tcsh with bash and you should be fine.

To summarize, automating these steps are now done in two easy steps:

  1. Double-click putty.bat. This opens PuTTY and logs me into the host.
  2. Run command tcsh sql. This adds the oracle tool to my host, and logs me into the sql database.
查看更多
一纸荒年 Trace。
3楼-- · 2019-03-25 15:06

You can use the -i privatekeyfilelocation in case you are using a private key instead of password based.

查看更多
你好瞎i
4楼-- · 2019-03-25 15:07

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.

查看更多
Summer. ? 凉城
5楼-- · 2019-03-25 15:08

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, see 3.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

查看更多
闹够了就滚
6楼-- · 2019-03-25 15:17

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 ;

查看更多
SAY GOODBYE
7楼-- · 2019-03-25 15:18

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.

查看更多
登录 后发表回答