WinSCP command to execute command on the server - automation

I just wanted to automate the file transfers without opening WinSCP, through script.
Up to some extent I performed the file transfer operation except PuTTY session.
I prepared a sftprun.cmd batch file like:
D:\winscp\winscp.com /script=D:\winscp_auto\sftpscriptJAC.txt
Can you please suggest me to open PuTTY through above mentioned script to execute command on the server?
mount -o remount,rw /
The putty details should be provided under the script file "sftpscriptJAC".

WinSCP can execute remote commands on its own from the script (with some limitations), using the call command:
open sftp://user#example.com/
put C:\file.txt
call mount -o remount,rw /

Related

Getting "Server unexpectedly closed network connection" after executing a remote command with Plink

I am using Plink to execute remote command:
When using remote command (text file) error occurs:
FATAL ERROR: Server unexpectedly closed network connection
test.bat
"C:\Program Files (x86)\PuTTY\plink.exe" XX.XX.XX.XX -l userID -pw password -m "D:\FindingLog\test.txt"
test.txt
cd log
When I remove -m "D:\FindingLog\test.txt" in batch file, it works (successful login)
What's the problem?
The SSH session closes (and Plink with it) as soon as the command finishes. Normally the "command" is shell. As you have overridden this default "command" and yet you seem to want to run the shell nevertheless, you have to explicitly execute the shell yourself:
cd log
/bin/bash
Also as use of -m switch implies a non-interactive terminal, you probably want to force an interactive terminal back using -t switch.
See also How to prevent PuTTY shell from auto-exit after executing command from batch file in Windows?
Upgrading to plink 0.74 fixed this issue for me (from much older version 0.60).

Start SSH connection in PuTTY, run a command, and leave the session active

I want to run a few shell commands every time I SSH to a server via PuTTY. I'm connecting to a production web server managed by someone else, and I don't want to store my own scripts there.
I see the option Connection > SSH > Remote Command, but if I put my initialization commands there, after starting the session, it closes immediately after the commands execute. How can I run the Remote Command, and then keep the session open so I can continue using it?
The SSH session closes (and PuTTY with it) as soon as the command finishes. By default the "command" is a shell. As you have overridden this default "command" and yet you want to run the shell nevertheless, you have to explicitly execute the shell yourself:
my-command ; /bin/bash
See also Executing a specific command on the server.
One option to go is set up your putty remote command like this:
ls > dir.ls & /bin/bash
In this example command you want to run is "ls > dir.ls" what creates file dir.ls with content of directory listing.
And as you want to leave shell open you can add aditional command "/bin/bash" or any other shell of your choice.

Getting PSEXEC to run a local .vbs file on a remote machine without copying it over

I would like to get PSEXEC to run a .vbs file on a remote machine for me, however I currently have to have the .vbs located on the remote machine.
Below is an example of my script which does work.
psexec \\<\i.pAddress\> -u <\User\> -p <\Password\> -w c:\ -h cscript.exe "C:\Users\admin\Desktop\test.vbs"
Is there any solution which saves me having to place the test.vbs file on the remote machine before hand?
Use the -c option (http://technet.microsoft.com/en-us/sysinternals/bb897553.aspx)
-c Copy the specified program to the remote system for execution. If you omit this option the application must be in the system path on the remote system.

In command line, putty remote shell doesn't work, but in GUI, it does work

Using the GUI putty.exe, I can connect to my windows server and once it is connected, i can type any command like rename file or mkdir folder and they all work
However, using command line such as
putty -load test -m C:\users\test.txt
or using the GUI putty, but add 1 command to remote command in SSH under Connection, then the command doesn't get executed.
Can anyone explain to me why this is happening or how can i fix this? I am using FreeSSHd on windows 2008 server.
Not sure if this helps, but try adding the /bin/bash directly after your command in the text file. It will keep the window open and you can see what the output of the shell would be if you ran it from the gui.
; /bin/bash
For example if test.txt is running a script
bash myscript.sh
bash myscript.sh; /bin/bash
This is assuming bash.

Passing shell script file

I have a linux shell script file which collects various data from linux server. (Services, Process, FreeSpace etc.).
From windows to collect the data we are using Plink to connect to linux Boxes and run the shell script
plink root#servername -pw Password -noagent -m Batch-File.
and using pscp to copy the file to windows location.
Now when I try to do the same for Esxi the plink command fails with the error below.
FATAL ERROR: Server unexpectedly closed network connection
though If i give a direct command as below.
plink root#servername -pw Password -noagent ls /etc
works out.
Let me know how to use the plink for esxi .. if possible.
After seeing the messages log it looks like that the issue is with esxi's limitation to read long character string. The message log fails in the session with String Too Long and then post a message of closing the connection.
Thus the approach was to copy the shell script as a pscp connection, run the file with executable permission and collect the data gathered and delete the file from system.