ping a server in lua - lua

Just curious to see if there is a lua way to ping a server without using os.execute. The purpose being to see if a server is up.
I checked the lua sockets library but I don't think ICMP is supported? Any idea?

You can use io.popen() to execute ping commands.
e.g.,
local handler = io.popen("ping -c 3 -i 0.5 10.10.10.10")
local response = handler:read("*a")
print(response)

To the best of my knowledge, no, you can't send ICMP raw packets without root access. That's not a Lua limitation, it's an OS restriction.
To get root access, the best way is to have a small well tested program that's SUID root rather than changing your entire application with Lua to be SUID root. This means you'll end up using os.execute(). And rather than writing your own program, the OS provided ping seems to be a nice command for solving your issue.
I agree it's not ideal (especially since this creates OS specific code to handle the various ping commands). But without a SUID function call, I don't think there's any better way.

Related

AGI in Docker container does not receive DTMF input

I am writing an application using Asterisk-Java. It is designed to run on a server that also runs Asterisk. So far, so good.
My application, that originates calls (using the AMI) and that manages user input (using Asterisk-Java's FastAGI and an embedded AgiServer) works great on both my development server and the production server.
For deployment purposes, I am now asked to create a Docker container that would pack up Asterisk and my application, so that it could be easily deployed to other places without having to go through installations and configurations.
The thing is, my application does not behave the same way in the Docker container: on the development / production servers, using the getData function, I can get a DTMF code; on the Docker container, getData seems to never receive the DTMF data from Asterisk (I can stream a file, but the function eventually times out, which means it did not get anything).
I first though of an unexposed port, but since this communication problem seems to be between the AGI Server and Asterisk, which are both running in the container, I find it hard to believe.
I have no other idea, please suggest.
Check out the dtmfmode Parameter for your SIP-Peer...
If your are using RFC2833 (DTMF via RTP), unexposed media ports could very well be the reason.
You could try to optimize your port settings (could be a lot of ports!).
Or try to use DMTF via SIP-Info as an alternative.
But that wouldnt fix any media problems...

Do I need to implement terminal emulation if I have a ptty?

If I have a pseudo terminal given to me (from say a pty-req (http://www.ietf.org/rfc/rfc4254.txt)) do I need to emulate the data coming back, or is that already emulated data?
It depends on the level of emulation you're talking about. The pty uses the remote server's terminal driver, so options controlled by stty (and the analogous system calls) will be emulated there.
But you're running an application that sends terminal control escape sequences, you'll need to handle that locally.

Erlang Port Data Transfer Length

I am trying to evaluate php code through erlang using erlang ports. The problem is when Data to be evaluated is bigger then I am getting parse error from php. But if data is smaller then I am getting the correct output. I think when the Data length is bigger erlang is truncating the data before it is being sent to php for evaluation. Is there any limit on data length which can be sent or received on erlang port. Or is this error due to some other reason ?
I am using open_port(PortName, PortSettings) to open a new port and in PortSettings I am setting [{packet,4},exit_status] as my port options.
The {packet, 4} tuple says the program launched to handle the other end of the port expects data in a 4-byte length-prefixed form. I don't see anything in the docs for the php(1) program that says it knows how to deal with such data. Probably the only reason it works for short inputs is that the length prefix looks kinda like ASCII if you squint, as long as the data you're sending is under 127 bytes. As soon as you go over that, PHP is probably running into a UTF-8 decoding error.
I'm pretty sure you want to say spawn here instead. This gets you standard Unix-like pipe interaction: data sent down the port goes to stdin on the launched process, and anything it sends to stdout comes back to your Erlang process.
The only problem doing it this way is that it re-launches php(1) on each transaction. This may seem expensive, but it's not too bad on any Unix type system, due to the relative efficiency of the fork(2) system call. If you're on Windows or you've benchmarked this and found that you really do need to build a FastCGI like system, you may be out of luck. There seems to be no libphp to embed PHP into a program you write to deal with packetized input, and no way to run php(1) in a way that lets it stay active on the other end of a port. You might be better off switching to a native Erlang templating system.
Also, note that the exit_status atom passed to open_port() does nothing unless you use spawn.

Detected if a machine is connected/Available?

How can I detect if a machine is connected/available in the present network.
It has several uses of course, but my main concern here is that my application uses resources located in specific machines and if they are not available it will not even attempt the connection and will use local resources.
you can try making a ping to the machine. check this article Making a PING with Delphi and the WMI.
ICMP echo request (PING) will tell you if the machine is up and reachable on the network. It will not tell you if the service you want to connect to is available on the machine (up and running).
Best bet would probably be to just attempt the connection and fall back to local resources if the connection fails.
Just try to use the resource and if you get an error use the local resource instead. The strategy you are trying to implement suffers from several problems including timing windows between the test and the use, during which the resource may become unavailable, and also doesn't actually test the resource for availability, only some lower-order thing like a TCP port or the ICMP echo part of the stack. In general the best way to detect whether a resource is available is just to try to use it, and recover from the failures. You have to write code to handle those failures anyway, why do it all twice?
A different strategy than trying to connect: let the server tell the clients if the services are still available, by sending UDP Broadcast or some kind of heartbeat signal over middleware (pipes?), which the clients listens to - a publish/subscribe communication model.

How do I send an ARP packet from a C program?

I'm working on an embedded linux system in C, I'm looking for the source code to the equivalet of SendARP in Windows. Any pointers?
Take a look at arping. The quick and dirty way of sending an arp would be to do:
foo = system("/somepath/arping somehost");
But a look through the arping source should be able to give you a better solution.
For the all-out solution though, you can construct your own by hand and use either a raw socket or libpcap to send it.
btw. If all you're trying to do is force an arp to be sent (but necessarily from you) you could achieve that by deleting any arp entry that you already have for your host. The next access to that address will require an arp to be sent.
eg. /usr/sbin/arp -d destination_host_ip
This may be of interest: http://cvs.linux-ha.org/viewcvs/viewcvs.cgi/linux-ha/resources/heartbeat/SendArp.in?rev=1.4
It is an implmenetation in a Bourne Shell script.
I've never seen anything specifically for ARP, but I think you can send any kind of packet you want using libpcap and the appropriate RFCs.

Resources