Entering SSH passwords using mixed Batch/Jscript script - batch-file

im trying to automate a ssh connetcion to a NAS in order to wake up a computer using WOL.
All goes fine until i try to enter the root password required for the wake command
start cmd.exe #cmd /k "ssh admin#xxx.xxx.xxx.xxx"
ping localhost -n 2
%SendKeys% "xxxxxxxxxx"
%SendKeys% "{ENTER}"
ping localhost -n 5
%SendKeys% "sudo synonet --wake xx:xx:xx:xx:xx:xx eth0"
%SendKeys% "{ENTER}"
%SendKeys% "xxxxxxxxxx"
%SendKeys% "{ENTER}"
this is the result of the above code
strange enough, the ssh password is entered correctly but calling %SendKeys% "{ENTER}" after the wake command cause strange excape characters entered causing the command to fail.
Is there some way to avoid this side effects? Thanks in advance.
i tried the code above but i cannot figure out why it displays strange characters.Also other methods like putty are useless because cannot automatically enter the root password after the command.

Related

How to merge two batch file into one batch file?

I have two batch files as below, and I want to merge them in to one btach file? (I don't want to run them seperately want to one click to run)
First one delete and copy file, second one is opens chrome and login. These batch files working properly one by one when I clicked. But I need one batch file.
Thanks for your help..
First one is:
#echo off
del "C:\web.config"
copy "C:\\Web - New.config" "C:\web.config"
Second one one is:
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
START CHROME "http://localhost:15000/"
rem the script only works if the application in question is the active window. Set a
timer to wait for it to load!
timeout /t 2
rem use the tab key to move the cursor to the login and password inputs. Most htmls
interact nicely with the tab key being pressed to access quick links.
rem %SendKeys% "{TAB}"
rem now you can have it send the actual username/password to input box
%SendKeys% "admin"
%SendKeys% "{TAB}"
%SendKeys% "1234"
%SendKeys% "{ENTER}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
When you have two batchfiles, batch1.bat and batch2.bat and you want both of them to run in one batchfile, the simpliest solution is to create this batchfile:
call batch1.bat
call batch2.bat
There's absolutely no problem running a batchfile from another batchfile!

Set placeholder command in cmd at start shell without executing the command automatically [duplicate]

This question already has answers here:
How can I run cmd from batch file and add text to line without executing?
(4 answers)
Closed 4 years ago.
I want to open a cmd shell and set some command in the promt without executing them. The goal is: Shell opens with command (e.g. echo hello) and the user only need to press enter instead of typing it.
Is this possible? I found the /k switch like cmd /k echo hello but this executes the command immediately. The only workaround I see is something like cmd /k "pause && echo hello" but this is not very transparent as the user doesn't know what got executed.
So there are 2 ways of doing this:
Create a shortcut.
Right click in a folder where you want to store the shortcut
Select new Shortcut
In the command window, type the following:
cmd /k echo press Enter to continue ping localhost command && pause >nul && ping localhost
Click next and give it a name and save.
Now you can double click the file and it will print to screen:
press Enter to continue ping localhost command
when Enter is pressed, it will ping localhost
Batch File
Open notepad.exe
Type the following
#echo off
echo press Enter to continue ping localhost command
pause >nul
ping localhost
pause
Save the file to the desired location and give it a .cmd extension
Now you can double click the file and it will do the same as the previously created shortcut version.
As for transparency, if you want the use to see what will be running, you would need to show the user by echoing what will be done.. See the following example.
#echo off
echo echo hello
pause >nul
echo hello
echo ping localhost
pause >nul
ping localhost
pause..
this only duplicates the actual commands as echo commands, hiding the actual command as well as the pause.
Similarly with the shortcut version:
cmd /k echo echo hello && pause >nul && echo hello && echo ping localhost && pause >nul && ping localhost

Send Command to Bluetooth Serial using Putty/Plink

I have searchewd a few posts here, but I cannot find one that is exactly what I am looking for. In simple terms I am trying to send a character to my Arduino via bluetooth Automatically.
I have tried both Putty and Plink, but neither work automatically. Here is the commands I have tried so far:
command.bat | putty -serial com3 -sercfg 9600
Command.bat:
#echo off
timeout /t 5
echo 2
and
plink -load Arduino echo 2
This connects to the bluetooth adapter on the Arduino, but opens an Interactive console. I can hit the number 2 on the keyboard it is sends it correctly. However I want that to be sent automatically. I have timeout in there because it takes a few seconds to connect to the bluetooth.
Is there a way to do this so I can just run a bat file and have it send the commands automatically?
If the interactive console opens up and is the most present item, you can use the following code upon the interactive console startup...
#if (#CodeSection == #Batch) #then
#echo off
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem wait for interactive console to appear before pressing 2 to initialize
timeout /t 5
%SendKeys% "{2}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
Call it a cheap fix if you will, the code will call a javascript to press 2 for you. Im not sure how to tie it with your absolute program but this will run it automatically as the computer will simulate the input for you.

Entering values to command prompt through a script/batch/ANT/Java Code etc

I want to automate an application. On calling this application (Which starts from a batch file) it shows up a command prompt window in which it asks for values.
By values I mean :-
First, it will ask you to hit ENTER (Can ENTER be automated?)
Then it asks for a path (String, of course)
Then it asks again for another path (String, again)
How can I pass these values to the command prompt using any script/batch/tool/Java program?
Right now these values has to be entered manually, I want to automate this procedure. So, here I don't want to enter them manually, I want a script to do it. Is it possible? If yes, how?
This is a copy of the solution at this post, but adjusted for your particular needs. First, the "application.bat":
#echo off
set /P "=Hit ENTER to continue"
set /P "aPath=Enter a path: "
set /P "anotherPath=Enter another path: "
echo/
echo I read "%aPath%" and "%anotherPath%"
Then , the solution:
#if (#CodeSection == #Batch) #then
#echo off
echo Start the Batch file
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
rem Start the "application" in the same Window
start "" /B cmd /C application.bat
rem Wait and send the first ENTER
ping -n 2 -w 1 localhost > NUL
%SendKeys% "{ENTER}"
rem Wait and send the first path
ping -n 2 -w 1 localhost > NUL
%SendKeys% "C:\The\first\path{ENTER}"
rem Wait and send the second path
ping -n 2 -w 1 localhost > NUL
%SendKeys% "D:\A\Second\Path{ENTER}"
goto :EOF
#end
// JScript section
WScript.CreateObject("WScript.Shell").SendKeys(WScript.Arguments(0));
The output:
Start the Batch file
Hit ENTER to continue
Enter a path: C:\The\first\path
Enter another path: D:\A\Second\Path
I read "C:\The\first\path" and "D:\A\Second\Path"

Automatically open a browser and login to a site?

I have a 70 years old grandma that is not used to use the computer, but she has an e-mail and Facebook, and every time she wants to access those sites i have to help her through the process. So, i got an idea, i know a little of programing, so i tried to do a batch file to automatize this process of opening Firefox, typing "www.exemple.com", and login in her account. With this she could at least see if there are any emails or facebook notifications, but i could just do an batch to open the email site, i would like to know if there is any way to do a program to login.
Batch File:
ECHO OFF
START FIREFOX "WWW.EXAMPLE.COM" "WWW.EXAMPLE2.COM"
EXIT
Figured this out today, how one can use batch files on windows computers to push keyboard commands to websites or other applications if needed. See here for the original coding information.
The core of the command that you will find here simply results as
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
%SendKeys% "{ENTER}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
While i myself am still experimenting and testing this batch file program on different applications, i am not sure as to the inner-workings of what this program actually does. All i know is it uses a java script installed on every windows computer to push keyboard commands to be executed. However in my experimentation i found that it could also serve as a means to fill in passwords and usernames.
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
START FIREFOX "WWW.EXAMPLE.COM"
rem the script only works if the application in question is the active window. Set a timer to wait for it to load!
timeout /t 5
rem use the tab key to move the cursor to the login and password inputs. Most htmls interact nicely with the tab key being pressed to access quick links.
%SendKeys% "{TAB}"
rem now you can have it send the actual username/password to input box
%SendKeys% "{U}"
%SendKeys% "{s}"
%SendKeys% "{E}"
%SendKeys% "{r}"
%SendKeys% "{N}"
%SendKeys% "{a}"
%SendKeys% "{M}"
%SendKeys% "{e}"
%SendKeys% "{TAB}"
%SendKeys% "{P}"
%SendKeys% "{4}"
%SendKeys% "{S}"
%SendKeys% "{S}"
%SendKeys% "{W}"
%SendKeys% "{O}"
%SendKeys% "{R}"
%SendKeys% "{D}"
%SendKeys% "{ENTER}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
Thus logging your grandmother into the specific website. While this is a rough approximate of the code i would have attempted running, it should still work. If it doesn't, start from scratch with the link i provided. In addition, if you need ways to include those special characters check here as well.
*EDIT:
I later made something like this for myself and found a simpler way instead of typing out all those %SendKeys%. You can simply do
%SendKey% "Username{TAB}"
%SendKey% "Password{ENTER}"
as a more simpler way of going through the login process. The only downside to any of this is that if she decides to change her login, you would have to change her password from within the program.
This worked for me actually, I referred jouster500 answer, there were few bugs which I rectified, works like a charm.
#if (#CodeSection == #Batch) #then
#echo off
rem Use %SendKeys% to send keys to the keyboard buffer
set SendKeys=CScript //nologo //E:JScript "%~F0"
START CHROME "https://login.classy.org/"
rem the script only works if the application in question is the active window. Set a
timer to wait for it to load!
timeout /t 10
rem use the tab key to move the cursor to the login and password inputs. Most htmls
interact nicely with the tab key being pressed to access quick links.
rem %SendKeys% "{TAB}"
rem now you can have it send the actual username/password to input box
%SendKeys% "{TAB}"
%SendKeys% "{TAB}"
%SendKeys% "username"
%SendKeys% "{TAB}"
%SendKeys% "password"
%SendKeys% "{ENTER}"
goto :EOF
#end
// JScript section
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));

Resources