How does PUTTY/PLINK determine a command has returned? - powershell

Is it newline? prompt? What exactly?
Trying to run powershell over plink and the command executes but plink doesn't recognise its finished and the session hangs. Most curiously though, the command executes successfully when sent through the shell (via Putty). However, when sent via plink, the same command hangs...
Any ideas?

Telnet is nearly a raw TCP connection. All Putty needs back is a response from the server. The rest is controlled by the shell and SSH/Telnet server.
While your task is running, it's not going to return a command prompt.
On Linux, Unix, and Mac OS X you could put a & after the command to run it in the background and return to the command prompt.
Try running it in the local terminal/command shell. You should basically see the same thing.

Ok, well I'm still not quite sure what the problem is, but I've found a workaround via the TeamCity forums.
Basically you want to echo some abitrary string and pipe that output into your powershell executable, like thus:
echo 'executing powershell...' | C:\windows\system32\windowspowershell \v1.0\powershell.exe exit 1
So then your full plink command becomes:
plink.exe user#someIp -i key.ppk -P 22 -batch -v "echo 'executing powershell...' | C:\windows\system32\windowspowershell\v1.0\powershell.exe exit 1"
Nb. Plink will still pass through return codes and console output using this method.
Link to TeamCity forum:
http://youtrack.jetbrains.net/issue/TW-6021
Hope this helps

I had the same problem with an other program. I used the >&2 (redirect output to std err) after the last command, this worked fine for me.

Just add "return XX" into remote shell script, it will be return value to local console. After plink has been finished, type echo %errorlevel% to see return code XX.

Related

How to run a cmd line application in PowerShell

I want to run this command
telnet -a servername port
-CASServer V2.00 connection from 0.0.0.0 [] Diag mode only
status
Clients: Static 0/50 Dynamic 19/50
Application1: 1 0xcb4388 hSock(0x2d4)
Application2: 0 0
OK
I am basically only interested in "Dynamic 19/50". If possible, I'd like to get this information from PowerShell
The PowerShell script at http://community.spiceworks.com/scripts/show/1887-get-telnet-telnet-to-a-device-and-issue-commands might work for what your're trying to accomplish. It creates a PowerShell function called 'Get-Telnet'; it can establish a telnet connection and send a series of commands.
Your usage would be something like:
Get-Telnet -RemoteHost [servername] -Port [port] -OutputhPath results.txt
After which you could parse the data you need (e.g., the 'Clients' line) from 'results.txt'.
In order to run a cmd line application in PowerShell, specifically the telnet command, you first need to install telnet.
Use this command, with admin privs:
pkgmgr /iu:"TelnetClient"
After installing you can run your telnet -a servername port command in PowerShell:
Running a command line application (telnet) in PowerShell
I am not positive on what exact command line application you are trying to run. However here is an example of running a command line application in powershell.
&'C:\Program Files\gs\gs9.14\bin\gswin64c.exe' -sDEVICE=tiffg4 -dBATCH -dNOPAUSE -q -r600 "-sOutputFile=$OutputFullDirectory" "$InputPath"
This would be ghostscript however the key is using the & symbol to call the command line and then enter in the path to the exe file you want to run then you can pump in the parameters. Hope this helps lead you in the right direction

unable to take user input in perl

I am having a strange issue. I have written a script which is basically running a perl script in remote server using ssh.
This script is working fine but after completion of the above operation it will ask user to choose the next operation.
it is showing the options in the command prompt but while I am giving any input it is not showing in the screen even after hitting enter also it remain same.
I am not getting what is the exact issue, but it seems there is some issue with the ssh command because if I am commenting out the ssh command it is working fine.
OPERATION:
print "1: run the script in remote server \n2: Exit\n\nEnter your choice:";
my $input=<STDIN>;
chomp($input);
..........
sub run_script()
{
my $com="sshg3.exe server -q --user=user --password=pass -exec script >/dev/null";
system("$com");
goto OPERATION;
}
after completing this ssh script it is showing in screen:
1: run remote script
2: exit
Enter your choice:
but while I am giving any input it is not displaying in the screen until and unless I am exiting it using crtl C.
Please can anyone help what might be the issue here ?
One of the classic gotchas with ssh is this - that it normally runs interactively, and as such will attach STDIN by default.
This can result in STDIN being consumed by ssh rather than your script.
Try it with ssh -n instead.
You can redirect the output in command prompt if -n option is not available for you.
try this one it might work for you.
system("$com />null");
As per https://support.ssh.com/manuals/client-user/62/sshg3.html there is an option for redirecting input use --dev-null (*nix) or --null (Windows).
-n, --dev-null (Unix), -n, --null (Windows)
Redirects input from /dev/null (Unix) and from NUL (Windows).

interactive powershell from Cygwin

I cannot run PowerShell.exe interactively in a Cygwin rxvt or mintty terminal. Seems any session using a /dev/tty? or /dev/pts? pseudo terminal device. An instance using the junky windows console device /dev/console or /dev/cons? will work.
cygstart /bin/bash -li
launches the console version in the cruddy Windows Command Prompt which is the only place I can get an interactive PowerShell.
Works. Rxvt doesnt:
Nor does mintty:
I've tried all the echo -e | powershell.exe and powershell.exe </dev/null
I'm assuming when I see answers on Stackoverflow on this they are using Console's ... or am I missing something?
Why I cannot run PowerShell 2 from Cygwin? seems to run fine, just gets powershell v3 when he wants v2 ... wish I had that problem.
I've developed a powershell wrapper to call powershell scripts and commands from a Cygwin terminal session but cannot get the interactive option to work (if you give the wrapper no script or commands then you want to go interactive). see https://bitbucket.org/jbianchi/powershell/wiki/ for info on the wrapper script. It works for most powershell.exe calls and even acts like a "she-bang" if used in the first line of the ps1 script.
Today, typing powershell at a Cygwin bash prompt just works.
If you need to run powershell inside cygwin/babun, follow https://code.google.com/p/mintty/issues/detail?id=56#c64 . Bascilly, downloard or compile https://github.com/rprichard/winpty, copy it to your $PATH and then run
console.exe powershell
This also works with batch scripts that invoke powershell inside.
The solution I've found is to use http://sergeybelous.com/ (main site) Proxy32 proxywinconsole.exe program. If this program is in the path, my poweshell.bash wrapper will call it which will let you work interactively with PowerShell.
First Install cygwin in your system.
After that type bash on powershell terminal and you can access cygwin terminal.
PS C:\Users\username\1and1> bash
username#LWMT-14R25Q2:/mnt/c/Users/username/1and1$
I'm afraid I can't answer your question, but maybe I can help you a little further on your way with this:
I believe this is related to this issue, as discussed on the MinTTY Issue #56.
It goes into great detail as to how common cmd.exe shell applications work and interact, so much as they detail how common unix applications using their TTY abstraction layer work differently than Windows command line applications.
Script for running powershell on Cygwin minty:
blahblah#blahblahbin $ cat pwrshl
#!/usr/bin/bash if [[ ! -f "$1" ]] then
echo "Usage: $0 <PowerShellScriptFile>"
exit fi
echo "\n" | powershell -Command "& {$(<$1)}"

Can I execute a multiline command in Perl's backticks?

In Unix, I have a process that I want to run using nohup. However this process will at some point wait at a prompt where I have to enter yes or no for it to continue. So far, in Unix I have been doing the following
nohup myprocess <<EOF
y
EOF
So I start the process 'myprocess' using nohup and pipe in a file with 'y' then close the file. The lines above are effectively three seperate commands - i.e. I hit enter on the first line in UNIX, then I get a prompt where I enter 'y' and then press enter to then finally type 'EOF' and hit return again.
I want to know execute this in Perl but I am not sure how I can execute this command as it is over three lines. I don't know if the following will work....
my $startprocess = `nohup myprocess <<EOF &
y
EOF
`
Please help - thank you!
I think your proposal will work as is. If not, try replacing the redirect with a pipe:
my $startprocess = `(echo "y" | nohup myprocess) &`;
Also, depending on WHY you are doing a nohup, please look at the following pure Perl daemonizing approach using Proc::Daemon : How can I run a Perl script as a system daemon in linux?
Expect for interactive programs can be used as well.

Cygwin - run script silenty from "run command"

I have script lets say:
C:\foo.bsh
I want to be able to run this command via the windows run command:
Start -> Run
Windows Key + R
and type something small like 'foo' and hitting return.
However, I do not want a cmd prompt to be visible. This script does some preprocessing for an IDE. I do not want the cmd prompt to be open for the lifetime of the IDE process.
I have tried:
1) Creating a bat file with the following contents:
c:\cygwin\bin\bash --login "C:\foo.bsh" (this fails because it keeps a cmd open)
2) Converting the above bat file to an exe using bat_2_exe_converter (does not make the cmd silent)
thoughts?
EDIT: The solution so far suggests something to type from an actual cygwin shell. I am trying to get a faster solution by having something short I can type in the Windows run command. Also, the nohup command; exit doesn't automatically kill the box - however I can manually kill it without killing the IDE process. The run command accepts shortcuts (.lnk's), bat's, exe's.
Try the run.exe command of cygwin. It is a big install, a complete unix environment for your Windows machine. Suppose you installed it at c:\cygwin\.
No mystery, just run c:\cygwin\bin\run.exe <your command here> and you will have your no dos window execution.
You can run it from any DOS window (run cmd.exe from the start menu). You don't need to run it from cygwin.
To make it easier, append C:\cygwin\bin to your %PATH% env var (My Computer → Properties → Advanced → Environment Variables) (Kudos to Felipe Alvarez comment).
Now you can just type
c:\cygwin\bin\run.exe "C:\foo.bsh"
You must create a link in your Start Menu with this command so will be able to run it with Win-R.
Here is the man page of the runcommand:
$ man run
RUN(1) run 1.3.0 RUN(1)
NAME
run - start programs with hidden console window
SYNOPSIS
run [ -p path ] command [ -wait ] arguments
runcommand [ -p path ] [ -wait ] arguments
DESCRIPTION
Windows programs are either GUI programs or console programs. When
started console programs will either attach to an existing console
or create a new one. GUI programs can never attach to an exiting con‐
sole. There is no way to attach to an existing console but hide it if
started as GUI program.
run will do this for you. It works as intermediate and starts a pro‐
gram but makes the console window hidden.
With -p path you can add path to the PATH environment variable.
Issuing -wait as first program argument will make run wait for program
completition, otherwise it returns immediately.
The second variant is for creating wrappers. If the executable is
named runcommand (eg runemacs), run will try to start the program (eg
emacs).
EXAMPLES
run -p /usr/X11R6/bin xterm
run emacs -wait
runemacs -wait
run make -wait
AUTHORS
Charles S. Wilson
Harold L Hunt II
Jehan Bing
Alexander Gottwald
Version 1.3.0 November 2005 RUN(1)
You can use either...
c:\cygwin\bin\bash -l /path/to/script_to_interpret.sh
...or...
c:\cygwin\bin\bash -l -c /path/to/executable_script.sh
Note: the -l flag tell bash to "act as if it had been directly invoked by login" and use Bash Startup Files. This is important in that it sets your $PATH and other things you rely on when you launch a cygwin terminal. If you don't include -l or --login you will get "command not found" when you try to call anything except of a bash builtin.
The difference between the 2 is like the difference between doing...
bash script_to_interpret.sh
...and...
./executable_script.sh
...in *nix. The former interprets the script using bash. The latter executes the script (only if it has chmod +x executable_script.sh) and interprets it according to its "shebang" line. The latter method is also what you want to do if your executable is not a script at all, like a *nix binary compiled from source.)
It has been bugging me for a while I couldn't find the solution for this, but I finally got the right mix together.
You can simply do the following if you have cygwin on your PATH:
run bash test.js
If cygwin is not on your path, you can do this:
c:\cygwin\bin\run.exe -p /bin bash test.js
If you are looking for more control over the created window (maximize, etc) it looks like you can use cygstart also.
Sources:
- neves answer above (though that wasn't enough by itself for me personally to figure it out)
- http://cygwin.com/ml/cygwin/2008-09/msg00156.html
As the terminal can't close while your script is still running, try the command:
"nohup C:\foo.bsh; exit"
This way your script will be backgrounded and detached from the terminal, and it should exit quickly so the terminal goes away. I think that the window may still 'flash' with this approach, but the results should be better than what you're getting.
I'm running Cygwin64 and the xwin server link points to:
C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
This creates an icon AND a notification on the taskbar. I don't like that. The icon is rather useless, the notification has all your menu options from .XWinrc.
So... I wrote a .vbs script to silently run this command and make the icon go away:
Set objShell = CreateObject("WScript.Shell")
objShell.Run("C:\cygwin64\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe"), 0
Another imperfect possibility is to run the script via a shortcut and set the shortcut's Run option to "minimized".
Go to the directory where you have installed cygwin(on my machine it is c:/cygwin64/bin)
Once there simply type "bash.exe"