0

I am doing a manual process that is done each day within Putty and wanted to automate it. There is no need for a person to do this because all of the keyboard inputs do not change each day. I'm trying to free up time to increase productivity, not to mention it is mind-numbing to continually do this every day. The process requires someone to open Putty (this would use plink of course), login (storing the password and username in plain text is fine, steps for generating a key are not necessary), enter the same keyboard presses, output the file manually, and then save it to a network drive folder. So this is a completely unnecessary process to have someone doing it manually and I am seeking a way to complete this automation.

Currently, this is being used in a Windows 10 environment and from what I have read, Putty (plink) is the best route to go. I can utilize other SSH programs if there is a better method as well, but I think this may be the better route from the research I've done. I haven't scripted much at all and I'm trying to learn a bit as I go with this. I need to automate logging into PLINK (done) and then multiple keyboard entries (kind of done) for each screen within the server I'm accessing. Essentially, each screen needs to enter predetermined keyboard keys, such as "ENTER", some numbers 1-10 depending on the screen and then when it is complete, I need to print the results to a file, which preferably would be a xlsx, but csv or text would suffice as well.

I have added the code that I was able to create so far below. I am stuck at this point because PLINK does not remain visible so it's tough to analyze the issue and the cursor keeps jumping to any active window when running it.

Dim WshShell

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "C:\PuTTY\plink.exe 123.server.com -l username -pw password -t{ENTER}"

WScript.Sleep 3000

WshShell.AppActivate "plink.exe"

WScript.Sleep 2000

WshShell.AppActivate "plink.exe"

WshShell.SendKeys "{ENTER}"

WScript.Sleep 6000

WshShell.AppActivate "plink.exe"

WshShell.SendKeys "{ENTER}"

WScript.Sleep 6000

WshShell.AppActivate "plink.exe"

WshShell.SendKeys "command123{ENTER}"

Update

I attempted to use the suggested code and it does work in terms of automating the login and bringing up the application within the server, but it is not accepting any of the inputs I attempt to code. Every time an input is used the following message below is displayed. Please note, my command I'm passing is 100% correct, it just seems to not even consider it. The script does actually input the text into the proper field, but it just errors it out.

Enter program name (or abbreviation): I don't recognize
that program name. Press <return> for a list of programs."

(I have updated code per suggestions).

(
  echo WEST COAST
  timeout /t 5 > nul
  echo 09
  timeout /t 5 > nul
  echo third_screen_keys
) | C:\PuTTY\plink.exe 123.server.com:PORT# -l username -pw password -t
  • Cannot you just put your input to a text file and use the file as an input to Plink? (no VBScript). – Martin Prikryl Apr 01 '20 at 14:51
  • I'm not sure how to code the input file. How do you layout the commands and seperate them from each other (one command per session page to progress to the next screen)? – HonoraryTitle Apr 01 '20 at 15:10
  • You may be looking for something like this: [Wait between sending login and commands to serial port using Plink](https://stackoverflow.com/q/55851109/850848) – It's about serial connection, but it does not matter, the concept would be the same (except that with SSH you can [and should] still login with command-line parameters and use the input file only for the rest). – Martin Prikryl Apr 01 '20 at 15:11
  • Thanks for the reference, but I think that example is a bit different than mine. I'm new to scripting so forgive my ignorance. – HonoraryTitle Apr 01 '20 at 15:17
  • Well, I believe it's exactly what you want. If you do not think so, please explain why not. – Martin Prikryl Apr 01 '20 at 15:32
  • I'm confused on how this would be adapted to my scenario. I don't need root access as i'm already at an admin level, which is all I would need for this. In addition, i'm not removing a directory at all either. I understand, this may allow me to pipe in a txt file with commands to accomplish the task, but I'm not sure where to begin with the second file. – HonoraryTitle Apr 01 '20 at 15:52
  • (Aside: once an answer is given, please do not edit your question in a way that would invalidate their work. If a new reader would not be able to understand why an answer is given from the state of the question they can see, they might downvote it, believing it to be irrelevant. If in doubt, make your changes by way of an update rather than a modification. Note also that questions here are for posterity - we hope that future engineers will read them for many years.) – halfer Apr 01 '20 at 21:43
  • My apologies, I just started using the site and i'm still getting used to it. I will make sure I follow that process going forward. – HonoraryTitle Apr 01 '20 at 22:14

1 Answers1

0

There's a rather similar question here:
Wait between sending login and commands to serial port using Plink

So you should be able to use a batch file like this:

(
  echo first_screen_keys
  timeout /t 5 > nul
  echo second_screen_keys
  timeout /t 5 > nul
  echo third_screen_keys
) | C:\PuTTY\plink.exe 123.server.com -l username -pw password -t

(or the PowerShell equivalent, if you *nix CR line endings)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • How would the keys be coded, it is just like the wscript for example encapsulated like this {ENTER] or {1}? Also, wouldn't the command to run plink come before the code you entered? For example, in this format: C:\PuTTY\plink.exe 123.server.com -l username -pw password -t | ( echo first_screen_keys timeout /t 5 > nul echo second_screen_keys timeout /t 5 > nul echo third_screen_keys ) – HonoraryTitle Apr 01 '20 at 16:52
  • Each line is followed by `Enter` implicitly. If you want to see an explicit enter in the code, you better use the PowerShell code. – And no, the command should be like this. – Martin Prikryl Apr 01 '20 at 16:55
  • Thanks Martin, I am making some progress on this now. Do you know how I would send a command of using the enter key? The first page requires a press of the ENTER key after I login to PLINK – HonoraryTitle Apr 01 '20 at 17:24
  • If you want to send only `Enter`, without any key/letter before, just do `echo.` (that's a batch file trick to send an empty line = enter). – Martin Prikryl Apr 01 '20 at 17:28
  • Martin, thanks for the response. I have it echoing my inputs, but the one input I have is WEST COAST. That input is saying that it is not recognized. However, it appears to properly add the text input, the server just isn't accepting it. Is this because the server is UNIX based? Is it reading some invisible character i'm not seeing? – HonoraryTitle Apr 01 '20 at 18:01
  • Can you add your code to the question? Did you pass any similar prompt or is this the first of the kind? – Martin Prikryl Apr 01 '20 at 19:03
  • Hey Martin, thanks for following up. I updated the original post with more context on the current status. Please let me know if there is anything else I can provide for clarity – HonoraryTitle Apr 01 '20 at 21:36
  • So it did not work any problem yet. So did you try the PowerShell solution (not the *nix line endings)? – Martin Prikryl Apr 02 '20 at 06:41
  • When using the original windows script it doesn't actually accept the text input that the send keys function sends. The font displays in the window, but the server does not recognize the inputs. It's very odd. Any suggestions? I am still stumped on this one. – HonoraryTitle Apr 02 '20 at 19:57
  • For the third time: Try the PowerShell solution. – Martin Prikryl Apr 02 '20 at 20:00