Java/Network Protocol/Net Command
Содержание
Connects to an rexec server
<source lang="java">
/*
* Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package examples; import java.io.IOException; import org.apache.rumons.net.bsd.RExecClient; /***
* This is an example program demonstrating how to use the RExecClient class. * This program connects to an rexec server and requests that the * given command be executed on the server. It then reads input from stdin * (this will be line buffered on most systems, so don"t expect character * at a time interactivity), passing it to the remote process and writes * the process stdout and stderr to local stdout.*
* Example: java rexec myhost myusername mypassword "ps -aux" * <p> * Usage: rexec <hostname> <username> <password> <command> * <p> ***/ // This class requires the IOUtil support class! public class rexec { public static final void main(String[] args) { String server, username, password, command; RExecClient client; if (args.length != 4) { System.err.println( "Usage: rexec <hostname> <username> <password> <command>"); System.exit(1); return ; // so compiler can do proper flow control analysis } client = new RExecClient(); server = args[0]; username = args[1]; password = args[2]; command = args[3]; try { client.connect(server); } catch (IOException e) { System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); } try { client.rexec(username, password, command); } catch (IOException e) { try { client.disconnect(); } catch (IOException f) {} e.printStackTrace(); System.err.println("Could not execute command."); System.exit(1); } IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out); try { client.disconnect(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.exit(0); } } </source>
Connects to an rlogin daemon
<source lang="java">
/*
* Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package examples; import java.io.IOException; import org.apache.rumons.net.bsd.RLoginClient; /***
* This is an example program demonstrating how to use the RLoginClient * class. This program connects to an rlogin daemon and begins to * interactively read input from stdin (this will be line buffered on most * systems, so don"t expect character at a time interactivity), passing it * to the remote login process and writing the remote stdout and stderr * to local stdout. If you don"t have .rhosts or hosts.equiv files set up, * the rlogin daemon will prompt you for a password. * <p> * On Unix systems you will not be able to use the rshell capability * unless the process runs as root since only root can bind port addresses * lower than 1024. * <p> * JVM"s using green threads will likely have problems if the rlogin daemon * requests a password. This program is merely a demonstration and is * not suitable for use as an application, especially given that it relies * on line buffered input from System.in. The best way to run this example * is probably from a Win95 dos box into a Unix host. * <p> * Example: java rlogin myhost localusername remoteusername vt100 * <p> * Usage: rlogin <hostname> <localuser> <remoteuser> <terminal> * <p> ***/
// This class requires the IOUtil support class! public class rlogin {
public static final void main(String[] args) { String server, localuser, remoteuser, terminal; RLoginClient client; if (args.length != 4) { System.err.println( "Usage: rlogin <hostname> <localuser> <remoteuser> <terminal>"); System.exit(1); return ; // so compiler can do proper flow control analysis } client = new RLoginClient(); server = args[0]; localuser = args[1]; remoteuser = args[2]; terminal = args[3]; try { client.connect(server); } catch (IOException e) { System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); } try { client.rlogin(localuser, remoteuser, terminal); } catch (IOException e) { try { client.disconnect(); } catch (IOException f) {} e.printStackTrace(); System.err.println("rlogin authentication failed."); System.exit(1); }
IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out); try { client.disconnect(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.exit(0); }
}
</source>
Connects to an rshell daemon
<source lang="java">
/*
* Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package examples; import java.io.IOException; import org.apache.rumons.net.bsd.RCommandClient; /***
* This is an example program demonstrating how to use the RCommandClient * class. This program connects to an rshell daemon and requests that the * given command be executed on the server. It then reads input from stdin * (this will be line buffered on most systems, so don"t expect character * at a time interactivity), passing it to the remote process and writes * the process stdout and stderr to local stdout. * <p> * On Unix systems you will not be able to use the rshell capability * unless the process runs as root since only root can bind port addresses * lower than 1024. * <p> * Example: java rshell myhost localusername remoteusername "ps -aux" * <p> * Usage: rshell <hostname> <localuser> <remoteuser> <command> * <p> ***/
// This class requires the IOUtil support class! public class rshell {
public static final void main(String[] args) { String server, localuser, remoteuser, command; RCommandClient client; if (args.length != 4) { System.err.println( "Usage: rshell <hostname> <localuser> <remoteuser> <command>"); System.exit(1); return ; // so compiler can do proper flow control analysis } client = new RCommandClient(); server = args[0]; localuser = args[1]; remoteuser = args[2]; command = args[3]; try { client.connect(server); } catch (IOException e) { System.err.println("Could not connect to server."); e.printStackTrace(); System.exit(1); } try { client.rcommand(localuser, remoteuser, command); } catch (IOException e) { try { client.disconnect(); } catch (IOException f) {} e.printStackTrace(); System.err.println("Could not execute command."); System.exit(1); }
IOUtil.readWrite(client.getInputStream(), client.getOutputStream(), System.in, System.out); try { client.disconnect(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.exit(0); }
}
</source>
Finger client
<source lang="java">
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.util.StringTokenizer; public class Finger {
public static void main(String[] arguments) throws Exception { StringTokenizer split = new StringTokenizer(arguments[0], "@"); String user = split.nextToken(); String host = split.nextToken(); Socket digit = new Socket(host, 79); digit.setSoTimeout(20000); PrintStream out = new PrintStream(digit.getOutputStream()); out.print(user + "\015\012"); BufferedReader in = new BufferedReader(new InputStreamReader(digit.getInputStream())); boolean eof = false; while (!eof) { String line = in.readLine(); if (line != null) System.out.println(line); else eof = true; } digit.close(); }
}
</source>
Implement the finger command in Java
<source lang="java">
/*
* Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package examples; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import org.apache.rumons.net.FingerClient; /***
* This is an example of how you would implement the finger command * in Java using NetComponents. The Java version is much shorter. * But keep in mind that the Unix finger command reads all sorts of * local files to output local finger information. This program only * queries the finger daemon. * <p> * The -l flag is used to request long output from the server. * <p> ***/
public class finger {
public static final void main(String[] args) { boolean longOutput = false; int arg = 0, index; String handle, host; FingerClient finger; InetAddress address = null; // Get flags. If an invalid flag is present, exit with usage message. while (arg < args.length && args[arg].startsWith("-")) { if (args[arg].equals("-l")) longOutput = true; else { System.err.println("usage: finger [-l] [[[handle][@<server>]] ...]"); System.exit(1); } ++arg; }
finger = new FingerClient(); // We want to timeout if a response takes longer than 60 seconds finger.setDefaultTimeout(60000); if (arg >= args.length) { // Finger local host try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { System.err.println("Error unknown host: " + e.getMessage()); System.exit(1); } try { finger.connect(address); System.out.print(finger.query(longOutput)); finger.disconnect(); } catch (IOException e) { System.err.println("Error I/O exception: " + e.getMessage()); System.exit(1); } return ; } // Finger each argument while (arg < args.length) { index = args[arg].lastIndexOf("@"); if (index == -1) { handle = args[arg]; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { System.err.println("Error unknown host: " + e.getMessage()); System.exit(1); } } else { handle = args[arg].substring(0, index); host = args[arg].substring(index + 1); try { address = InetAddress.getByName(host); } catch (UnknownHostException e) { System.err.println("Error unknown host: " + e.getMessage()); System.exit(1); } } System.out.println("[" + address.getHostName() + "]"); try { finger.connect(address); System.out.print(finger.query(longOutput, handle)); finger.disconnect(); } catch (IOException e) { System.err.println("Error I/O exception: " + e.getMessage()); System.exit(1); } ++arg; if (arg != args.length) System.out.print("\n"); } }
}
</source>
Implement the Linux fwhois command in Java
<source lang="java">
/*
* Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package examples; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import org.apache.rumons.net.WhoisClient; /***
* This is an example of how you would implement the Linux fwhois command * in Java using NetComponents. The Java version is much shorter. * <p> ***/
public class fwhois {
public static final void main(String[] args) { int index; String handle, host; InetAddress address = null; WhoisClient whois; if (args.length != 1) { System.err.println("usage: fwhois handle[@<server>]"); System.exit(1); } index = args[0].lastIndexOf("@"); whois = new WhoisClient(); // We want to timeout if a response takes longer than 60 seconds whois.setDefaultTimeout(60000); if (index == -1) { handle = args[0]; host = WhoisClient.DEFAULT_HOST; } else { handle = args[0].substring(0, index); host = args[0].substring(index + 1); } try { address = InetAddress.getByName(host); } catch (UnknownHostException e) { System.err.println("Error unknown host: " + e.getMessage()); System.exit(1); } System.out.println("[" + address.getHostName() + "]"); try { whois.connect(address); System.out.print(whois.query(handle)); whois.disconnect(); } catch (IOException e) { System.err.println("Error I/O exception: " + e.getMessage()); System.exit(1); } }
}
</source>
Use the TimeTCPClient and TimeUDPClient: simple Unix rdate
<source lang="java">
/*
* Copyright 2001-2005 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */
package examples; import java.io.IOException; import java.net.InetAddress; import org.apache.rumons.net.TimeTCPClient; import org.apache.rumons.net.TimeUDPClient; /***
* This is an example program demonstrating how to use the TimeTCPClient * and TimeUDPClient classes. It"s very similar to the simple Unix rdate * command. This program connects to the default time service port of a * specified server, retrieves the time, and prints it to standard output. * The default is to use the TCP port. Use the -udp flag to use the UDP * port. You can test this program by using the NIST time server at * 132.163.135.130 (warning: the IP address may change). * <p> * Usage: rdate [-udp] <hostname> * <p> * <p> * @author Daniel F. Savarese ***/
public class rdate {
public static final void timeTCP(String host) throws IOException { TimeTCPClient client = new TimeTCPClient(); // We want to timeout if a response takes longer than 60 seconds client.setDefaultTimeout(60000); client.connect(host); System.out.println(client.getDate().toString()); client.disconnect(); } public static final void timeUDP(String host) throws IOException { TimeUDPClient client = new TimeUDPClient(); // We want to timeout if a response takes longer than 60 seconds client.setDefaultTimeout(60000); client.open(); System.out.println(client.getDate(InetAddress.getByName(host)).toString()); client.close(); }
public static final void main(String[] args) { if (args.length == 1) { try { timeTCP(args[0]); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } else if (args.length == 2 && args[0].equals("-udp")) { try { timeUDP(args[1]); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } else { System.err.println("Usage: rdate [-udp] <hostname>"); System.exit(1); } }
}
</source>