Batch file to open server with port - batch-file

I am trying to write a script to connect to a server on a specific port. I can get the script to open the browser with the server address as long as I don't include the port. If I try to include the port then I get an error saying:
There is no program associated to perform the requested action...
When I exclude the port it attempts to connect to the server in the browser, but this won't work without a port. Here's some of my code:
#ECHO off
SET serv=exampleserver.company.com
ECHO Server address is %serv%
SET /p port=Enter the port number
ECHO The port is %port%
SET addr=www.%serv%
ECHO Your full address is %addr%:%port%
START %addr%:%port%
PAUSE
EXIT

as you can test manually, while start www.stackoverflow.com will work, start stackoverflow.com won't, and neither start www.stackoverflow.com:80. I suspect that the recognition of "www." is a especially encoded exception.
To resolve this, use start http://<server>:<port>.
as an aside, if you surround your start parameter with quotes (required if it has a space or other separator), you need to put (empty) quotes before, like start "" "c:\program files\bla", since the first quoted argument will be interpreted as a window title.
xyz:abc is actually a valid filename under windows, where the second part names an alternate data stream (ADS).

Related

Batch stops after setting variables

I'm new to batch scripting and just tried to do some simple stuff and trigger some other applications. At first my batch set some variables and then it should save the results from a command to a file. Both parts alone work fine, but if I put them together it stops after setting the variables. Can someone help me?
Code:
set path=%cd%
set filename=\MAC.txt
set filepath=%path%%filename%
arp -a |find "192.168.1.1">>%filepath%
pause
Since path is the variable used by cmd for finding executables, changing it to something where arp isn't will mean that you won't be able to locate and run arp.
Use a different variable name, such as:
set filepath=%cd%
set filename=\MAC.txt
set fullname=%filepath%%filename%
arp -a |find "192.168.1.1">>%fullname%
pause
Note that this still has another problem present in your original script, the fact that spaces in your current directory may cause issues (unless you quote them correctly).
Since you're using the current directory exclusively, you probably don't need to worry about the path:
set fullname=.\MAC.txt
arp -a |find "192.168.1.1">>%fullname%
pause

WMIC: Run Batch Script Remotely

I've been trying to get a Jenkins deploy job to work by running a batch script to do the install of an msi from the Jenkins build machine itself. I've given the appropriate access rights, but still am not able to run the following command remotely, using WMIC
wmic /node:myServerIp /user:"clientpc\my-user" /password:"my-password" process call create "D:\someDir\someOtherDir\test.bat"
The follow response from the above command:
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 9;
};
After some research, it looks like return value of '9' is 'Path not found' according to https://msdn.microsoft.com/en-us/library/aa389388(v=vs.85).aspx, but I've verified that the path exists on the remote server.
The test.bat file that I'm trying to run is very simple, and should just write to a text file.
#echo This is a test.> test.txt
I've verified that both files exist on the server, and have granted 'EVERYONE' to the shared folder 'someDir'.
I have tried prefixing 'cmd.exe /c' to the path called:
wmic /node:myServerIp /user:"clientpc\my-user" /password:"my-password" process call create "cmd.exe /c D:\someDir\someOtherDir\test.bat"
...for which I receive:
Invalid Verb Switch.
I've verified that the user access is correct by providing a bad password, in which case permission is denied.
EDIT:
Changed the path from D:\someDir\someOtherDir\test.bat to D:\\someDir\\someOtherDir\\test.bat but now receive the following error:
ERROR:
Description = The RPC server is unavailable.
EDIT 2:
Looks like the RPC user I was using was the cause for the error. Still troubleshooting, but when I use my AD user, as opposed to the administrator I created to run this, I get the following AGAIN...
Executing (Win32_Process)->Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 9;
};
I was able to get the following to work on an Active Directory domain.
Wmic /node:"ComputerName" process call create "cmd.exe /c (net use o: /delete /y & net use o: \\Server\share /user:Domain\Administrator Password & o:\Dir\Subdir\test.cmd) >> c:\users\MyUser\testout2.txt"
The very simple contents of test.cmd:
echo Just a test >> c:\users\MyUser\testout.txt
date /t >> c:\users\MyUser\testout.txt
time /t >> c:\users\MyUser\testout.txt
The "job" is being sent to "ComputerName" on the domain. The batch/script file the job runs is on a network share. The job running on "ComputerName" will not see any mapped drives, so I delete and map a drive. I don't believe it is ever necessary to delete the drive, but I added that for completeness sake.
After execution, testout2.txt shows the batch file executing the commands and
testout.txt contains the results of the batch file commands as expected.
Things to watch out for:
As mentioned, mapped drives are not visible from the remote job
You are executing in the target machine's environment - drive letters need to make sense to that machine
Internal commands such as 'echo' require the job starts with 'CMD.EXE /c'
Group multiple commands inside parentheses and separate with ampersands (&)
Don't collide file access. I use testout.txt and testout2.txt files. If I had given them the same name, one set of outputs would have been lost.
Nothing you do this way will ever be visible to the user; the job is run in such a way that it can not display on the user's screen.
Sending a password in clear text as I show in the example is a security hazard and should be avoided. I'm not sure of a better way to map drives in this context however.

How to run netsh command through a batch file

I want to run the following commands through a batch file.
netsh
wlan
connect name=NETWORK-NAME
The issue is that 'netsh' and 'wlan' are unable to be done in the same line using conventional delimiters (&, &&). If I run this string as a batch file:
echo 1 & netsh & echo 2 & wlan & echo 3 & connect name=NETWORK-NAME
It outputs 1, activates netsh and stops there without executing the rest of the batch file.
I know this is an older post, but info on the internet is forever.. so..
That actually won't work. NETSH is, itself, a shell like DOS is (now). You can execute that on a command line directly, but not in a batch file. Using NETSH in Batch mode is a little more complex and, in this instance, would require 2 batch files. One to launch NETSH, and another for NETSH to execute after launch.
Here is a good post on how it should be done:
https://www.techrepublic.com/article/build-your-skills-netsh-scripts-add-network-functionality-to-batch-files/
Hope this helps someone who stumbles across this.
netsh is a program that can run either in interactive mode when you enter the program and you enter command modifiers and switch contexts one by one per line or just run the required operation in the required contect immediatelly.
So you just need to make a single string:
#echo off
netsh wlan connect name=profile_name ssid=SSID_name interface="Wirelss network adapter"
Note that ssid and interface are optional. You need to specify interface if you have several wirelss network adapters in your system - it corresponds to the adapter name. And you need to specify ssid if there are multiple SSIDs in your saved wireless network profile.

BAT file programming assistance : how to make a response in a bat file copy to another part OR to clipboard?

My apologies if the title was confusing. It should be fairly basic, but I cannot find a way to do this.
What i'm wanting to do is have a bat file prompt for an answer to a question and then surround that answer with another piece of code and either copy to the clipboard or after the connection.
Maybe it will make more sense if i give the code.
#echo off
set /p input ="what server would you like to connect to? (example srv02) :"
echo Myhome.%input%.com
pause
c:\program files\putty
the echo gives the correct response, but i would like to see if there is a way to paste this past the c:\program files\putty to connect to a server.
OR if there is a way to copy that response to the clipboard so the bat file would open putty (which it does now) and then you could just paste that response.
Or am i going about this the wrong way? thanks for the help!!
I'm not a PuTTY user, so I can't be sure - But I believe you are simply trying to pass the the server host that you want to connect to. I think the following will do what you asked:
"c:\program files\putty" %input%
or
"c:\program files\putty" Myhome.%input%.com
However, there are probably other command line options you need to really accomplish your goal.
You should learn the PuTTY command line arguments and options. I suspect a web search could find some tutorials or examples to get you started.
Perfect! That is what i ended up doing after a few more hours of research.
I came up with two different types of scripts as my company wants us to use a specific set of defaults for different servers.
#echo off
set /p input="what server would you like to connect to? : "
echo myhome.%input%.com| clip
echo ----
echo myhome.%input%.com has been copied to your clipboard,
echo you can now paste into the host name box in putty
echo -----
echo Press enter to open puTTY
echo -----
Pause
start c:\users\public\apps\putty.exe
Ended setting it to open putty instead of pasting because it should be defaulted settings, but not everyone kept it that way, of course.
Thanks for the assistance though!

GnuWin32 openssl s_client conn to WebSphere MQ server not closing at EOF, hangs

I am trying to use the GnuWin32 version of OpenSSL to fetch certificates from several WebSphere MQ queue managers. All attempts result in OpenSSL keeping the connection open until I manually hit ENTER.
I tried piping various text into the command as per this SO question and this SO question, but with no luck.
Ideally, OpenSSL would close the connection once the input file or piped text hit EOF.
Examples:
echo QUIT | openssl s_client -connect qmgrhost:1414 -showcerts
openssl s_client -connect qmgrhost:1414 -showcerts < responsefile.txt
The examples using the QUIT command appear to work in the other SO question because they are hitting an HTTP server. Since this is WMQ in my case, the connection handshake is different and sending QUIT (or anything else I've tried) doesn't get it to close the connection. I could probably feed it a giant file until the QMgr choked and killed the connection but I'm trying to be as polite as possible to the server. The current method of supplying an ENTER from the keyboard is causing FDC dumps on each connection as it is, setting off all kinds of alarms and making the Tivoli guys mad at me.
So best case would be a string or hex value the QMgr interprets as a graceful rejection and closes the connection. Second best case is any method that allows this to be scripted and we'll accept the FDC files as a cost of automating this function.
UPDATE: 31 May 2013
I've since moved to AIX where this works perfectly. The s_client hangs up immediately after making the connection when run in a script and piping a newline into it. However, I'd still like to have a solution for Windows. Does anyone know if the Cygwin version works or has the same problem? Is it Windows signalling/POSIX issue? Code bug?
It seems that this problem is related to a synchronization issue between the Batch file and the openssl.exe program. I need your collaboration in order to do some tests and report the result. Below is a Batch-JScript hybrid script for the first test. The JScript section have two parts; the first one WScript.Stdout.WriteLine("QUIT"); is entirely equivalent to echo QUIT Batch command. The second part (with two lines) is similar, but it load the keyboard buffer with "QUIT" and an Enter key instead of send the string via STDOUT.
I need you to make a test with both sections of the JScript code and report the result (the double-slash // mark the rest of the line as comment). If we are lucky, the openssl.exe program will end with the Sendkeys method; if not, try to send a "QUIT" string via STDOUT and type just an Enter key with Sendkeys. If the openssl.exe program terminate before returning the desired information then the problem is almost solved, because in this case we can synchronize the sending of the Enter key until the desired information had been received from openssl.exe.
Save the following as a .bat file. Try it, then comment out the Wscript line, uncomment the WshShell lines, and then try it again.
#if (#CodeSection == #Batch) #then
:: The first line above is...
:: in Batch: a valid IF command that does nothing.
:: in JScript: a conditional compilation IF statemente that is false,
:: so this section is omitted until next "at-sign end".
#echo off
CScript //nologo //E:JScript "%~F0" | openssl s_client -connect qmgrhost:1414 -showcerts
goto :EOF
#end
// JScript section
WScript.Stdout.WriteLine("QUIT");
// var WshShell = WScript.CreateObject("WScript.Shell");
// WshShell.SendKeys("QUIT{ENTER}");
You may also try with String.fromCharCode(26) to generate a Ctrl-Z (EOF) character, both in WriteLine or Sendkeys methods; for example:
WshShell.SendKeys("QUIT{ENTER}" + String.fromCharCode(26));

Resources