Reputation: 41
I have about 3000 individual commands that I need to execute on a system via PuTTY. I am doing this by copying ~100 of the commands and pasting them into a PuTTY SSH session. It works, however the issue is that PuTTY does not process them serially and the output gets garbled.
Is there a way to make PuTTY process each command, wait for a return and then process the next? The Windows command prompt does this and I'm thinking there is a way to do so with PuTTY.
Yes, I know I could put this in a Bash script, but due to circumstance outside my control, this has to be done using SSH and in a manner that can be monitored as we go and logged.
Upvotes: 4
Views: 30961
Reputation: 940
In the PuTTY installation I have, I just paste into it and it works.
Click on your PuTTY window. Right-click once, into where you type your commands.
You should see all of the commands inserted into your entry box.
Hit Enter.
Note: I used this to enter multiple lines into cin
prompts from a C++ program compiled on Linux. I don't know if it will work directly into the terminal.
Upvotes: -1
Reputation: 3533
I do this all the time. Put your commands in a (
)
block, which will run it as a subshell, perfectly everything within serially. I'm running Windows PuTTY and connecting to Linux and AIX servers. Try it.
(
Command1
Command2
Command3
)
In practice, I might have a huge load of many 100s of statements I want to run, in Notepad++ or whatever. So I copy them to clipboard, and then in PuTTY:
(
paste in your wad here
)
If you want to log the output from each of your statements individually, you might do something like this:
(
Command1 > /home/jon/command1output.txt
Command2 > /home/jon/command2output.txt
Command3 > /home/jon/command3output.txt
)
Or if you just want one big stream of output, you could interleave separators for easier reading later:
(
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "[`date`] Now running Command1 ..."
Command1
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "[`date`] Now running Command2 ..."
Command2
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "[`date`] Now running Command3 ..."
Command3
)
Another variation using an inline function. All paste-able into PuTTY, with perfect serial running, logging as command1:output1,command2:output2,... , and capable of driving SQL*Plus.
(
function geniusMagic() {
echo " "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
date
echo "RUNNING COMMAND:"
echo " "
echo "$*"
echo " "
echo "OUTPUT:"
echo " "
sh -c "$*"
}
geniusMagic df -m /home
geniusMagic 'printf $RANDOM | sed "s/0//g"'
geniusMagic 'echo "select count(*)
FROM all_tables;
" | sqlplus -s scott/tiger'
)
Sample output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Wed Jun 25 17:41:19 EDT 2014
RUNNING COMMAND:
df -m /home
OUTPUT:
Filesystem MB blocks Free %Used Iused %Iused Mounted on
/dev/hd1 1024.00 508.49 51% 3164 3% /home
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Wed Jun 25 17:41:19 EDT 2014
RUNNING COMMAND:
printf $RANDOM | sed "s/0//g"
OUTPUT:
2767
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Wed Jun 25 17:41:19 EDT 2014
RUNNING COMMAND:
echo "select count(*)
FROM all_tables;
" | sqlplus -s scott/tiger
OUTPUT:
COUNT(*)
----------
48
Upvotes: 9
Reputation: 663
I'm not sure why you could not use Plink, but you could make a batch file with Notepad++.
plink <hostname> -l <login_name> -pw <password> <command 1>
plink <hostname> -l <login_name> -pw <password> <command 2>
plink <hostname> -l <login_name> -pw <password> <command 3>
...
plink <hostname> -l <login_name> -pw <password> <command 3000>
Run the batch file:
filename.bat > log.txt 2>&1
Upvotes: 1
Reputation: 976
Just an idea here: PuTTY comes with a command-line tool called Plink. You could write a script on your Windows machine that creates a connection to the remote server with Plink, and then parses your list of commands one at a time and sends them.
This should look exactly the same to the remote server (which I assume is what's doing the logging), while letting you have a bit more control than copy-pasting blocks of commands.
Upvotes: 2
Reputation: 2564
Maybe the answer you are looking for is here.
Here a copy of the answer I think may be interesting for you :
// Wait for backup setting prompt
Repeat Until %D1% = 1
Activate Window: "DAYMISYS1.qdx.com - PuTTY"
Mouse Move Window 12, 11 <------- Moves mouse to upper left corner to activate menu options
Mouse Right Button Click
Delay 0.1 Seconds
Text Type: o <------- Activates Copy All to Clipboard command
Delay 0.2 Seconds
If Clipboard Contains "or select a number to change a setting:" <------- Look for text of prompt that I am waiting for
Repeat Exit <------- If found, exit loop and continue macro
End If
Delay 1 Seconds <------- If prompt is not found, continue loop
Repeat End
Upvotes: 0