Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save PuTTY output to file from command-line

Is there any way to save the PuTTY output to a file using the command line? I know this is easily done using the GUI but in my case it has to be done automatically.

What I'm working on:

User clicks batch file -> starts PuTTY, automatically connects to my device over SSH and runs a bunch of commands -> PuTTY should save the output to a file.

The last part I can't get working. Is there any command to do this?

like image 915
sjaak Avatar asked Dec 15 '14 05:12

sjaak


People also ask

How do I copy text from PuTTY terminal to Notepad?

Left-click inside the PuTTY terminal window near the text you want to copy. Holding down the left mouse button, drag your cursor across the text to select it, then release the button to copy it. Left-click on the destination Windows application where the pasting will occur. Right-click and select Paste or press Ctrl+V.

How do I download a text file from PuTTY?

Enter pscp.exe [email protected]:/file_path/filename c:\directory\filename on the command line except replace “username” with the name of an account that has permissions to access the remote computer through SSH, replace “x.x.x.x” with the IP address or hostname of the remote SSH computer, replace “file_path” with the ...


4 Answers

This can be done with putty. The answer is little late considering the time the questions was asked, however this might help someone.

In putty, using GUI, you can save sessions with logging option on, as shown below.

enter image description here

enter image description here

Enter Host Name, Name the session, Go to Logging Option in the left top corner, select all sessions, provide log file name and location, go back to Session tab, click on the save button. Done, you have saved a session.

Now open CMD and write the command as below enter image description here

You are done. Every time this session is invoked, the commands and output will be logged. Hope this helps.

like image 158
azim Avatar answered Oct 20 '22 08:10

azim


The specific program putty is not designed for this. Instead use plink, a different program in the PuTTY suite, which uses the same session settings and keys as putty but gets input from stdin and puts output to stdout, both of which can be redirected in the usual ways. See http://the.earth.li/~sgtatham/putty/0.63/htmldoc/Chapter7.html#plink .

like image 45
dave_thompson_085 Avatar answered Oct 20 '22 07:10

dave_thompson_085


As mentioned in previous answer, use plink for this.

Make sure it is in your environment path, by typing

plink -V

in your console. If it returns a version number, then you know it is in environment path variables. If it doesn't, probably best to fix this first. There are plenty of good SO answers to help you with this. Failing that, use the full path to your plink.exe in the CLI command that follows.

Then use plink to open your ssh connection, with the option -v set to provide verbose output. Finally, this all needs to be piped to a log file.

The complete cli command that I use is

plink -v [email protected]  > ssh-output.log 2>&1

Open up the file ssh-ouput.log to see the results.

like image 1
charliefortune Avatar answered Oct 20 '22 08:10

charliefortune


Expanding on Dave's and Charlie's answers...

Apart from making sure plink is in the path, also check whether you have write access to local ouput file.

This is how you redirect command output from remote machine to local file with plink. In this example we store an output from man page for nfcapd:

plink [email protected] -pw joespassword man nfcapd > output.log 2>&1

The first time you try to access the server, it will ask you store key in cache. So make sure to access the machine at least once before:

plink [email protected] -pw joespassword

The server's host key is not cached in the registry. You 
have no guarantee that the server is the computer you
think it is.
...
Store key in cache? (y/n)
like image 1
rluks Avatar answered Oct 20 '22 08:10

rluks