Passing shell script file - plink

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.

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.

WinSCP command to execute command on the server

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 /

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.

How to run a script file remotely using SSH

I want to run a script remotely. But the system doesn't recognize the path. It complains that "no such file or directory". Am I using it right?
ssh kev#server1 `./test/foo.sh`
You can do:
ssh user#host 'bash -s' < /path/script.sh
Backticks will run the command on the local shell and put the results on the command line. What you're saying is 'execute ./test/foo.sh and then pass the output as if I'd typed it on the commandline here'.
Try the following command, and make sure that thats the path from your home directory on the remote computer to your script.
ssh kev#server1 './test/foo.sh'
Also, the script has to be on the remote computer. What this does is essentially log you into the remote computer with the listed command as your shell. You can't run a local script on a remote computer like this (unless theres some fun trick I don't know).
If you want to execute a local script remotely without saving that script remotely you can do it like this:
cat local_script.sh | ssh user#remotehost 'bash -'
It works like a charm for me.
I do that even from Windows to Linux given that you have MSYS installed on your Windows computer.
I don't know if it's possible to run it just like that.
I usually first copy it with scp and then log in to run it.
scp foo.sh user#host:~
ssh user#host
./foo.sh
I was able to invoke a shell script using this command:
ssh ${serverhost} "./sh/checkScript.ksh"
Of course, checkScript.ksh must exist in the $HOME/sh directory.
Make the script executable by the user "Kev" and then remove the try it running through the command
sh kev#server1 /test/foo.sh