Both display and save Plink output - windows

I'm logging into to a remote ssh session using plink.exe to perform certain tasks using a batch script. Getting the output of these commands in a log file as well on the screen is very important for me.
I tried using usual batch way i.e. plink servername -m cmd.txt>logfile.log way but the problem with this is that it won't display it on the Windows terminal that the batch script is running on.
Then I found the -sshlog option of Plink. This does the work, i.e. I can get the output but on screen and in a log file, but this results in output as follows:
00000f90 56 4c 41 4e 2a 2a 0d 0a 20 65 6e 63 61 70 73 75 VLAN**.. encapsu
00000fa0 6c 61 74 69 6f 6e 20 64 6f 74 31 51 20 34 30 34 lation dot1Q 404
00000fb0 0d 0a 20 69 70 20 61 64 64 72 65 73 73 20 31 30 .. ip address 10
00000fc0 2e 37 31 2e 31 39 31 2e 31 34 35 20 32 35 35 2e .71.191.145 255.
My actual output starts at "VLAN**.. encapsu" in the text above The output has these "00000010 74 65 72 ... "bla bla characters which I do not want. Plus the main output (that would be displayed if i was using Plink interactively is "word-wrapped" and looks horrible which makes it very difficult to understand for a general user
Is there any way to prevent Plink output unwanted 'sshlog' characters in the log file? or Is there any other way to get the output on screen and log fail simultaneously in a Plink/PuTTY session inside a batch script?
I tried both -sshlog and -sshrawlog but same output. Also tried -sessionlog as per the documentation but it does not work!
I tried also > file.txt but it gave an empty file!
Expected results:
encapsulation dot1Q 404
ip address 10.71.191.145

There's no command-line switch in Plink to log only the readable output.
You would have to configure "Printable output" logging in Windows Registry prior to running Plink.
But there are other options.
See Displaying Windows command prompt output and redirecting it to a file.
So this should do what you need:
powershell "plink -batch servername -m cmd.txt | tee logfile.log"
(the tee is an alias to Tee-Object cmdlet)

Related

invalid characters not visible in BASH

I have been working on some device that allowed login via telnet and I extracted some data from devices and made some reports, without any problems. recently, I had to switch to SSH while rest of the script is all the same, only login procedure has been changed from telnet to SSH. after switching to SSH, I am facing some problem with the data extracted that there are some invalid characters in some of the lines, below is an example: as can be seen, there is an invalid character after PON7 in the line:
OLT:LT6.PON7.ONT1,ALARM,Date time,
problem is that this invalid character is not even visible in the bash/csv file, but it was discovered when I copied the line in notepad++ or while posting it here.
now I have two problems:
1st: if someone knows what is causing these invalid characters while switching between telnet/ssh.
2nd: how to deal with this invalid character in BASH as it is not even visible in BASH, but this report is being used somewhere and these invalid characters are causing problems.
Edit:
Pasting the text into a text-to-hex converter produces this:
4f 4c 54 3a 4c 54 36 2e 50 4f 4e 37 11 2e 4f 4e 54 31 2c 41 4c 41 52 4d 2c 44 61 74 65 20 74 69 6d 65 2c
It looks like there's a DC1 character (hex 11) between the "7" and the ".".
Unfortunately, this edit also has the side effect of removing the character from the sample text.
Passing your text through a text to hexadecimal converter shows that the invisible character is an ASCII DC1 character (hex 11, octal 021). This character is also known as Ctrl-Q or XON. It's sometimes used in flow control.
In a bash script, you could filter it out using the tr program:
echo $badtext | tr -d '\021'
SSH doesn't inherently insert DC1 characters into text streams. If you're getting a DC1 character in the output from a device, presumably the device sent that character.

Logging in readable Plink output (sshlog is too clumsy) [duplicate]

I'm logging into to a remote ssh session using plink.exe to perform certain tasks using a batch script. Getting the output of these commands in a log file as well on the screen is very important for me.
I tried using usual batch way i.e. plink servername -m cmd.txt>logfile.log way but the problem with this is that it won't display it on the Windows terminal that the batch script is running on.
Then I found the -sshlog option of Plink. This does the work, i.e. I can get the output but on screen and in a log file, but this results in output as follows:
00000f90 56 4c 41 4e 2a 2a 0d 0a 20 65 6e 63 61 70 73 75 VLAN**.. encapsu
00000fa0 6c 61 74 69 6f 6e 20 64 6f 74 31 51 20 34 30 34 lation dot1Q 404
00000fb0 0d 0a 20 69 70 20 61 64 64 72 65 73 73 20 31 30 .. ip address 10
00000fc0 2e 37 31 2e 31 39 31 2e 31 34 35 20 32 35 35 2e .71.191.145 255.
My actual output starts at "VLAN**.. encapsu" in the text above The output has these "00000010 74 65 72 ... "bla bla characters which I do not want. Plus the main output (that would be displayed if i was using Plink interactively is "word-wrapped" and looks horrible which makes it very difficult to understand for a general user
Is there any way to prevent Plink output unwanted 'sshlog' characters in the log file? or Is there any other way to get the output on screen and log fail simultaneously in a Plink/PuTTY session inside a batch script?
I tried both -sshlog and -sshrawlog but same output. Also tried -sessionlog as per the documentation but it does not work!
I tried also > file.txt but it gave an empty file!
Expected results:
encapsulation dot1Q 404
ip address 10.71.191.145
There's no command-line switch in Plink to log only the readable output.
You would have to configure "Printable output" logging in Windows Registry prior to running Plink.
But there are other options.
See Displaying Windows command prompt output and redirecting it to a file.
So this should do what you need:
powershell "plink -batch servername -m cmd.txt | tee logfile.log"
(the tee is an alias to Tee-Object cmdlet)

Firefox breaks pdf after download

Our web application provides ability to download pdf.
When user clicks on download link we open the pdf in a new tab.
My firefox uses pdfjs as a pdf viewer and I can save pdf through it's interface.
Everything was fine in Firefox 19, but version 24 download file which looks like corrupted (it displays that file, but can't download it correctly).
I noticed that result size of file is a nearest power of 2, for example if my original pdf size is 97kb then after downloading it through Firefox's pdfjs its size becomes 128kb and my desktop pdf viewers (like acrobat) can't open it.
I tested it on the same version of our app.
update
Demo pdf file - everything is fine with downloading through linux google chrome viewer and linux firefox 21 (pdfjs), but the same problem with linux firefox 23.0.1
Is something wrong with pdfjs or with our server?
update #2
I looked at binary contents of broken and not-broken file:
$ git diff not-broken.dump broken.dump
diff --git a/not-broken.dump b/broken.dump
index 3621089..5de337c 100644
--- a/not-broken.dump
+++ b/broken.dump
## -336,5 +336,7 ##
000014f0 b8 d0 3d 76 85 f8 76 9d e6 50 74 df e7 a7 bd b0 |..=v..v..Pt.....|
00001500 00 f1 6e 05 63 0a 65 6e 64 73 74 72 65 61 6d 0a |..n.c.endstream.|
00001510 65 6e 64 6f 62 6a 0a 73 74 61 72 74 78 72 65 66 |endobj.startxref|
-00001520 0a 35 32 31 33 0a 25 25 45 4f 46 0a |.5213.%%EOF.|
-0000152c
+00001520 0a 35 32 31 33 0a 25 25 45 4f 46 0a 00 00 00 00 |.5213.%%EOF.....|
+00001530 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
+*
+00010000
What we have here is a genuine bug. I filed: https://github.com/mozilla/pdf.js/issues/3634
Since the data transfer does not specify a content-length, but uses chunked transfer encoding, pdf.js will use an initial buffer of 64kb that is doubled each time it would overflow. However, once the transfer is complete pdf.js will not shrink that buffer to the actual size, nor will it remember the actual size, so that upon download the whole over-sized buffer (still the initial 64kb in your example) will be transferred.
I don't think there is a real work-around, short of not using pdf.js at all (which is a user choice).

Ruby - Window command line interface

UPDATE -- I tried exactly the same thing using Python and it works perfectly !!
import os
os.system('certutil.exe -v -getkey "614D628A00000000014C" C:/Users/kra/kevin')
Could somebody shed some light on this issue please!
If I run this ruby code:
require 'open3'
stdin, stdout, stderr = Open3.popen3('certutil -v -getkey "614D628A00000000014C" C:/Users/kra/kevin')
puts stdout.read
I get the following error:
Querying WIN-3CF41NBPT85.demo.com\demo-CA
CommonName: 614D628A00000000014C
CertUtil: -GetKey command FAILED: 0x80092004 (-2146885628)
CertUtil: Cannot find object or property.
However if I run the command directly from the command line, it works.
C:\Users\kra>certutil -getkey "614D628A00000000014C" C:/Users/kra/kevin
Querying WIN-3CF41NBPT85.cjndemo.com\cjndemo-CA.....................
"WIN-3CF41NBPT85.demo.com\demo-CA"
Serial Number: 614d628a00000000014c
Subject: CN=Kevin, C=GB
NotBefore: 11/30/2012 10:20 AM
NotAfter: 5/7/2013 9:29 AM
Template: Copy of Web Server
Version: 3
Cert Hash(sha1): 88 b1 7a 74 8c be 73 d5 16 07 7f 19 16 57 14 c5 dd a9 79 7f
Recipient Info[0]:
CMSG_KEY_TRANS_RECIPIENT(1)
CERT_ID_ISSUER_SERIAL_NUMBER(1)
Serial Number: 129e45d3000000000130
Issuer: CN=demo-CA, DC=demo, DC=com
Subject: CN=kra, CN=Users, DC=demo, DC=com
CertUtil: -GetKey command completed successfully.
Interestingly if I run this ruby code:-
require 'open3'
stdin, stdout, stderr = Open3.popen3('certutil -recoverkey -p lexicon C:\Users\kra\kevin C:\Users\kra\kevin.pfx')
puts stdout.read
It also works.
Computed Hash: 6e d3 b8 ad 93 16 7b f0 fb b3 f5 cd 7e e4 bb ad fb 95 a0 81
User Certificate:
Serial Number: 614d628a00000000014c
Issuer: CN=demo-CA, DC=demo, DC=com
Subject: CN=Kevin, C=GB
Cert Hash(sha1): 88 b1 7a 74 8c be 73 d5 16 07 7f 19 16 57 14 c5 dd a9 79 7f
CertUtil: -RecoverKey command completed successfully.
I'm assuming its some kind of weird environmental thing because clearly ruby is able to call the certutil.exe command?
The first parameter for Open3.popen3 is the environment you want to pass to the sub-command. I've had to use this to get things working as I expected on occasion:
Open3.popen3(ENV, 'command') { ... }
which passes the current script's environment to the sub-command. The current script will have inherited its environment from the command-line, so, theoretically, the sub-command will have the same information the command issued at the command-line did.
If necessary, you can also extract subsets of ENV, or temporarily overwrite variables before calling popen3.
Instead of popen3, try using capture3. It's very similar, but I consider it a bit less low-level. I've seen some weird behavior with popen3 that capture3 avoided nicely. Also, again, notice that you can pass in ENV.

Windows batch file - The system cannot find the batch label specified

The Problem
I'm having a problem with a Windows batch file and labels.
I keep getting this error:
The system cannot find the batch label
specified
What I've tried
Two computers; a WindowsXP and a 2003 Server.
Made sure it was encoded as ASCII
Editted the hex code for the line continuation characters. Tried replacing all with CR , LF, and CRLF in turn. All combinations give me the same error.
Tried inserting extra characters before the label to make the label past 512 characters.
Here is the code:
cls
#echo off
SET zip=7za a dependencies.7z
call:dozip "c:\temp\dir.txt"
pause
goto exit
:dozip
echo Testing 1.2.3...
%zip% %1
goto:eof
:exit
Here's the hex with CRLF (0d 0a).
63 6c 73 0d 0a 53 45 54 20 7a 69 70 3d 37 7a 61 20 61 20 64 65 70 65 6e 64 65 6e 63 69 65 73 2e 37 7a 0d 0a 63 61 6c 6c 3a 64 6f 7a 69 70 20 22 63 3a 5c 74 65 6d 70 5c 64 69 72 2e 74 78 74 22 0d 0a 0d 0a 70 61 75 73 65 0d 0a 67 6f 74 6f 20 65 78 69 74 0d 0a 0d 0a 3a 64 6f 7a 69 70 0d 0a 20 20 65 63 68 6f 20 54 65 73 74 69 6e 67 20 31 2e 32 2e 33 2e 2e 2e 0d 0a 20 20 25 7a 69 70 25 20 25 31 0d 0a 67 6f 74 6f 3a 65 6f 66 0d 0a 3a 65 78 69 74
Here's the console's output (when I remove #echo off):
C:\>SET zip=7za a dependencies.7z
C:\>call:dozip "c:\temp\dir.txt"
C:\>echo Testing 1.2.3...
Testing 1.2.3...
C:\>7za a dependencies.7z "c:\temp\dir.txt"
The system cannot find the batch label specified - dozip
C:\>pause
Press any key to continue . . .
It never actually creates the 7zip file, so I think I can assume that its crashing on this line;
7za a dependencies.7z "c:\temp\dir.txt"
If I run that line by itself from a command prompt, it works fine and creates the dependencies.7z, so I don't think its necessarily a problem with 7za.exe.
I've already read this stackoverflow question:
stackoverflow.com/questions/232651/why-the-system-cannot-find-the-batch-label-specified-is-thrown-even-if-label-ex
and the link from that post;
help.wugnet.com/windows/system-find-batch-label-ftopict615555.html
The Answer
So, I found the problem guys.
I was using a technique I commonly use that I could only really describe as "proxy" batch files. I have a folder called c:\scripts, and I put several bat files in there to target commonly used exes. This saves my PATH variable from becoming absolutely massive with all of my command line tools. This way I only need to add c:\scripts to my PATH, and create a proxy batch file when I need something.
I had 7za.bat in c:\scripts, containing only this;
#echo off
"C:\Program Files\7-zip\7za.exe" %*
I changed my script to this;
SET zip="c:\program files\7-zip\7za.exe" a dependencies.7z
instead of this;
SET zip=7za a dependencies.7z
and it worked flawlessly.
The moral of the story...
Avoid calling other batch files from within a batch file. If you do, you will need to prefix them with "call".
I would point out that the "Testing 1.2.3..." and "Press any key to continue . . ." lines indicate that execution has successfully gone to the :dozip label and then successfully returned to the caller.
Is the "7za" executable actually a batch file? If I modify my test script to have the helper be a batch file, I get the same error. The fix is to do 'call %zip% %1'
the moral of the story: when calling external programs/batch files in a batch file, use call
call foo.bat
and/or
call %foo%
(Calling one batch from another has been done since the days of DOS, just remember to call)
The "The system cannot find the batch label specified" error message brought me here (10 years later!) and the problem turned out to be the CRLF was incorrect.
The cause, in our case, was a Git Repository that didn't recognize BAT files as being text files which needed to be CRLF on a Windows 7 machine.
Our solution was to create a .gitattributes file containing the line:
*.bat text eol=crlf
Then deleting the BAT files and checking out from the repository re-wrote our BAT files with the correct line endings. The BAT labels are now working again.
One possibility, although it seems unlikely, is that command extensions aren't enabled, or up-to-date, and this is interfering with call/goto/label behaviour.
Try:
echo [%cmdextversion%]
and if it's less than [2] (or empty -- []) then check to see if cmd.exe is being invoked with /e:off, or just run
cmd /e:on
in the console window where you will run this batch file.
In my case the reasons of "The system cannot find the batch label specified" were:
No CALL before jar- and unzip-commands
No ENDLOCAL for every SETLOCAL DisableDelayedExpansion
Some other batch-magic, which I didn't completely understood near SETLOCAL DisableDelayedExpansion, it was important to place SET param=1 after SETLOCAL DisableDelayedExpansion or something like that
Are you using Windows NT 4/Windows 2000? Only there can you use CALL to call subroutines within the same batch file.
Looking closely at your hex, it does not actually have all CRLF (0d 0a). Several lines end in LF-only (0a without a preceding 0d).
Check in your hex editor to make sure every 0a is preceded by 0d (exactly one).
Or just cut and paste your file into a blank Notepad document and re-save it.

Resources