B4J Question Shell - Permission denied

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

I am running my B4J app on a Linux board.

I am trying to run a script which will allow me to change the devices IP address to something else.

I am using the following code (not sure if this is the best way or not)..

B4X:
Sub AppStart (Args() As String)
 
    Dim Writer As TextWriter
   
     Writer.Initialize(File.OpenOutput(File.DirApp, "run.sh", False))

        Writer.WriteLine("sudo ifconfig eth0 192.168.0.34")
        Writer.WriteLine("sudo ifconfig eth0 netmask 255.255.255.0")
        Writer.WriteLine("sudo ifconfig eth0 broadcast 192.168.0.255")

    Writer.Close
   
    Dim shl As Shell
    shl.Initialize("shl", "./run.sh", Null)
    shl.WorkingDirectory = File.DirApp
    shl.Run(-1)
   
    Log("done!")
   
    StartMessageLoop
End Sub

I am connected using the B4J-Bridge, and when I run the above I get the following error in the IDE log:

Error: org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "./run.sh" (in directory "/home/Aaron/tempjars"): error=13, Permission denied)

Any ideas on what I have done wrong ?

I am guessing it's some permissions error somewhere, but not sure where and how to allow it.

I am hoping the above will allow me to change the IP address of the server to something else. If it doesn't anyone know how to do it ?
 

aaronk

Well-Known Member
Licensed User
Longtime User
1. Try to run b4j-bridge with sudo:
sudo java -jar b4j-bridge
I installed the bridge using:
wget www.b4x.com/b4j/files/b4j-bridge.jar
then I run it using: sudo java -jar b4j-bridge.jar

When I try and run it with sudo java -jar b4j-bridge it comes up saying,
Error: Unable to access jarfile b4j-bridge

2. Does the script run properly if it is empty?
I tried running it with the following code, but get the same error.

B4X:
Sub AppStart (Args() As String)

    Dim Writer As TextWriter
  
     Writer.Initialize(File.OpenOutput(File.DirApp, "runfile.sh", False))

      '  Writer.WriteLine("sudo ifconfig eth0 192.168.0.34")
       ' Writer.WriteLine("sudo ifconfig eth0 netmask 255.255.255.0")
        'Writer.WriteLine("sudo ifconfig eth0 broadcast 192.168.0.255")

    Writer.Close
  
    Dim shl As Shell
    shl.Initialize("shl", "./runfile.sh", Null)
    shl.WorkingDirectory = File.DirApp
    shl.Run(-1)
  
    Log("done!")
  
    StartMessageLoop
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It should be: sudo java -jar b4j-bridge.jar
This code works:
B4X:
Sub AppStart (Args() As String)
   File.WriteString(File.DirApp, "1.sh", $"
ls
"$)
   Dim shl As Shell
   shl.Initialize("shl", "bash", Array("1.sh"))
   shl.Run(-1)
   StartMessageLoop
End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   Log(Success)
   Log(StdOut)
   Log(StdErr)
End Sub
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
thanks heaps.. that worked for me as well.

In case anyone else comes across this and wants to know.. the following allowed me to change the IP address of my linux server.

Only catch is once it changes the IP address you then need to connect to the new IP.

B4X:
Sub AppStart (Args() As String)
   File.WriteString(File.DirApp, "1.sh", $"
ifconfig eth0 192.168.0.34
ifconfig eth0 netmask 255.255.255.0
ifconfig eth0 broadcast 192.168.0.255
"$)
   Dim shl As Shell
   shl.Initialize("shl", "bash", Array("1.sh"))
   shl.Run(-1)
   StartMessageLoop
End Sub

Sub shl_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
   Log(Success)
   Log(StdOut)
   Log(StdErr)
End Sub
 
Upvote 0
Top