I would like to create a batch script to run an already made bat script, delay for a few seconds and then type some commands - windows

Need some help with this one (I'm new to batch scripting). So here's the problem:
I have a batch file called connection.bat:
- it connects to a network WiiU console, but it takes a few seconds to load the environment
- after it loads, it looks something like this:
#userid:
- that's when you can type some specific console commands (install and run mostly)
I want to make a batch file that does the following:
1. runs connection.bat
2. waits for it to finish loading the environment
3. and then type the install command
I tried doing it this way:
cd C:\test\connection.bat install -1 10.xx.xx.xx testupload.pkg
but it doesn't work, because it needs to connect first to the console
Is there a way to do this?

I always use ping to add a delay in batch files:
ping 0.0.0.0 > nul
adds a 3 second delay

Related

Sending a command in the windows command prompt that terminates after a certain time

I am using a batch script to create a short functional test for technicians at my work which would normally require them to send several commands back-to-back themselves.
There is a command that brings up an image on the test device, but the command hangs until you press ctr+c. When doing everything manual that's fine but I don't want the technicians to do that during the script because they might accidentally exit out of the whole thing.
Is there a way to make it so the script can stop that command and move on the next line in the script? Something like a timeout on the command? Or a key press that just stops the command but doesn't close the script?
Code sample:
echo "Booting Device..."
adb start device
timeout 40
adb shell load_image yosimite.png
timeout 5

Opening an ssh connection and keeping it open on startup

I need to open an ssh connection on login and keep it open, but to not acutally do anything with it. It would be best if all of it would run in the background.
I created an automator application and made it run a shell script on the bash. The script looks as follows:
sshpass -p 123456 ssh 123456#123.123.123.123
If i try to run the application i keep getting an error message, however if i execute the exact same script in an terminal it works just fine.
Is there any way i can open that connection with an automator application and keep in the background?
You can send a KeepAlive packet to stop the pipe from closing.
In your ~/.ssh/config, and the following:
Host *
ServerAliveInterval 300
ServerAliveCountMax 2
What this says is that that every 300 seconds, send a null (keep-alive) packet and give up after 2 tries.
Source: http://patrickmylund.com/blog/how-to-keep-alive-ssh-sessions/
Do you really need to involve Automator at all?
Just save the script (say, foo.sh) in a folder with the same name as the script (i.e. foo.sh as well).
Put this folder in /System/Library/StartupItems/ and it will run when you start up your machine.

Shutdown via batch file into infinite loop?

Problem:
While running the batch file, it goes into infinite loop
Code:
shutdown -s -t 050
Output:
Ran the batch file
Output when i run in normal batch file:
Output when i run in admin batch file:
Question:
Now I wonder why this happens, and want to know how to run shutdown command normally from batch file, if not like this ?
You called the batch file shutdown
Use a name that is not a system command or internal command.

In batch programing can one command run before the previous command finishes executing?

In batch programing is one command waited until completed until the next one is run? What I mean is for example
net stop wuauserv
net start wuauserv
Since net stop wuauserv takes a while to complete is it given time to complete or do I need another command to wait until it completes?
The NET STOP command does wait (or timeout while waiting) for a service to stop or start.
You can check the %ERRORCODE% from the command to get more information about if there was a problem or if it worked as expected.
In general most system command line tools return control once they are done executing. A few specialized programs will call into other services or systems and may return control before execution is complete. You will need to check the docs for whatever you are trying to run, but generally processes exit once the 'task' they perform is complete.
In a batch file, all commands are run sequentially, and execution waits for the command to complete.
In your example, net stop wuauserv would complete before net start wuauserv gets run.
You could confirm that by running something you know will take a long time, such as
ping www.google.com
ping www.stackoverflow.com
and you'll see that the second ping does not start until the first completes.
In your case, yes the second command will not execute until the first finishes.
However, GUI apps will start up and return control the batch file.
For example,
PING localhost
NOTEPAD
DIR
The DIR command will execute even if NOTEPAD is still running.

Running remotely Linux script from Windows and get execution result code

I have the current scenario to deal with:
I have to schedule the backup of my company's Linux-based server (under Suse Linux) with ARCServe R15 (installed on Windows 2003R2SP2).
I know I have the ability in my backup software (ARCServe) to add pre/post execution scripts to my backup-jobs.
If failure of the script, ARCServe would be specified NOT to run the backup-job, and if success, specified to be run. I have no problem with this.
The problem is, I want to make a windows script (to be launched by ARCServe) for executing a Linux script on the cluster:
- If this Linux-script fails, I want my windows-script to fail, so my backup job in ARCServe wouldn't run
- If the Linux-script success, I want my windows-script to end normally with error code 0, so my ARCServe job would run normally.
I've tried creating this batch file (let's call it HPC.bat):
echo ON
start /wait "C:\Program Files\PUTTY\plink.exe" -v -l root -i "C:\IST\admin\scripts\HPC\pri.ppk" [cluster_name] /appli/admin/backup_admin
exit %errorlevel%
If I manually launch this .bat by double-clicking on it, or launching it in a command prompt under Windows, it executes normally and then ends.
If I make it being launched by ARCServe, the script seems never to end.
My job stays in "waiting" status, it seems the execution code of the linux script isn't returned to my batch file, and this one doesn't close.
In my mind, what's happening is plink just opens the connection to the Linux, send the sript execution signal, and then close the connection, so the execution code can't be returned to the batch. Am I right ?
Is what I want to do possible or am I trying something impossible to do ?
So, do I have to proceed differently ?
Do I have to use PUTTY or CygWin instead of plink ?
Please, it's giving me headaches ...
If you install Cygwin, you could do it exactly like you can do it on Linux to Linux, i.e. remotely run a command with ssh someuser#remoteserver.com somecommand
This command will return with the same return code on the calling client, as the command exited with on the remote end. If you use SSH shared keys for authentication instead of passwords, it can also be scripted without user interaction.

Resources