Telnet client for Cisco switch using Android java apps

922 views Asked by At

I am trying to write an Android apps as telnet client for cisco switch(2960) using apache.commons.net.telnet. But i have problem for connection. No output displayed on android emulator, but ConnectBot is running on the same emulator and can telnet to the switch perfectly. Here is code sample:

MainActivity.java

package com.example.android2switch;

import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;

import org.apache.commons.net.telnet.EchoOptionHandler;
import org.apache.commons.net.telnet.InvalidTelnetOptionException;
import org.apache.commons.net.telnet.SuppressGAOptionHandler;
import org.apache.commons.net.telnet.TelnetClient;
import org.apache.commons.net.telnet.TelnetOptionHandler;
import org.apache.commons.net.telnet.TerminalTypeOptionHandler;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends Activity {

    Button button;
    TextView textview;
    EditText edit1;
    Editable server;
    static TelnetClient telnetClient = new TelnetClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {
                    setOptionHandlers();
                    //telnetClient.connect("192.168.0.1");
                    edit1 = (EditText)findViewById(R.id.edit1);
                    server = edit1.getEditableText();
                    telnetClient.connect(server.toString());

                    read();
                    telnetClient.disconnect();
                } catch (SocketException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }   
              }});      
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private static void setOptionHandlers() throws IOException {
        ArrayList<TelnetOptionHandler> optionHandlers = new ArrayList<TelnetOptionHandler>();
        optionHandlers.add(new TerminalTypeOptionHandler("VT100", false, false, true, false));
        optionHandlers.add(new EchoOptionHandler(true, false, true, false));
        optionHandlers.add(new SuppressGAOptionHandler(true, true, true, true));
        for (TelnetOptionHandler handler : optionHandlers) {
            try {
                telnetClient.addOptionHandler(handler);
            }
            catch (InvalidTelnetOptionException e) {
                System.err.println("Error registering option handler " + handler.getClass().getSimpleName());
            }
        }
    }

    public static void write(byte[] data) throws IOException {
        telnetClient.getOutputStream().write(data);
        telnetClient.getOutputStream().flush();
    }

    public void read() throws IOException {

        TextView tv1=(TextView)findViewById(R.id.textView1);
        tv1.setText("Read");
        byte[] buff = new byte[1024];
        int read;
        if((read = telnetClient.getInputStream().read(buff)) > 0) {
            tv1.append(new String(buff));
        }
        tv1.append("read="+read);
    }

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
tools:context="com.example.android2switch.MainActivity" >

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/edit1"
android:hint="Command"
android:text="ip address" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:text="Run Program"
android:id="@+id/button1" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
 />

</LinearLayout>
</ScrollView>
</LinearLayout>

LogCat

06-10 00:35:52.976: E/Trace(1938): error opening trace file: No such file or directory (2)
06-10 00:35:53.716: D/libEGL(1938): loaded /system/lib/egl/libEGL_emulation.so
06-10 00:35:53.716: D/libEGL(1938): loaded /system/lib/egl/libGLESv1_CM_emulation.so
06-10 00:35:53.716: D/libEGL(1938): loaded /system/lib/egl/libGLESv2_emulation.so
06-10 00:35:53.726: D/(1938): HostConnection::get() New Host Connection established 0xb8c6d868, tid 1938
06-10 00:35:53.887: W/EGL_emulation(1938): eglSurfaceAttrib not implemented
06-10 00:35:53.906: D/OpenGLRenderer(1938): Enabling debug mode 0
06-10 00:36:26.367: W/EGL_emulation(1938): eglSurfaceAttrib not implemented
06-10 00:36:39.556: D/AndroidRuntime(1938): Shutting down VM
06-10 00:36:39.556: W/dalvikvm(1938): threadid=1: thread exiting with uncaught exception (group=0xb3df9288)
06-10 00:36:39.616: E/AndroidRuntime(1938): FATAL EXCEPTION: main
06-10 00:36:39.616: E/AndroidRuntime(1938): android.os.NetworkOnMainThreadException
06-10 00:36:39.616: E/AndroidRuntime(1938):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at libcore.io.IoBridge.connect(IoBridge.java:112)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at java.net.Socket.connect(Socket.java:842)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at org.apache.commons.net.SocketClient.connect(SocketClient.java:182)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at org.apache.commons.net.SocketClient.connect(SocketClient.java:203)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at org.apache.commons.net.SocketClient.connect(SocketClient.java:296)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at com.example.android2switch.MainActivity$1.onClick(MainActivity.java:45)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at android.view.View.performClick(View.java:4084)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at android.view.View$PerformClick.run(View.java:16966)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at android.os.Handler.handleCallback(Handler.java:615)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at android.os.Looper.loop(Looper.java:137)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at android.app.ActivityThread.main(ActivityThread.java:4745)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at java.lang.reflect.Method.invokeNative(Native Method)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at java.lang.reflect.Method.invoke(Method.java:511)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-10 00:36:39.616: E/AndroidRuntime(1938):     at dalvik.system.NativeStart.main(Native Method)

I'm completely new to Android development(Eclipse Luna) and i can't find any references/examples of android base telnet client for cisco switch/router. Any references, sample code or guidances will be much appreciated, thank you!

1

There are 1 answers

4
Fund3rburk On

Have you added the correct permissions to your manifest file? Make sure you have added the permission that allows applications to open network sockets.

"<uses-permission android:name="android.permission.INTERNET"/>

see here for the details: http://developer.android.com/reference/android/Manifest.permission.html

Also, similar problem was resolved here: Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'