I use spawn for JScript (from windows)
My problem with decode text
???? ??? T-SQL Microsoft SQL Server 2012
???? ??? - russian text.
var worker,path = require('path'), spawn = require('child_process').spawn, cscript = path.join(process.env.SystemRoot, 'system32', 'cscript.exe')
function startCscript() {
worker = spawn( cscript, [ '/nologo', 'testwsh.js' ] );
worker.stdout.setEncoding('utf8');
worker.stdout.on( 'data', onData );
}
function onData (data) {
console.log(data.toString());
}
startCscript();
How do I get Russian letters? Thanks all )
Can you try to set the worker encoding as ucs2 or utf16le.
Change this
worker.stdout.setEncoding('utf8');
to this
worker.stdout.setEncoding('ucs2');
Tell if it solves the issue.
look at issue 2190 and issue 2196:
Node.js does always expect UTF-8 output from a child process, but
Windows with Russian locale defaults to CP866
you need to execute chcp 65001 to change current console code page.
My solution: use iconv-lite, to get the Russian text in the console.
Related
let { Subprocess } = ChromeUtils.import("resource://gre/modules/Subprocess.jsm");
let result = Subprocess.call({ command: "C:\\\\windows\\\\explorer.exe" });
While this works for explorer, mspaint and calculator for instance trying to open cmd.exe using System32\cmd.exe path won't work, is there any explanation for this?
Also I can't seem to be able to pass arguments to explorer.exe, something like :
let result = Subprocess.call({ command: "C:\\\\windows\\\\explorer /seperate, C:\Windows\System32\cmd.exe" });
won't work
Any ideas or help? Thank you
Hello I want to send to the child_process, for example, ping 8.8.8.8-t, that is, an infinite number of ping. And some of the iterations I want to stop this command and execute a new, but in this case I do not want to kill a child process.
Example:
var spawn = require('child_process').spawn('cmd'),
iconv = require('iconv-lite');
spawn.stdout.on('data', function (data) {
console.log('Stdout: ', iconv.decode(data, 'cp866'));
});
spawn.stderr.on('data', function (data) {
console.log('Stderr: ', iconv.decode(data, 'cp866'));
});
spawn.stdin.write('ping 8.8.8.8 -t'+ '\r\n');
spawn.stdin.write(here control-c...); // WRONG
spawn.stdin.write('dir' + '\r\n');
I found your previous question. Looks like you are trying to create/emulate a terminal from within node.js. You can use readline for reading and writing from a terminal.
To write control character, you can see the example from its docs :
rl.write('Delete me!');
// Simulate ctrl+u to delete the line written previously
rl.write(null, {ctrl: true, name: 'u'});
To directly answer the question, to pass special characters you will need to pass their ASCII values. Ctrl + C becomes ASCII character 0x03. Value taken from here.
spawn.stdin.write("\x03");
At:
node js interact with shell application
Trindaz linked to his YouTube video demonstrating how to interact with a bash shell (including interpreters available from the shell):
http://www.youtube.com/watch?v=16nFMucvwYQ
However I could not follow the frames from 20 seconds to 54 seconds. At 54 seconds the browser window shows:
....................................
connected
$ log in
$
.....................................
What are the steps required to get to this window? Any hints or guidance would be appreciated.
Thank you,
RP
I did not exactly replicate what is there in the video but I hope a sample like below should do it.
This is how I configured the server part.
var app = require('express').createServer(),
io = require('socket.io').listen(app),
sys = require('util'),
exec = require('child_process').exec;
app.listen(4990);
io.sockets.on('connection', function(socket) {
socket.on('console', function(command, callBack) {
// client sends {command: 'ls -al'}
function puts(error, stdout, stderr) {
socket.emit('commandresult', stdout);
}
exec(command.command, puts);
});
});
I hope you can do the client part.
Hope this helps
Looking for a simple script that would run on windows 2003 server that would basically send me an email. What I plan to do us the windows services auto recovery manager to trigger the script.
I did find a reference to how I can trigger the use of this script: How to monitor Windows services
But I need some help on writing an send email script that would work for windows platform. I'm not sure what language would be best for this. thanks.
One simple way would be to use javascript (or VBscript). If you google for "Server.CreateObject("CDO.Message")" you will find more examples.
Put the code below in a file with extension: ".js", for example email.js
To call use "cscript email.js" on the command line. Replace server name and emails with valid values.
Windows 2003 should have CDO installed. The script used to work on windows XP and server 2003. This example uses smtp server over the network but there are other options too.
Powershell is probably available for server 2003 .. so it could be another option.
============================== code ==============================
function sendMail ( strFrom, strTo, strSubject, strMessage ) {
try {
objMail = Server.CreateObject("CDO.Message");
objConfig = Server.CreateObject("CDO.Configuration");
objFields = objConfig.Fields;
with (objFields) {
Item("http://schemas.microsoft.com/cdo/configuration/sendusing")= 2;
Item("http://schemas.microsoft.com/cdo/configuration/smtpserver")= "xxxxsmtp.xxxserver.xxorg";
Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport")= 25;
Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 30;
Update();
}
with (objMail) {
Configuration = objConfig;
To = strTo; //"\"User\" ,"\"AnotherUser\" ;"
From = strFrom;
Subject = strSubject;
TextBody = strMessage;
//if we need to send an attachement
//AddAttachment("D:\\test.doc");
Send();
}
}
catch(e) {
WScript.Echo(e.message);
return false;
}
delete objFields;
delete objConfig;
delete objMail;
return true;
}
//WScript.Echo('qqq');
sendMail( 'from#xxxxxx.com', 'to#yyy.com' , 'test', 'msg');
I want to launch a browser and load a web page using Java's Runtime exec. The exact call looks like this:
String[] explorer = {"C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE",
"-noframemerging",
"C:\\ ... path containing unicode chars ... \\Main.html"};
Runtime.getRuntime().exec(explorer);
In my case, the path contains "\u65E5\u672C\u8A9E", the characters 日本語.
Apparently it's a java bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4947220
My question is: is there a viable workaround that can be done solely using Java? It appears that it is possible to write a JNI library for this, but I'd like to avoid that if possible. I have tried URI-encoding the path as ascii and writing the commands to a batch file, without success.
At the mentioned Java bug page you will find a workaround that is reported to work using ProcessBuilder and wrapping the parameters in environment variables. Here is the source code from Parag Thakur:
String[] cmd = new String[]{"yourcmd.exe", "Japanese CLI argument: \ufeff\u30cb\u30e5\u30fc\u30b9"};
Map<String, String> newEnv = new HashMap<String, String>();
newEnv.putAll(System.getenv());
String[] i18n = new String[cmd.length + 2];
i18n[0] = "cmd";
i18n[1] = "/C";
i18n[2] = cmd[0];
for (int counter = 1; counter < cmd.length; counter++)
{
String envName = "JENV_" + counter;
i18n[counter + 2] = "%" + envName + "%";
newEnv.put(envName, cmd[counter]);
}
cmd = i18n;
ProcessBuilder pb = new ProcessBuilder(cmd);
Map<String, String> env = pb.environment();
env.putAll(newEnv);
final Process p = pb.start();
Create a .bat/.sh file. Write your commands to that file and execute it. Make sure that you have changed the code page to unicode in case of windows(chcp 65001).
For example to execute the below command in windows:
String[] command ={"C:\\aconex\\学校\\mysql\\bin\\mysql", "-esource", "大村箕島a\\data.sql"};
Create a temp file called temp.bat and execute with the Runtime.getRuntime().exec
temp.bat
chcp 65001
C:\aconex\学校\mysql\bin\mysql -esource 大村箕島a\data.sql
These are the two solutions I considered, each of which are more or less workarounds:
Create a temp html redirect file which will redirect the browser to the proper page.
Note that IE will expect unencoded unicode for local files, while other browsers may accept only uri-encoded file paths
Use the short filename for the windows file. It won't contain unicode characters.
We've been using a JNI to start processes from Java for years. Neither Runtime.exec or ProcessBuilder will work, and it seems unlikely that they will fix this, given how long it's been already.
However, you should be able to work around the issue by using the input stream, a socket, or environment variables to pass parameters. If you don't have direct control over the executable, you'll have to make a wrapper.
You could use JNA. With version 3.3.0 or later call CreateProcess:
WinBase.PROCESS_INFORMATION.ByReference processInfo =
new WinBase.PROCESS_INFORMATION.ByReference();
WinBase.STARTUPINFO startupInfo = new WinBase.STARTUPINFO();
String command = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE " +
"-noframemerging \"C:\\\u65E5\u672C\u8A9E\\Main.html\"";
if (!Kernel32.INSTANCE.CreateProcess(
null, // Application name, not needed if supplied in command line
command, // Command line
null, // Process security attributes
null, // Thread security attributes
true, // Inherit handles
0, // Creation flags
null, // Environment
null, // Directory
startupInfo,
processInfo))
{
throw new IllegalStateException("Error creating process. Last error: " +
Kernel32.INSTANCE.GetLastError());
}
// The CreateProcess documentation indicates that it is very important to
// close the returned handles
Kernel32.INSTANCE.CloseHandle(processInfo.hThread);
Kernel32.INSTANCE.CloseHandle(processInfo.hProcess);
long pid = processInfo.dwProcessId.longValue();
Redirecting output from the child process is a bit harder but not impossible.
I think you can use Apache Commons Exec library or ProcessBuilder to give a try;)