BSOD in windows7 because of Simple tcl script - windows-xp

I am using Windows7 64-bit and TCL8.4. i have a simple script as follows:
package require Expect;
spawn ping 192.168.25.23 -n 15;
#NOTE: output of the ping is "request timed out" for my testing purpose.;
set id $spawn_id;
expect -i $id "Ping statistics *";
This script is giving Blue Screen Error of Death.
Same script i ran in another machine having windowsXP. here i did not observed any such issue.
FYI:
default timeout in "ping" is 4sec and that of "Expect" is 10sec.
Any help would be appreciable.

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

Perl Try::Tiny stuck in Loop

I am using a Try::Tiny statement to avoid failing if I can't rsh to a host. But it seems that the rsh is getting retried indefinitely.
In this particular case, Host2 went down, and the code just repeatedly attempted to contact Host2 until I cancelled execution with CTRL+C. What I am expecting is for Host2 rsh command to fail, and the script to move on to Host3.
Any idea of why this doesn't work and how I could do it differently?
#Win_Clients = ("Host1","Host2","Host2");
foreach $Win_Client (#Win_Clients){
try {
#mem= rsh ($Win_Client, "wmic memphysical get MaxCapacity");
print "host=$Win_Client, mem=$mem";
};
}
I think your best solution would be to not use rsh to run wmic, and instead use the built in remote access mechanism /node
C:\Users\ME>wmic /node:localhost memphysical get MaxCapacity
MaxCapacity
4194304
C:\Users\ME>wmic /node:narf memphysical get MaxCapacity
Node - narf
ERROR:
Description = The RPC server is unavailable.
And then you won't need to do anything like running rsh or try. You can simply trap error levels in the 'usual way'. ($? in your perl client)
C:\Users\ME>wmic /?
[global switches] <command>
The following global switches are available:
/NAMESPACE Path for the namespace the alias operate against.
/ROLE Path for the role containing the alias definitions.
/NODE Servers the alias will operate against.

Run perl script on remote server

Is it possible to run perl script, which is located on a remote server, on that server from Windows? There is a job on a remote server that I want to get done every time I make something on Windows.
You have to have something listening for an instruction to run the script, and then you have to send the instruction.
There are lots of approaches you could take to that, including:
Running an SSH server and then connecting to it from an ssh client on the windows machine
Running an HTTP server, running the script through FastCGI, and then requesting the URL for it from curl or a browser on the Windows machine
Writing a custom protocol, listening on a socket, and then writing a custom client that you run on the Windows machine
Absolutely.
You can use plink to run commands on the server from Windows, assuming the server is running sshd.
plink user#a.domain.ext echo hi
This will print "hi\n" to the standard output.
Substitute /path/to/perl/script for echo above and substitute hi with any command line argument that the script needs.
plink is available here: http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
One cautionary personal note from doing this many times is that the environment in which the perl script will be run is much less complete than what you would experience when logging in via a full SSH session and running the command interactively. Many environment variables you would normally expect are unset.
For instance using "set | wc -l" in the command above produces only 39 environment variables defined, but from an interactive SSH session, there are 57 environment variables defined. You have to make sure your perl script isn't depending on an environment variable that hasn't been set. For instance, you may need to use full paths for any modules that it uses, or by using the -I flag in the shebang line, because #INC may not be what you expect it to be.

Powershell Output inconsistencies

I have a powershell script that works just fine when I open powershell manually and run the script. It produces an output like such:
10.52.30.131 BALL-AIRKYYCP0 Not installed Ping successful Windows
10.52.30.133 BALL-4FNRAMLOD Not installed Ping successful Windows
10.52.30.134 BALL-5UU20W8E2 Not installed Ping successful Windows
If I right click the script file and then click run in powershell the script runs fine and does everything it needs to do but the output returned is different, see below:
10.52.30.131 BALL-AIRKYYCP0 Not installed Ping successful Wind
ows
10.52.30.133 BALL-4FNRAMLOD Not installed Ping successful Wind
ows
10.52.30.134 BALL-5UU20W8E2 Not installed Ping successful Wind
ows
For some reason running it by right click 'Run in Powershell' causes the output to be messy and cells to be cut off and finished on the next row. This is a small sample
Any ideas why the output would be different when running the script this way?
It appears the console windows were different sizes depending how you were launching it.
You can set your console window size from within your Powershell script if you want, using get-host.
For example, this will set the width of the console to 120:
$ws = (get-host).UI.RawUI.WindowSize
$ws.Width = 120
(get-host).UI.RawUI.WindowSize = $ws
Got this technique from here: http://technet.microsoft.com/en-ca/library/ee156814.aspx

Perl's backticks/system giving "tcsetattr: Input/output error"

I'm writing a perl script that kicks off a script on several different servers using ssh. The remote script needs to run as long as this script is running:
#!/usr/bin/perl
require 'config.cfg'
##servers is defined in config.cfg
#Contains server info as [username,hostname]
#
# #servers = ([username,server1.test.com],[username,server2.test.com])
#
foreach $server ( #servers ) {
my $pid = fork();
if ( $pid == 0 ) {
$u = ${$server}[0];
$h = ${$server}[1];
print "Running script on $h \n";
print `ssh -tl $u $h perl /opt/scripts/somescript.pl`;
exit 0;
} else {
die "Couldn't start the process: $!\n";
}
}
[...]
When I run this script, I get the following output:
./brokenscript.pl
Running script on server01
$ tcsetattr: Input/output error
Connection to server01 closed.
The same result occurs when running with system (and backticks are preferred anyways since I want the output of the script). When I run the exact command between the backticks on the command line, it works exactly as expected. What is causing this?
The tcsetattr: Input/output error message comes from ssh when it tries to put the local terminal into “raw” mode (which involves a call to tcsetattr; see enter_raw_mode in sshtty.c, called from client_loop in clientloop.c).
From IEEE Std 1003.1, 2004 (Posix) Section 11.1.4: Terminal Access Control, tcsetattr may return -1 with errno == EIO (i.e. “Input/output error”) if the calling process is in an orphaned (or background?) process group.
Effectively ssh is trying to change the setting of the local terminal even though it is not in the foreground process group (due of your fork and, the local script exiting (as evidenced by the apparent shell prompt that comes immediately before the error message in your quoted output)).
If you just want to avoid the error message, you can use ssh -ntt (redirect stdin from /dev/null, but ask the remote side to allocate a tty anyway) instead of ssh -t (add your -l and any other options you need back in too, of course).
More likely, you are interesting in keeping the local script running as long as some of the remote processes are still running. For that, you need to use the wait function (or one of its “relatives”) to wait for each forked process to exit before you exit the program that forked them (this will keep them in the foreground process group as long as the program that started them is in it). You may still want to use -n though, since it would be confusing if the multiple instances of ssh that you forked all tried to use (read from, or change the settings of) the local terminal at the same time.
As a simple demonstration, you could have the local script do sleep 30 after forking off all the children so that the ssh command(s) have time to start up while they are part of the foreground process group. This should suppress the error message, but it will not address your stated goal. You need wait for that (If I am interpreting your goal correctly).
That probably happens because you are forcing SSH to allocate a tty when stdin/stdout are not really ttys. SSH tries to call some specific tty function on those handlers (probably forwarded from the remote side) and the call fails returning some error.
Is there any reason why you should be allocating a tty?
Is there also any reason to use the obsolete version 1 of the SSH protocol?