Plink output log coded name? - windows

First of all, sorry if the question doesn't make sense, I'm not the best at English. My question is, I'm trying to save Plink session into a log, that part is now done:
Plink.exe -v -pw [PW] -batch -m C:\BATS\comandosx3.txt root#[IP] > C:\BATS\Logs\outputdegradado.log 2>&1
Now my question is, can I code the output.log name to be stored with date and time so everytime the program starts doesn't write over the last session? for example output2009.log

I didnt know it was related to the batch file, i thought it was plink specific code. Watching for batch specific code, i end up with a single file that registry the date and time at the start of the program.
#echo off
set logfile=C:\BATS\Degradado\Logs\Seguimiento.log
echo starting Plink.exe at %date% %time% >> %logfile%
Plink.exe -v -pw [PW] -batch -m C:\BATS\Degradado\comandosx3.txt root#[IP] oas >> %logfile% 2>&1
Thanks #martin-prikryl for the suggestion

Related

Piping output of process to file asynchronously in windows

I am trying to run a program using "start" and I want to have the output piped to a txt file, for example, I want to have the output of a simple http python server to be logged to a text file:
START cmd /c python -m SimpleHTTPServer 8000^>engineLog.txt
The above will print it to the log file but the problem with that is, it only prints it when the process finishes. Are there any way that I can store it to the log as it is running? Also is it possible to have it log in the txt file as well as having it print out in the console?
Give it a try:
START /b "Your title" "cmd /c python -m SimpleHTTPServer 8000 > engineLog.log | type engineLog.log"
Alternatively, you can get tee port for Windows.

Passing inputs to program prompt in a batch file

I am running simulations in parallel using mpich2. I've got rather stringent security on my workstation, and must register using a new password each time I run a simulation. I have to enter:
mpiexec -register
which then prompt me for a username, and then prompt me for a password. Unfortunately, there seem to be no way to pass the user/pass to mpiexec on a single line, e.g.
mpiexec -register user:pass
does not work.
I'm trying to prepare a batch file that can automatically pass the username and password to the mpiexec prompts, but I cannot seem to get it to work. I've tried various things like timeout /t 5 but that doesn't work.
Can anyone tell me how to pass these inputs to the mpiexec program prompts in a batch file?
Thanks!
EDIT: I think I am getting closer. I've tried
(
echo username
echo password
echo password
) | mpiexec -register
which appears to be passing the username and password inputs to the mpiexec prompts. Program is still hanging at the next step however - not sure if that's a problem with the way I'm passing these or not.
You could redirect or pipe into mpiexec.
With redirection it's gets a bit nasty for user/password entries, as there are often unwanted (and unvisible) spaces at the line ends.
(
echo user
echo pwd
) | more > fetch.txt
Creates in fetch.txt
user<space>
pwd<space>
When you want to suppress the spaces use a file redirection instead
(
echo user
echo pwd
) > file.tmp
< file.tmp mpiexec -register
In both cases (redirection or pipe), you need to serve all inputs for the program, not only username and password.
You can't enter inputs from keyboard anymore.

How to combine batch file and expect script in cygwin?

i have an application on windows that requires user name and password to start, but i don't want user to enter the user/password, so i created a script to interact with CMD interface to enter the password, using cygwin on windows 8.1, but i can't find expect command on the Cygwin, and i would like to ask also if my script has any issue.
below is the script
#!/usr/bin/expect
sh C:\Users\Osama.Barakat\Desktop\batchfile.bat
expect "Enter the password for administrator:"
send "Pa$$w0rd"
interact
I identified two issues in your question:
How to perform batch files in cygwin/bash?
How to get expect installed/running in cygwin/bash?
1. How to perform batch files in cygwin/bash?
In your sample code, I found
---8<---
sh C:\Users\Osama.Barakat\Desktop\batchfile.bat
--->8---
IMHO, this is a mistake. I believe that bash will try to execute anything as script regardless of the script file extension. But if batchfile.bat is really a batch file then it should be called with the correct interpreter (i.e. cmd.exe) instead.
I tried it to see if there is any issue in the cygwin bash but it worked. My sample batch file test-expect.bat:
#echo off
rem output
echo Hello %USERNAME%
rem input
set /p INPUT= Enter input:
rem output
echo Input: %INPUT%
...tested in bash on cygwin:
$ cmd /c test-expect.bat
Hello Scheff
Enter input: Hello cmd
Input: Hello cmd
$
OK, done.
2. How to get expect installed/running in cygwin/bash?
To install expect on cygwin I googled a little bit and found Installation of Cygwin, expect and Ssh. Then, I started the cygwin-setup setup-x86.exe and tried something else:
On the "Select Packages" page, I set View to Full and Search to expect. I got a list of four entries only where the first was: "expect: Tool for automating interactive applications" (Category: Tcl). I choosed this for installation and had to confirm another package as dependency.
After installation, I tried this in an interactive bash:
$ which expect
/usr/bin/expect
$
OK - looks quite good.
Combining CMD.EXE and expect
To put all together, I wrote another test script test-expect.exp:
#!/usr/bin/expect
spawn cmd /c test-expect.bat
expect "Enter input: "
send "Hello expect\r"
interact
Tested in bash on cygwin:
$ ./test-expect.exp
spawn cmd /c test-expect.bat
Hello Scheff
Enter input: Hello expect
Input: Hello expect
$
Well, it seems to work.

How to open a .txt file after using netstat command with Batch

I am trying to open a simple log.txt file (in this example comandos.txt) after running a netstat command like so:
# echo off
echo. >> C:\comandos.txt
netstat -b -o 1 >> C:\comandos.txt
start C:\comandos.txt
After netstat Prompt Windows won't close and comandos.txt won't open.
Any clues on how to solve this?
# echo off
echo. >> C:\comandos.txt
netstat -b -o >> C:\comandos.txt
start C:\comandos.txt
The above snippet works fine. Note that you were specifying the interval in netstat command which redisplays the statistics again and again. Also, because you have echo turned off and display redirected to the file, the empty prompt window showing up for a long time would send wrong signals. Show some message like Collecting information... or similar.
Also, given that the command needs to resolve addresses and depends on the number of processes with network connection, it may take some time for netstat to complete which would be system dependent.

plink truncating commands

I'm using plink.exe on WinXP to run some commands on Z/OS BASH. My commands are interspersed with echo commands so that I can parse the output and work out what is where. The first dozen or so commands run fine, but then one of them gets truncated.
For example:
echo :end_logdetail:
echo Job Name : TfmMigration
echo :jobinfo:
What happens:
user#host:/dev> echo :end_logdetail:
:end_logdetail:
user#host:/dev> echo Job Name : Tf
Job Name : Tf
user#host:/dev> echo :jobinfo:
:jobinfo:
I just checked where in the input file the error occurs, and it's exactly 4444 bytes in, on line 116 (so it's done 115 successful commands before it goes wrong). The command I'm using is:
Code:
plink -batch -pw xxxx user#host < "c:\dev\telnetcmd.txt" > "c:\dev\telnetout.txt"
The telnetcmd.txt is just a DOS text file with an "exit" command at the end.
Any idea why one of my commands is being truncated in this way?
Update: I don't get the problem if I pass the command file to plink with -m, only when I feed it in with the < operator.
As shellter points out, I should have been using the -m option. This does mean that (unlike the telnet solution that I was using) my commands do not show up in the output, and neither do the shell prompts, but I can manage without those.

Resources