How to write a script to automate Putty (PLINK) on Windows 10 - vbscript

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

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)

Related

xset in BASH script does not work under Cron

To learn about BASH scripting, I set myself the objective to write a Cron script which shuts down a PC with Mint 20 when activity on the Ethernet interface dropped below a threshold over an 1 hour.
I mainly (but not exclusively) use the PC as File/DLNA Server. The script works, but now I find that it also shuts down the PC the rare times I'm using the the front end. So I want my script to verify if the screen has been blanked (As per Power Management settings)
To test the principle I included this in my script:
screenon=$(/usr/bin/xset -q | grep 'Monitor is' | cut -d "s" -f 2)}
which when run in a terminal window gives (debug: set -x)
screenon= On
but when run from cron gives. (logger)
/usr/bin/xset: unable to open display ""
I have learned about similar problems, but cannot figure out how to solve this.
My script includes:PATH=$PATH:/usr/local/bin
and my PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Thanks in advance for any help.

How do I create a .bat (batch) file that presses Enter key every 10800 seconds in a loop?

I've tried to make it myself, but it didn't work, as I'm not experienced enough with .bat to get it working.
So what I need is bascily a .bat file which I only need to click once and it will "simulate" me clicking the key 'Enter' every 10800 seconds (3 hours) in a loop, so that it doesn't ever stop clicking the key 'Enter' with a loop time of 10800 seconds, would someone help me out please! :)
You can do this with VBScript and the WScript host:
Save this as "press_enter.vbs" and run it from the CLI with "wscript press_enter.vbs"
Dim WshShell, FSO, secs
Set WshShell = WScript.CreateObject("WScript.Shell")
Set FSO = CreateObject("Scripting.FileSystemObject")
secs = 10800
kill_file = "C:\Users\Public\Documents\kill_switch.txt"
Do While true
WScript.Sleep(secs * 1000) ' this is in milliseconds, multiple by 1000
WshShell.SendKeys "{ENTER}"
if FSO.FileExists(kill_file) then exit do
Loop
As you probably guessed, the program runs forever until it detects the existence of the file "C:\Users\Public\Documents\kill_switch.txt"
You can also kill the program by opening Task Manager and killing the "Microsoft Windows Based Script Host" which will be listed under the "Background processes" section...
Remember that if you stop the application with the "kill_switch.txt" file, you will need to remove it before you start the application again. Perhaps it is best just to kill it with Task Manager...

Disconnect automatically a vpn connection after 5 hours

Is there a way to create a batch file or run some command like shutdown -s -t , where I can include time after which a vpn connection will be disconnected for example after 4 or 5 hours.
My gratitude to everyone.
I'd say you start this script up at a similar time that you start the VPN software up:
TIMEOUT /T 18000 /NOBREAK
TaskKill /f /IM VPNName.exe
This will wait for 5 hours, specified by the 18000 ( the seconds it should wait ) and will then forcefully close your VPN executable. Of course, you must replace VPNName with the executable's name. Also, you should try to change the /t value to test it as 5 hours is a long time to wait to test something.
By the way, this is a batch file.
thank you for your help. I have found something that may prove more useful in the future, though. It's called Perfect Automation, and I can record any action and set it to run at some specific time.
My thanks extended to the creator of this software as well as to you.

Run a batch file 5 minutes after startup in Windows Xp

I want to execute a .bat file 5 minutes after windows starts up. Unfortunately, windows task scheduler doesn't offer anything of the sort, only execute something right on start up. However, I need something to be 5 minutes after startup.
.bat file doesn't do much, just calls one separate .cmd file and passes a parameter. I've tried:
timeout /t 300 /nobreak
"C:\Documents and Settings\Administrator\Desktop\sikuli\runIDE.cmd" -r "C:\Documents and Settings\Administrator\Desktop\sikuli\SikuliXmlTestRunner.sikuli"
However, the runIDE.cmd gets called right away, regardless of the timeout.
You can give wait(300) command at the beginning in your sikuli script to achieve this.
XP doesn't have timeout command, use ping -n 300 localhost>nul or ping -n 1 -w 300000 localhost>nul.

User session timeout force logoff

I have been doing a little research on how to make a script using vbs or batch to set a session time limit for users logging onto a specific set of computers linked through Group Policy.
I am not familiar with log off or timed scripts, I can make the simplest batch script to log the current user off a machine. The problem I get to is not being able to set a timed session, or running the log-off script due to the time limit.
I researched setting it through group policy, then came up with the idea of linking a script to a group policy in the start up for users on the certain set of computers.
I found a vbs by googling key phrases but I am not sure exactly how to implement it with the log off script or how to make it begin its count down
echo.
echo Waiting For thirty minutes...
TIMEOUT /T 1800 /NOBREAKS
echo.
echo (logoff)
echo.
pause >nul
The log off script that I was planning on using goes along the lines of:
shutdown -l -f -t 30 -c "Your 30 minute session is over."
But I cant get the comment to display and the 30 second time doesn’t take affect.
Any help would be greatly appreciated. Thank you for reading.
How about somthing like this?
Set oSystems = GetObject("winmgmts:{(Shutdown)}//./root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
For Each oSystem in oSystems
oSystem.Win32ShutdownTracker 3600,"Logging off...",0,4
Next
I know this question is old but here you go anyway:
have a .vbs file called invis.vbs and place it in the Startup directory of the user you want to be limited.
It should contain the following (replace #username# with the Username):
wscript.exe “C:\Users\#username#\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Start-up\invis.vbs” “C:\SessionLimit.bat”
Now save the below as a batch file called SessionLimit.bat in the C:\ Directory.
It should contain the following:
#echo off
timeout 1800 /nobreak
shutdown /l ;;Replace this comment with /f if you want the log off to be forced without the Do you want to save your work interruption window.

Resources