Search code examples
batch-filesshterminate

Terminate SSH call through scripting without any manual intervention in Windows environment


What I am trying to achieve here:

I want to automate a daily process of starting a remote SSH session through PuTTY, calling some commands taking its response into a txt file and terminate this SSH session through script.

Now as per suggestion by Mofi, I have this thing working which is like:

plink remote_host_name -m "dir\with\Inputcommands.txt" -l username -pw password >> "dir\with\OutputResponse.txt" 

This actually works okay, but I want it to be able self terminate this session once the OutputResponse is saved.

Any suggestions on how to terminate this through the same script/without manual intervention in Windows 7 environment?


Solution

  • I downloaded putty.zip from Download PuTTY: latest release (0.70) with 64-bit version of PuTTY and its additional tools and extracted the ZIP file to temporary directory C:\Temp\PuTTY.

    Next I double clicked on C:\Temp\PuTTY\PUTTY.CHM to open this help file containing complete PuTTY user manual.

    On displayed contents page I first clicked on list item 2.5 Logging out and read about how to log out from a session. I read that server's own logout command should be used which might vary between servers and logout or exit should be tried if not knowing it.

    I clicked on button Back and clicked on contents page next on 3.8 The PuTTY command line. On this help page I clicked first on 3.8.1 Starting a session from the command line. I opened a command prompt window, used cd /D C:\Temp\PuTTY to set current directory to directory of PuTTY and started to write the command line according to information on this help page.

    plink -ssh username@hostname
    

    I clicked again on button Back in help and next on list item 3.8.3.4 -l: specify a login name. After reading I understood that this is an alternate method to specify user/login name for the host (name or IP address) which I don't need for the command line already typed, but not yet executed. So nothing to change on command line in command prompt window.

    I clicked again on button Back and next on list item 3.8.3.6 -m: read a remote command or script from a file. After reading this help page I created in same temporary folder containing the PuTTY files a text file with name GetLists.txt with the lines:

    ls -l /
    ls -l /lib
    history -c ; exit
    

    This commands file was saved as ANSI file with DOS/Windows line terminations. I don't know if Unix line endings would be better. Nothing is written about line termination type in help of PuTTY.

    And I extended the already typed command line in command prompt window to:

    plink -ssh username@hostname -m GetLists.txt
    

    I clicked again on button Back in help and next on list item 3.8.3.8 -pw: specify a password. After reading the two small paragraphs on this help page I extended the command line in the command prompt window once again to:

    plink -ssh username@hostname -m GetLists.txt -pw password
    

    I clicked again on button Back in help and looked if something other could be also important, but I did not look so.

    So I switched to command prompt window and executed this command. It worked as expected with displaying the two directory lists. (I used PUTTY.EXE months or years ago (I don't remember) for this host and therefore the key data were already stored on my computer for this host.)

    I pressed key UP and extended the command line to:

    plink -ssh username@hostname -m GetLists.txt -pw password >DirectoryLists.txt
    

    I executed this command and it worked without displaying anything on screen.

    So I typed next the command:

    type DirectoryLists.txt
    

    And the output was the two directory listings as seen already before.

    Conclusion: I can't reproduce your problem with my test configuration on the host I connected to.

    By the way: I used PuTTY months or years ago (I don't remember) to connect to this host with older version 0.64. But it was the first time that I used plink.exe. And I did not have any problem with reading in manual and creating the command line step by step as written here.