Modify hosts file from AutoIT script executed from Selenium - java

I am trying to change out my hosts file based on whichever environment I run my script in. It looks like what I'm needing to do is run the commands as admin (elevated privileges), I just cannot seem to find out how to do that in AutoIT.
Here is an example of the call from Selenium:
Runtime.getRuntime().exec("C:\HostSwitcher.exe " + C:\Scripts + " " + "QA2");
Here is my code in AutoIT:
$filePath = $CmdLine[1]
$env = $CmdLine[2]
Run(#comspec & " /c DEL %WINDIR%\SYSTEM32\DRIVERS\ETC\HOSTS")
if $env = "Dev" then
Run(#comspec & " /c COPY "&$filePath&"hostFiles\DevHost.txt %WINDIR%\SYSTEM32\DRIVERS\ETC\hosts")
endif
if $env = "QA2" then
Run(#comspec & " /c COPY "&$filePath&"hostFiles\QA2Host.txt %WINDIR%\SYSTEM32\DRIVERS\ETC\hosts")
endif
How would I tell AutoIT to run as admin?

As a starting note, your account should have elevated privileges.
As far as I know you have options here:
the most desired and safe one
Run your Java application (IntelliJ, Eclipse) as Administrator. This way, any processes spawn or executed by it would be ran with elevated privileges (this works as a chain reaction in Windows and not only).
Run it with elevated privileges
Runtime.getRuntime().exec("runas /profile /user:Administrator \"C:\HostSwitcher.exe " + C:\Scripts + " " + "QA2");

Related

Problem in executing command on AIX through Java

I am trying to run following command on AIX to backup files occasionally.
tar cvf - /tmp/emflextmp/* |gzip > /APP/emflex/scada/Db/backup/dbBackup_31_Aug_2019__15_51_04.tar.gz
If I run this command on terminal through same login it works perfectly. Now I want to do it through the java app. My code looks like this:
Process process = Runtime.getRuntime().exec("tar cvf - "+tempFolder+"* |gzip > " + strBackupFolder + File.separator + "dbBackup_" + new SimpleDateFormat("dd_MMM_yyyy__HH_mm_ss").format(new Date()) + ".tar.gz" );
int exitVal = process.waitFor();
I get exit code 2 here.
From logs the command looks ok
2019-08-31 15:51:04:565 [INFO.] [Thread-16:ID=76]:[GarbageCollector.java:332] Running H2 Database Backup Command On AIX =[tar cvf - /tmp/emflextmp/* |gzip > /APP/emflex/scada/Db/backup/dbBackup_31_Aug_2019__15_51_04.tar.gz]
None of the output files are created. What is the problem here?

Unable to get Eclipse to recognize binaries using ProcessBuilder located on $PATH on mac os x

I have just switched over to working on a mac and I am trying to determine why I am unable to get Eclipse to recognize the binary I am trying to run via a ProcessBuilder.
I have tried to run it both as a Java Application in Eclipse and as a TestNG test.
If I compile the class with java and run it directly from the command line it will work but not through Eclipse which leads me to believe the configuration for the $PATH is not setup correctly in my TestNG configuration.
Question
I am sure this is a configuration issue within Eclipse but after searching for a day and coming up short I wanted to post for some help. I have tried to set $PATH on the configuration but it does not seem to work.
Thank you
Update /Answer
It turned out that the PATH I had set on the shell shown below was not the same that Java had which I checked using the code below. After verifying that I then added the proper path to my environment on the ProcessBuilder and executed the script as shown in the answer.
Map<String, String> env = processBuilder.environment();
for (String key : env.keySet())
System.out.println(key + ": " + env.get(key));
Map<String, String> envs = processBuilder.environment();
System.out.println("Path " + envs.get("PATH"));
envs.put("PATH", "/usr/local/bin");
System.out.println("PATH " + envs.get("PATH"));
Code
File logsDir = new File(logDirectory);
if (!logsDir.exists()) {
logsDir.mkdirs();
}
// run process directly
ProcessBuilder processBuilder = new ProcessBuilder();
processBuilder.command("appium");
processBuilder.redirectError(new File(logsDir, "appiumError.txt"));
processBuilder.redirectOutput(new File(logsDir, "appiumOutput.txt"));
process = processBuilder.start();
Output (it cannot find node to run appium hence the No such file or directory)
Caused by: java.io.IOException: Cannot run program "appium": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at AppiumService.startAppium(AppiumService.java:77)
Path (The bin for node and appium is in /usr/local/bin)
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:
/usr/local/opt/ant/bin:/usr/local/opt/maven/bin:
/usr/local/opt/gradle/bin
The PATH variable of the java-process might be explicitly set by Eclipse not containing the paths you need. You can call the command using the absolute path to the corresponding directory or you might try using a shell to start the process by creating the process with
processBuilder.command("/bin/sh", "-c", "appium");

Running shell script through Process in Java

I'm trying to move a file from Java Application Server to DB Server. For that I'm using a shell script. To run the shell script, I'm using Process in Java File. While trying as a stand alone java(class file) in application server using putty, it's moving to DB server. But while trying from application, its not working and process.waitFor() is returning 1.. Need Help.???
Code:
Process p = Runtime.getRuntime().exec("sh "+asyncFilePath+"/ManualAdjFileTransfer.sh "+asyncFilePath+ " "+destPath+" "+ destUserId + " " + destIp + " " + asyncFilePath + " ManAdj_File_Transfer.Log");
p.waitFor()
It could be a problem of permission:
Process p = Runtime.getRuntime().exec("chmod u+x "+yourscript.sh);
Related Question: Write an executable .sh file with Java for OSX

'ant' is not recognized as an internal or external command when executed in Java

Im developing a program where a batch file will execute Selenium TestNG in ant when run. That batch file works well if i open it by double clicking (which means there is nothing wrong in ANT). But when i try to open this file using java code with the following code it opens in cmd and gives the error below the code. Even though it works with double click i have to make it work with Java code. So this question is different from questions asked before. SO please dnt tell that the question exists. Thanks in advance
Runtime.getRuntime()
.exec("cmd /c start C:" + File.separator
+ "Users"
+ File.separator
+ "nvithushan"
+ File.separator
+ "Desktop"
+ File.separator
+ "HSBC"
+ File.separator
+ "gen"
+ File.separator
+ "seleniumwebdriver"
+ File.separator
+ "HSBC_Demo"
+ File.separator
+ "run.bat");
running...
'ant' is not recognized as an internal or external command,
operable program or batch file.
I set ANT in build path of eclipse and added this code. It worked for me.
Runtime.getRuntime()
.exec("cmd /c start run.bat", null, new File("C:\\Users\\nvithushan\\Desktop\\HSBC\\gen\\seleniumwebdriver\\HSBC_Demo"));

Java exec Error: Fatal system Error 1020005

I have a problem with a Java program. I uses a tool "ugpc" to get the assembly structure and write it in a textfile. When i start the program under netbeans it writes these files without problem. but when i compile it to a jar file and run this jar, i get the error "Error: Fatal system Error 1020005" written in the proc.getErrorStream(); and no .txt file ist written in the ouput. Also in the ErrorStream appears that the textfile can not be found, but he should write it and not open it. My PC is new installed and before that formatting the tool was running good. I use Java 7. When I manually start the command via cmd it works also fine. We startet the tool under another user account and the same error appears.
Process proc = Runtime.getRuntime().exec("cmd /c " + "c:\\ugs\\nx6\\UGII\\ugpc -a -s2 " +"S:\\Geraete\\L\\0154\\E2\\6169-001000-201_Rev-E2_DWG.prt" + " > " + "S:\\LAUF5\\1\\6169-001000-201_Rev-E2_DWG.txt");
proc.waitFor();

Categories

Resources