I have a node.js program running as a service on my windows PC which I use to open files. The only way I have found to make this work is to use node.js to open PSExec, as node doesn't appear to have the ability to open a program as another user (in windows).
The code below works, however it is somewhat insecure, as the username and password can be discovered by watching process monitor, or even the command line column in task manager. I don't store the password in the source by the way, just put it there to keep the example simple.
var EventLogger = require('node-windows').EventLogger;
var log = new EventLogger('File Launcher starting');
var exec = require('child_process').exec;
var userName = "mike";
var password= "mikesPassword";
var fullPath= "C:\folder\file.xls";
exec("PSExec.exe -accepteula -h -d -u "+userName+" -p "+password+" -i 1 C:\\WINDOWS\\SYSTEM32\\CMD.EXE /c start \"\" \""+fullPath+"\"",{cwd: process.cwd}, function(error, stdout, stderror) {
if(error){
log.error(stderror.replace('\n','').replace('\r',''));
}
if(stdout){
log.info(stdout.replace('\n','').replace('\r',''));
}
Is there some other way I can get node.js to launch a file as another user without exposing the username and password?
I attempted to do it using powershell, but it seems SYSTEM is not allowed to launch files as other users.
Related
I have the following:
\Config::set('remote.connections.runtime.host', $server->server_ip);
$commands = [
'tmux'
];
\SSH::run($commands, function($line) {
echo $line.PHP_EOL;
});
dd();
This outputs the following error:
open terminal failed: not a terminal
Is it possible to create a tmux session this way?
There's nothing you can do to make this work with Laravel SSH. However, Laravel's SSH component uses phpseclib SSH internally which is more flexible when used directly.
Now I'm assuming you're trying to create a tmux session to start some process that you want to continue running in the background after the SSH connection has ended. The good part is that since phpseclib is a dependency of Laravel SSH it's already there so there's no need to install it. I'm not very familiar with tmux and it's options, but I can offer a solution that uses screen instead:
use \phpseclib\Net\SSH2;
use \phpseclib\File\ANSI;
$ssh = new SSH2('host');
$ansi = new ANSI();
if ($ssh->login('username', 'password')) {
// The command below will start a screen
// session and automatically detach it
$ssh->write("screen -m -d -S processes top\n");
// You can include the lines below to see the
// output of the write command converted to HTML
$ansi->appendString($ssh->read());
echo $ansi->getScreen();
}
This creates a new automatically detached screen session named processes and runs the top command in it. To connect to the session you can simply run this in your console:
ssh username#host -t "screen -r processes"
This will reattach the processes session so you can see what's going on. Of course you could adapt this to use tmux if that's you're preference.
I am looking to persist a conversation with a remote server using ssh.net
What i am doing is connecting to a host, sending a command like change directory... to some directory besides root. Store the results value off as a global.
Then i am sending another command via RunCommand() to check the current directory...
What is happening is, i am getting the root directory, not the directory i just changed to in the initial run command.
What it seems is happening is, while the connection to the server has remained open i have somehow reset the terminal session thereby losing the conversation i was having with the server.
Does anyone know how to persist a conversation with a remote server using ssh.net so i can send multiple commands and have the state persist?
E.g. command 1 = cd/somedir
command 2 = pwd and the result of command 2 to is /somedir
E.g. command 1 = cd/somedir command 2 = pwd and the result of command 2 to is /somedir
Your example seems just fine. But I think, you are expecting to change directory and run the second command in that directory.
Server connection is an ssh tunnel to the server, it doesn't start a shell. RunCommand() creates a shell and runs a command, the second RunCommand also creates a new shell and runs the command, so change directory does not persist between commands.
After establishing connection, use a ShellStream to create a shell from which you can send and receive interactive commands from.
A sample from codeplex:
string command = "your command";
using (var ssh = new SshClient(connectionInfo))
{
ssh.Connect();
string reply = String.Empty;
try
{
using (var shellStream = ssh.CreateShellStream("dumb", 0, 0, 0, 0, BUFFERSIZE))
{
// Wait for promt for 10 seconds, if time expires, exception is thrown
reply = shellStream.Expect(new Regex(#":.*>"), new TimeSpan(0, 0, 10));
shellStream.WriteLine(command);
// Wait for Read for 10 seconds, if time expires, exception is thrown
string result = shellStream.ReadLine(new TimeSpan(0, 0, 10));
}
}
catch
{
// Do something
}
}
I have a command which takes 2 arguments.
When I run the command manually, I do this way:
cmd -i xyz.dat
hit enter
enter password in the prompt
hit enter
confirm password
My script needs to do the above operations without expecting user to enter password. I can hardcode the passwords in the file, but need a way to run this command successfully when I execute the shell script.
As on Feb 7th,
I have expect installed on my AIX. When I type expect at the command prompt, the prompt changes to expect 1.1> OS is 64 bit AIX
I have followed the instructions mentioned in the below comment, but I keep getting error - could not execute the command; no such file or directory"? I am able to manually run this command from same directory I am running the script. Besides that I have given the complete path of the command and the
file.
I am pasting another program I tried to su with root password as below: i get the same error message when I run the test program. I doubt if this is something related to quotes.
#!/bin/bash
set timeout 20
spawn "su"
expect "Password:" { send:"temp123\r" }
interact
Can someone please help me fix this error?
Sounds like you want to use expect. Here is a page with some examples.
So for your command you would want something like:
#!/usr/bin/expect
set timeout 20
spawn "cmd -i xyz.dat"
expect "<your password prompt" { send "<your password>\r" }
expect "<your password confirmation prompt" { send "<your password>\r" }
interact
I am trying to connect to an SFTP server via software called WinSCP which is a Secure FTP client, I can script it to work by sending keys and doing it in a psuedo unattended mode, but the Server has to have a user logged in to send the keys, I need WinSCP to logon and transfer the files like a service where the console doesn't launch.
I have tried following the Tutorials on the
WinSCP website (for automated/unattended transfers but it gives me errors: Cannot created object, or cannot find library (from the dll file that I have associated with the COM)
The error I get when I run the following code is:
line 13, Char: 2
Could not created object named "WinSCP.SessionOptions"
Code: 80040154
Source: Wscipt.CreateObject
I should also probably mention that I get a similar error about line 21 or 22 about creating the Session object, when I remove the code on line 13 to see if it was the only issue
<job>
<reference object="WinSCP.Session"/>
<script language="VBScript">
Option Explicit
' Setup session options
Dim sessionOptions
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
With sessionOptions
.Protocol = Protocol_Sftp
.HostName = "host"
.UserName = "username"
.Password = "password"
End With
Dim session
Set session = WScript.CreateObject("WinSCP.Session")
' Connect and Get
session.Open sessionOptions
session.GetFiles("/Fromn/*.*", "C:\testFilesFrom").Check()
' Disconnect, clean up
session.Dispose
</script>
</job>
Has anyone had any experience scripting this kind of job where the server is not logged on and the script can run an SFTP session? Is there somethign I am doing wrong or is this not possible?
WinSCP was made for interactive use. Use PSCP for non-interactive jobs. And I'd strongly recommend using public key authentication instead of password authentication.
One of the answers on a previous question mentioned that I can use curl to fetch a url; this can be done in Thread.new or in Process.spawn. But it seems that in either case, on Windows, I get a small command-prompt window appearing while curl is going out to the network.
I am invoking curl like this:
`curl "#{url}"`
Is there any way to hide the window so that it doesn't appear? Not only does it grab the focus away from the game (freezing it), but if I make frequent calls, it will be extremely annoying to the end user.
After some experimenting i got it working, no window when doubleclicking on the script, i use WMI from ruby, you just have to alter the path to curl.
IMPORTANT: save it with the extension .rbw and make sure that rubyw.exe is associated with that extenstion.
#hidden_curl.rbw
require 'win32ole'
HIDDEN_WINDOW = 0
cmd = '"C:\\Program Files\\curl\\curl.exe" --output c:\\test2.txt "http://stackoverflow.com/questions/10869789/hiding-curl-window-on-windows"'
objStartup = WIN32OLE.connect("winmgmts:\\\\.\\root\\cimv2:Win32_ProcessStartup")
objConfig = objStartup.SpawnInstance_
objConfig.ShowWindow = HIDDEN_WINDOW
objProcess = WIN32OLE.connect("winmgmts:root\\cimv2:Win32_Process")
errReturn = objProcess.Create(cmd, nil, objConfig, nil)
Hope this works on your system.
You could try passing the -s option to curl which will make it run in silent mode.
Something like
`curl -s "#{url}"`
I am guessing this will not show up the console.
I think you problem is that you run the command in a subshell and the subshell in windows is CMD which is attached to a window.
I don't have a ruby to test the following but you could try :
IO#popen
Runs the specified command string as a subprocess;
The subprocess‘s standard input and output will be connected to the returned IO object.
=> http://ruby-doc.org/core-1.8.6/IO.html if IO is available to you
you could also try to run
start /B curl -s "#{url}" instead of just curl see http://www.computerhope.com/starthlp.htm for start specs.
This solution doesn't pop up a window on my W7 system
cmd = '"C:\Program Files\curl\curl.exe" --silent --output c:\test.txt "http://stackoverflow.com/questions/10869789/hiding-curl-window-on-windows"'
IO.popen(cmd)
or without creating a file
cmd = '"C:\Program Files\curl\curl.exe" --silent "http://stackoverflow.com/questions/10869789/hiding-curl-window-on-windows"'
IO.popen(cmd, "w+") { |io| puts io.readlines }