PSEXEC returning garbage result. Why? - windows

I want to run a script on a remote windows 2008 server using PSEXEC of PSTOOLs..
I have installed PSTOOLs on my local machine and can run the PSEXEC command successfully as well. I connect to the remote server through it and it even picks up the script placed there. However, all I get is some sort of garbage values. And after that it states that the intended network is no longer available, which it is because I can see it and access it through the GUI.
I must mention that when I execute that script manually in the remote server it works fine. I am using windows remote desktop utility to connect to the server.
Am I using PSEXEC the correct way? Or is the intended function of PSEXEC the same as what I am trying to do?

First you should have a look at the help page
You must use -w to specify the remote working dir, and you can't call a .bat directly, you have to call cmd.exe. So try this
psexec \\remoteserver -w "c:\users\admin\desktop\" cmd.exe /c youbatch.bat

Related

TeamCity and psexec don't work at all

Im trying to execute script on remote machine (script resides REMOTELY, and NOT in agent folder or whatever) through Command Line Runner:
#echo off
%env.ALLUSERSPROFILE%\JetBrains\TeamCity\plugins\.tools\psexec.exe \\12.34.56.78 -h -u admin -p 12345 T:\Folder\Script\update.cmd T:\Folder\Server
But I always get:
The system cannot find the path specified.
Process exited with code 1
And NOTHING else. TeamCity and psexec just scrambling output and not giving it out on what actually not found, what is the problem or whatever. It just freaks me out to even use psexec to run something.
What am I doing wrong? What else do I need to specify to JUST run it remotely (Im not asking about giving me output of script, because as I searched I understand what such simple functionality is not supported by psexec at all)?

run command as administrator on remote windows machine

Situation: Running the bat file on windows machine:
1. When I double click the bat file: Bat running is failed.
2. When I right click on bat file and run as administrator: Bat run is successful.
Now I have to run this bat file successfully from remote machine.
What I did:
1. Installed freeSSDd on remote machine and configured administrator user on freeSSHd to access shell and SFTP.
2. Now I am able to login to the remote machine using putty.
Problem:
I am not able to run the bat file successfully. How can I achieve this?
I also used runas /savecred /user:administrator C:/install.bat, but It didn't helped.
There is a way to get this working without any 3rd party software.
You have to create a task on the remote machine using the windows task scheduler which simply executes the desired command. There is an option where you can tell the scheruler to run a bat with a specific account. Enter an admin account and the password and check the "run with highest privileges" box. Leave "Triggers" empty, go to "Settings" and check the "Allow task to be run on demand" box. That's it!
Now when you want to run your file from a different location do
SCHTASKS /RUN /S <RemoteServerName> /U username /P password /TN "<task name>"
If you don't want to enter username and password each time you can adept the user policy (e.g. add the calling machine to the trusted list of the server).
If you have installed an ssh daemon, then you can run your BAT in a remote shell, but you remote shell may open up in something other than CMD.COM. I use cygwin to set up sshd and then from a remote machine, if I ssh in to run a command, it is using cygwin's bash. I can run a BAT file, but need to call CMD first:
ssh WINDOWS_SERVER "cmd /C D:\PATH_TO_BAT\BATCHFILE.BAT"
But there are some pieces missing here. I looked briefly at the Freesshd page and saw only graphical interfaces. Does freesshd support remote command execution, or just secure fire transfer? And what sort of shell get executed on the windows server when you run it?
cygwin is an entire Linux subsystem that runs under Windows and includes an sshd server, but might be a bit much for someone starting out: https://cygwin.com/
\n makes a powershell remote server that listens on port 22 (ssh) and dumps you into a powershell prompt, you can then use my steps above to call CMD from powershell, versus a bash shell.
http://www.powershellserver.com/

Detach program from SSH connection on a windows

I need to log in to a Windows server via SSH through a local Python (2.7) script, start a script on the server and then disconnect the SSH connection, so that the local script can continue to run.
As of now, I am using fabric, and the local script will not continue before the remote script is done and the SSH connection is closed.
I have read on a range of forums, but it seems to my (admittedly inexperienced) eyes that most replys use unix commands. I need to be able to log onto a windows machine however.
What can I do?
Thank you very much in advance!
Does this help you? You can write a batch script that'll start in the background:
Running Windows batch file commands asynchronously

forward local port with plink in background and executing command on local machine

I want to write a batch script which forwards a local port on my machine to the server, dataexchange. I have given the server my public key for authentication, and I want to connect using plink, PuTTY's command line SSH tool.
Here's what I want the script to do.
Forward the local port 3309 to dataexchange:3306.
Run a Java program on my local machine which queries the mysql database on dataexchange:3306.
Closes the port.
I'm a novice at writing in batch, but here is what I think should work. I am not sure how to run the Java program in batch on my machine, and I am not sure how to close plink after it run.
#ECHO OFF
plink -L 3309:dataexchange:3306 -l myUser -N &
java -jar myprog.java
You have a few things backward in your example:
Your probably want the java program to connect to localhost:3306 which is then forwarded to the remote dataexchange:3306
The Port forwarding syntax is then: -L 3306:localhost:3306
You need a host to connect to in your plink command. In this case probably myuser#dataexchange
There is no & thing in windows, the start command should help you (Doc)
After the java program completes you can then use taskkill /im plink.exe to kill the plink tunnel. (But beware, that kills all plink processes currently running)
The java command is probably also wrong, you can't run .java files without compilation. (Did you write the program yourself? Then compile it first with javac)

how to connect to a remote server using ssh and getting the information

I'm trying to write a shell script using bash. I have multiple servers and each servers have multiple apps runnings on it. Each server also has specific app scripts to check/stop/start etc. All I want to do is that, do a ssh and connect to the remote server.
Which I'm also able to do sucessfully and exceute the commands also..
In some instance I need to check some process status on a remote machine, the app sepecific scripts already does that. But using my ssh when i try to execute that script I dont get any info ( it gets executed but no info is passed ). How do i get the information from the remote host and display on the local host here.
Any help on this is really appreciated.
Regards,
Senny
You can run remote commands and get results locally by passing the command as a string to ssh.
In your script, you can do:
CMD_OUT=$(ssh user#remote_host "/path/to/script argument")
The command will be run remotely and the output store in the CMD_OUT variable. You can then parse the output in your script to get the results you want.
To simplify usage of your script, you might want to set up passwordless ssh so you don't have to type your password each time the script tries to run a remote command.

Resources