insert command into start cmd - batch-file

I am trying to insert the content of a text file into a cmd
console, by using:
start cmd.exe < c:\text.txt
I also tried:
start cmd.exe | c:\text.txt
However, the both open the cmd shell but nothing gets passed.
My point being in the end that I have a scheduler jenkins, and I pass the content of the text file inside the console when opening it with the start command. So I am not simply trying to print to the cmd console; I could just use echo for that different case.

Something like this?
from CMD.exe itself,
type C:\Text.txt
If it is a batch file, then
type C:\Text.txt
pause
or to just see the content, use more
more C:\Text.txt
If you want to actually run commands from the file instead of trying to insert the commands from the text file into the cmd console, you should rather build it as a batch file which is an executable. you do this by renaming the file to either .bat or .cmd
you then insert you commands into the file and execute it by either double clicking the file or running it from a scheduler etc. Here is an example of a batch or cmd file:
echo Please wait while I execute.
tp merge $/ServerFolderA $/ServerFolderB
So just a few explanations on your initial commands. When you ran:
start cmd.exe | c:\text.txt
you actually told the system to run multiple executables from a single command. The pipe commands is like separator to specify each command. so this:
ping 127.0.0.1 | nslookup www.google.com | cmd.exe | c:\text.txt
will actually do all of those commands in sequence, first it will ping, the do nslookup, open cmd.exe then open c:\text.txt
Here you were on the right track, but my guess is you had a single line in the file and not a new line.
start cmd.exe < Text.txt
That will use the Text.txt file as an answer file so if I edit it and insert the following:
echo This is an answer file
ping 127.0.0.1
ping 10.132.4.99
echo Completed all commands
and then run start cmd.exe < Text.txt it will execute everything in sequence. This difference here is it reads the file line by line and displays each commands it runs. so your output will be something like this:
C:\>echo this is an answer file
this is an answer file
ping 127.0.0.1
Pinging 127.0.0.1 with 32 bytes of data:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Ping statistics for 127.0.0.1:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
ping 10.132.4.99
Pinging 10.132.4.99 with 32 bytes of data:
Reply from 10.132.4.99: bytes=32 time=3ms TTL=254
Ping statistics for 10.132.4.99:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 3ms, Maximum = 8ms, Average = 4ms
As you can see this works perfectly, but it displays each command you are running except, it does not run the very last command which is echo Completed all commands. So to run all commands you always have to add a new line after your last commands. When however you rename it to .cmd it will just run the commands without displaying the commands to run and runs each line until the end. The other issue with the answer file is it reads line by line, so having 3 new lines with no text in the answer file will result in something like this
C:\>
C:\>
C:\>
So having just this in the answer file:
ping 127.0.0.1
will not work as it is a single line without the enter section.
but by just adding a new line after it will make it work.
I hope all this makes some sense.

Related

How to force Windows Comand Prompt to flush text output to a file line-by-line

I'm trying to log every ping in my Windows CE5.0 machine Command Prompt using
> ping 192.168.1.1 -t -l 60000 >> ping.txt
The file starts with a single line of output and then only flushes after pressing 'ctrl+c'.
I was wondering if there was a way to force it to print in every new line.
I do not know any direct way to do this.
But you could work around that by doing single ping requests in a (infinite) loop and write to the log file in the loop, like this:
for /L %I in () do #(timeout 1 > nul & ping 192.168.1.1 -n 1 -l 60000 | find "TTL=" >> "ping.txt")
The timeout 1 command establishes a one-second delay in every loop iteration in order to avoid heavy CPU load, > nul suppresses its console output.
The find command is used to filter for lines containing TTL from positive replies (like Reply from 192.168.1.1: bytes=60000 time<1ms TTL=128). If you want, you can change that to findstr /B /C:"Reply from " /C:"Request " /C:"Ping request ", for example, to capture positive replies as well as negative ones like Request timed out. or Ping request could not find host ..., or you can remove it completely (also the |) to write the whole ping response to the file, including header and footer.

Save Ping Output to Text File

I am using this batch command to save ping output to a text file,
ping 10.226.2.10 -n 10 >>ping_ip.txt
But when i saved the above command in a batch file and trying to run it, in command prompt window my command gets converted to the below command...
ping 10.226.2.10 -n 10 1>>ping_ip.txt
you can see there is extra 1 in 1>> in the second command, i don't know how it came.. somebody please give your valuable opinion on the same
This is just the normal behaviour.
In batch files you have some input/output streams:
0 = standard input stream
1 = standard output stream
2 = standard error stream
3-9 = user defined streams
Your >> operator implicitly redirects the standard output stream, that is, it is redirecting the stream number 1, and the cmd parser converts the command
command >> output
into
command 1>> output
showing the explicit command executed based in an implicit request
Powershell with timestamp and appended to file.
ping.exe -t example.com | Foreach{"{0} - {1}" -f (Get-Date),$_} | out-file .\ping.txt -append

PuTTY command line automate serial commands from file

I am trying to connect to a serial port and send a series of commands from a file.
Firstly I have mananged to connect via the following:
PuTTY.exe -serial COM3 -sercfg 57600,8,n,1,N
E.g. I have a file called commands.txt with a series of serial commands I wished to be sent.
I tried the following however it failed to work:
PuTTY.exe -serial COM3 -sercfg 57600,8,n,1,N -m commands.txt
Any help is greatly appreciated.
Try like this :
for /f "delims=" %%a in ('type commands.txt') do PuTTY.exe -serial COM3 -sercfg 57600,8,n,1,N -m %%a
Another solution which I have used to regularly send commands to a device uses a combination of PuTTY and Autohotkey.
For the initial setup, configure a PuTTY session and save it. In my case I named is Oasis.
The following Autohotkey function can send a command to the already open PuTTY session. If PuTTY is not open it will start the saved session. oasis_putty_name() is the name of the PuTTY window once it's open, it will depend on the COM port selected. location_putty() is the location of the PuTTY executable. Both of these can be hard coded but I wanted to keep the variables separate from the functions.
; Oasis Check --------------------------------------------------
oasis_check(){
putty_name := oasis_putty_name()
; Start PuTTY if it's not already running
IfWinNotExist, %putty_name%
{
putty := location_putty()
Run %putty% -load Oasis
Sleep,1000
}
; Format Time Stamp
FormatTime, TimeString,,yyyy-MM-dd HH-mm-ss
; Record Oasis Values
ControlSend, , %TimeString%{ENTER}, %putty_name%
Sleep, 2000
ControlSend, , all?{ENTER}, %putty_name%
}
The frequency of execution can be controlled with another Autohotkey script or, as in my case, with the Windows Task Manager.

CMD - Ping and traceroute from list of ips

We have sporadic connection failures when webserver tries to connect to service on the net.
There is a problem to trace failure from PHP for many reasons.
I'm a web-programmer and not familiar with command-line scripts. Can anyone help with following cmd-script:
-there is a list of ips separated by newline in text file (ip_list.txt)
-take ip from list and ping it, if it fails on first attempt - traceroute it
-go to next ip in file
I don't really sure what you want to test, but here's a pretty useful command to test ping. Enter ping IP_ADDRESS -l 10 -n 10 directly to cmd, change the IP_ADDRESS to ip address you want. -l 10 - Ping you ip address with 10 bytes of data
-n 10 - Ping for 10 times
ping /? - For more informations
To get each ip address (line by line) in a text file, use for /f %%a in (YOUR_FILE.txt) do ( //to do ). Since I'm not sure about what you want, so that's all I can help with :)

Error passing multiple commands to Cisco CLI via plink

I've gotten some help with an earlier part of this batch file, but now I'm having trouble with the final component.
I've tried a few things with no success. I tried changing the CRLF to LF which did nothing. I also tried rephrasing the commands a few ways but I am still not getting anywhere. The following is my main batch file.
#echo on
REM delete deauth command file
SET OutFile="C:\temp\Out2.txt"
IF EXIST "%OutFile%" DEL "%OutFile%"
plink -v -ssh *#x.x.x.x -pw PW -m "c:\temp\WirelessDump.txt" > "C:\temp\output.txt"
setlocal
for /f %%a in (C:\temp\output.txt) do >> "Out2.txt" echo wir cli mac-address %%a deauth forced
REM Use commands in out2 to deauth
plink -v -ssh *#x.x.x.x -pw PW -m "c:\temp\Out2.txt"
pause
Below this sentence is the command found in Out2 which I think is giving the actual trouble. The number of lines varies but they are all this particular command just with differing MACs.
wir cli mac-address xxxx.xxxx.xxxx deauth forced
If Out2 has only a single line it runs fine, no issues. But when there are multiple lines, it fails with an error stating that the Line has an invalid autocommand. It's almost as if it was reading it as one contiguous command. As I mentioned above I changed from CRLF to LF hoping IOS would like it better, but that failed. I've tried adding extra lines between the commands, and I've tried calling the login every time from that file.
I am hoping that there is a way to tailor the commands to pass all lines one at a time to keep this down to a minimum of files.
I had another thought but it is kinda/very clunky. If there was a way to output each of those MAC deauth commands to their own file in a saperate folder (out1, out2, out3), and have the BAT able to run all the randomly generated files in that folder so that each one is a separated plink session.
Let me know if I need to change/add/elaborate on anything. Thanks in advance for anything you guys are willing to help with. I appreciate it.
EDIT: Martin has pointed out what the limitation actually is. It appears to be a limitation on Cisco to accept blocks of commands through SSH. So I still have the same question really, I just need some help figuring a workaround to this issue. I'm thinking the multiple file solution I mentioned above may have some possibility. But I'm too much of a noob to know how to make that work. I'll update if I have any breakthroughs though. Thanks for any contributions!
It's actually a known limitation of Cisco, that it does not support multiple commands in an SSH "exec" channel command.
Quoting section 3.8.3.6 -m: read a remote command or script from a file of PuTTY/Plink manual:
With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.
Though you can probably still feed multiple commands to Plink input:
(
echo command 1
echo command 2
echo command 3
echo exit
) | plink -v -ssh user#host -pw password > output.txt
Or you can simply use an input file:
plink -v -ssh user#host -pw password < input.txt > output.txt
Similar question: A way of typing multiple commands in cmd.txt file using PuTTY batch against Cisco
This works without cmd.exe and using files:
function Invoke-PlinkCommandsIOS {
param (
[Parameter(Mandatory=$true)][string] $Host,
[Parameter(Mandatory=$true)][System.Management.Automation.PSCredential] $Credential,
[Parameter(Mandatory=$true)][string] $Commands,
[Switch] $ConnectOnceToAcceptHostKey = $false
)
$PlinkPath="$PSScriptRoot\plink.exe"
$commands | & "$PSScriptRoot\plink.exe" -ssh -2 -l $Credential.GetNetworkCredential().username -pw "$($Credential.GetNetworkCredential().password)" $Host -batch
}
Usage: dont forget your exit's and terminal length 0 or it will hang
PS C:\> $Command = "terminal lenght 0
>> show running-config
>> exit
>> "
>>
PS C:\> Invoke-PlinkCommandsIOS -Host ace-dc1 -Credential $cred -Commands $Command
....
Sounds like your file 'Out2.txt' has only LF at end of line. Simple way to convert that to CRLF is to use MORE command and redirect output to a new file and then use the new file.
more Out2.txt > Out2CRLF.txt
I ran into the same issue when trying to pull the full list of ACLs on an ASA via plink in powershell.
Essentially, due to the abuse issue referenced in the documentation: https://the.earth.li/~sgtatham/putty/0.72/htmldoc/Chapter3.html#using-cmdline-m, I was getting inconsistent results in pulling the ACLs. Sometimes I would get 0, sometimes only 1 or 2, and sometimes I would get all of them. (I personally, had about a 1 in 5 success rate).
As I would occasionally be successful I used a while loop that would catch the unsuccessful attempts and retry. Just be sure to put some timing on the while loop to prevent it from spamming ssh connections too much.
It is not a good solution, but it worked as a last resort.

Resources