参照asterisk-java提供的示例,用以下方法执行了CLI命令:
import java.io.IOException;
import java.util.List;
import org.asteriskjava.manager.AuthenticationFailedException;
import org.asteriskjava.manager.ManagerConnection;
import org.asteriskjava.manager.ManagerConnectionFactory;
import org.asteriskjava.manager.TimeoutException;
import org.asteriskjava.manager.action.CommandAction;
import org.asteriskjava.manager.response.CommandResponse;
public class HelloManager {
private ManagerConnection connection;
public HelloManager() throws IOException {
ManagerConnectionFactory factory = new ManagerConnectionFactory("10.10.1.101", "manager", "manager");
this.connection = factory.createManagerConnection();
}
public void run() throws IOException, AuthenticationFailedException, TimeoutException {
CommandAction action;
CommandResponse response;
action = new CommandAction();
action.setCommand("odbc show");
// connect to Asterisk and log in
connection.login();
// send the originate action and wait for a maximum of 30 seconds for Asterisk
// to send a reply
response = (CommandResponse) connection.sendAction(action, 30000);
// print out whether the originate succeeded or not
System.out.println(response.getResponse());
// result
List result = response.getResult();
for (String str : result) {
System.out.println(str);
}
// and finally log off and disconnect
connection.logoff();
}
public static void main(String[] args) throws Exception {
HelloManager helloManager;
helloManager = new HelloManager();
helloManager.run();
}
}
asterisk-java提供了很多Action类用于执行不同的命令。
附件为asterisk-java的chm格式帮助文件。