How to execute a batch file on a remote system - java

I have a java web app running on a server. I will click a button on the UI on the server It should invoke the batch file on the client machine and execute it on the client. Please suggest me how to go on doing it?

If you're talking about a web app and it involves a browser as a client, then you cannot invoke a script on every client machine (I mean, you can do it in Windows, but not Linux).
Javascript is a client scripting language and runs in a sand-boxed environment often inside a web browser which prevents it from accessing resources on the computer.
Nevertheless in Windows, you probably could do this:
var cmdshell = WScript.CreateObject("WScript.Shell");
cmdshell.Run("the command to run a batch script goes here");
Check this for more details.
But in Linux, I haven't seen a way to do it since the environment is different altogether.

Related

Windows logged user informations

I want to create an application in java that is monitoring logged users activities(create, delete, update folders/files).
The problem is that I didn't found how to get the OS of the logged user (java app is running on a windows server and users have windows on their machine, I want to know if there is a way to get the windows version of the logged users).
BRs,
Mihai
You would use JNI and call a native (windows-specific DLL) method to get the information. You would have to create this DLL yourself in (likely in C/C++)
System.getProperty("os.version")
If it's a web application you can use user-agent header. It can change easily, but worth to try. Check https://stackoverflow.com/a/1328393/5684110.
You're asking for a Windows-specific feature. I doubt Java would support that, so you will need a native module (written in C/C++ or something) to read that information and pass it into your Java application via JNI or a local socket connection. Maybe you could poll that data from Active Directory.
Another idea is, you could get the info through another Java app running on the client PC at startup. That way you would be able to monitor changes in the file system and some basic system properties like OS name and version (see Mustafa's answer). The app would be silently downloaded into the workstation and run automatically through Active Directory, sending the data to your server app via socket, web or a webservice.
Hope this helps you.

open an exe which is on remote machine using Java

open an exe which is on remote machine and not on my local machine which has hub and node using java selenium,I do not know if that's possible or not.I am looking for a slution.
you can use psexec to do it.
Download from here:
https://technet.microsoft.com/en-us/sysinternals/pxexec.aspx
I would suggest that on the machine where you want to launch your exe you will run some Webserver running some web application that also could be written on java or any other language. That web App should expose an API (REST API is one of the popular options) that woulsd receive request and upon that request will launch your exe. Now on your remote machine your java app (or any other app or just a brouser) will need to send a regular HTTP request to your webserver, and your web app will do the launch

Java/Jmeter connect timeout but not on browser (Not a JMeter issue)

In windows 2003 using JDK 1.7_21
So I setup Jemeter on my desktop to do an HTTP request to a specific URL and it works fine. But when I try from another machine I get a connect timeout.
The URL works fine in the I.E browser and I can also ping the domain from the command line. But for wahat ever reason the JVM doesn't want to connect.
In fact this is to test a sample HTTPClient I wrote. it's not the code because the code works off the desktop but on the server environment the JVM wont connect, proof JMeter wont either. but the browser and pings works.
I suspect java is getting it's network settings from somewhere different??? Proxy???
Well.. is IE/Windows using a proxy? You can check by going to to Internet Options > Connections > LAN Settings
If it is, you can set some JVM properties so that your Java application uses a proxy, there a couple ways of doing that, one way is through the command line when you execute it, you can use something similar to:
java -Dhttp.proxyHost=your.proxy -Dhttp.proxyPort=your.proxy.port -jar your.jar
or
java -Dhttp.proxyHost=your.proxy -Dhttp.proxyPort=your.proxy.port YourMainClass

JSCH Java applet

I have embedded a JSCH SSH Java applet in a web page and need to know if it's possible to run a script (of any language like PHP) to automate logging in and running commands. I have heard of expect4j and java robot but cannot see any way to implement it. Keep in mind, I'm not great with Java so I don't know everything about it. Any help is appreciated.
JSch is an SSH client library, and by itself only allows programmatically steered connections to another server. The user interaction has to be build around it by users of the library.
The JCTerm applet provided on the website also contains a terminal emulator in form of a Java GUI. If you only want to automatically execute some command (and maybe show its output in the web page), you could do everything on the server side, and don't need the applet with its terminal emulator. (You would need either some PHP-Java bridge on the server side or some Java-enabled webserver with a Servlet or similar, though.)
(If the web server would be the same machine as the server you'll run the command on you wouldn't even need the SSH connection, but could execute the stuff directly.)
If the server can't do anything (i.e. a "static server"), an applet is the way to go, yes. You can either modify JCTerm or create a new applet from scratch (using JCTerm's connection code as an example on how to connect to to the server).
If you don't have to fear any malicious users in your LAN (i.e. between web server and user, the SSH server doesn't matter), you can embedd the password (or preferably a private key for public-key authentication) into the applet's jar file, and pass it to the library for connection. (You should also include the server's public key for easier checking.)
Provide the command(s) to a ChannelExec (instead of a ChannelShell), this makes it easier to provide input (if necessary) and capture the output. Pipe the output in a text area, or simply use a green/red label saying if the command was successfully executed.
(I might have a look at this in the next days and try to do it. No promise, though.)

how to automatically and periodically upload a file from a windows machine to a unix server?

i have 2 csv files on my windows machine that i want to transfer to a unix server every hour (because the 2 files are updated every 30min).
so i search on the web and i found this possibility: to ftp the files from windows to unix using a batch and task scheduler.
i want to know if it's possible to do it from the unix server with a cron job or with java timer and not from the windows machine and if if there's other easier solutions.
thanks for your help
The main problem you would have is in allowing the Unix machine to access a Windows file.
You didn't specify what kind of networking configuration you use and what version of Windows it is, as some later versions provide facilities for file sharing across platforms.
In the worst case, you could set up an FTP (or SFTP) server on your windows machine, share a specific directory in which you will put the file, and then write a script on the UNIX that will download the file from the windows machine. A variation on this is to install a web server on the windows machine, and have the script on the unix make an HTTP request for the file.
You could install a webserver like http://www.aprelium.com/abyssws/
It seems to be free and should be not that hard to set up. Afterwards put your files in the htdocs-folder and configure your Vista-firewall to let requests on port 80 pass.
Then you can access your files via
http://IPofYourWindowsComputer/yourfile.zip
If you want to access your windows machine from outside your local network you'd have to setup your router to forward port 80 to your windows machine. Otherwise the machine from outside will be blocked by the router.
PS: Samba (SMB) just works within the same LAN. If you are you could create a shared folder and put the files in there and access them via smbclient or mount.
PPS: Maybe your question is better placed # http://serverfault.com

Categories

Resources