I am attempting to send multiple commands to a device using the SSHJ library. There is the option of sending multiple commands in this format:
Command command = sshSession.exec("show line; show ip interface brief;");
This works, but it is not always usable in my case. I have found other suggestions such as the second answer here.
When I attempt this suggestion the first command works fine and then it cycles between this error:
net.schmizz.sshj.connection.ConnectionException: Broken transport; encountered EOF
...
Caused by: net.schmizz.sshj.transport.TransportException: Broken transport; encountered EOF
or
Exception in thread "main" java.lang.IllegalStateException: Not connected
at net.schmizz.sshj.SSHClient.checkConnected(SSHClient.java:713)
at net.schmizz.sshj.SSHClient.startSession(SSHClient.java:651)
The code used is:
sshSession = sshClient.startSession();
Command command = sshSession.exec("sho ip int brie");
System.out.println(IOUtils.readFully(command.getInputStream()));//Just to see the reply while testing
command.join(5, TimeUnit.SECONDS);
sshSession = sshClient.startSession();
Command command2 = sshSession.exec("sho line");
System.out.println(IOUtils.readFully(command2.getInputStream()));//Just to see the reply while testing
A note if needed, the device I am connecting to, and majority of devices that it will connect to are Cisco networking equipment.
Thank you for any assistance.
-Jarrod
Never found a resolution for the exact problem. But I worked around the issue by using the DefaultPTY and providing my own streams with all the data I wanted to send. Playing around with this example.
Related
Exception:
Command not supported
at net.sourceforge.jsocks.socks.Socks5Message.read(Socks5Message.java:162)
at net.sourceforge.jsocks.socks.Socks5Message.<init>(Socks5Message.java:123)
at net.sourceforge.jsocks.socks.Socks5Message.<init>(Socks5Message.java:108)
at net.sourceforge.jsocks.socks.Socks5Proxy.formMessage(Socks5Proxy.java:245)
at net.sourceforge.jsocks.socks.Proxy.exchange(Proxy.java:446)
at net.sourceforge.jsocks.socks.Proxy.udpAssociate(Proxy.java:385)
at net.sourceforge.jsocks.socks.Socks5DatagramSocket.<init>(Socks5DatagramSocket.java:110)
at net.sourceforge.jsocks.socks.Socks5DatagramSocket.<init>(Socks5DatagramSocket.java:54)
...
Code where error is occurring:
try(Socks5DatagramSocket socket = new Socks5DatagramSocket())/* error here */ ...
I am using jsocks only for the ability to send UDP data over a proxy, if there are any better alternatives I am also open.
I want to receive some messages from IBM MQ based on java. So I write a very simple code to test what I want. Here is the sample code below.
Const.MQ_QMANAGER = "QM.CREDITWEB3T.PC";
Const.MQ_QUEUE_RECEIVE = "MQ.AIRPORTS";
MQSimpleConnectionManager myConnMan = new MQSimpleConnectionManager();
myConnMan.setActive(MQSimpleConnectionManager.MODE_AUTO);
MQQueueManager qMgr = new MQQueueManager(Const.MQ_QMANAGER,
myConnMan);
When I new the MQQueueManager. It’s throw an error message.
The error message is
MQJE001: Completion Code '2', Reason '2495'
May someone help me to figure out what’s wrong here? Thanks a lot.
(1) Your 1st posting is requesting that the connection to the queue manager be in "bindings mode". This means you MUST run the code on the same server where the queue manager is running.
(2) Your 2nd posting is requesting that the connection to the queue manager be in "client mode" (over the network i.e.TCP/IP). This means you can run the code on a remote server and connect to the remote queue manager using the network.
After many times error and searched for the answer. Finally I found the solution. You needed to settle environmental parameter for MQ. I am sorry about that because I am so new about MQ. Here is the sample code below.
// Host
MQEnvironment.properties.put(MQConstants.HOST_NAME_PROPERTY,
Const.MQ_HOST_NAME_PROPERTY);
// Port
MQEnvironment.properties.put(MQConstants.PORT_PROPERTY,
Const.MQ_PORT_PROPERTY);
// Channel
MQEnvironment.properties.put(MQConstants.CHANNEL_PROPERTY,
Const.MQ_CHANNEL_PROPERTY);
MQEnvironment.properties.put(MQConstants.CCSID_PROPERTY, XXX);
I am working on a project that communicates to a server through a URLConnection.
Here is the code:
URL theSite;
theSite = new URL(TestURL);
URLConnection con = theSite.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
However this is taking my Motorola Atrix and Samsung Nexus S about 20 seconds or more to when talking with the server. (Information does get sent eventually it seams) Looking at the packets in wireshark, I've found that there are many groups of DNS packets sent which with about 3-5 seconds between each one. This is likely the cause of the slow communication.
Here is are two sample DNS packets. (I've changed the IP's, except for the destination of 8.8.8.8 which I believe is google)
Time Source Destination Protocol Info
20.308792 10.10.120.104 8.8.8.8 DNS Standard query PTR 3.120.10.10.in-addr.arpa
25.360726 10.10.120.104 8.8.8.8 DNS Standard query PTR 3.120.10.10.in-addr.arpa
Anyways, this happened today out of the blue. But it is very apparent that it is these DNS calls that are causing slow communication between the server and my device.
Another thing of note, is that I have also tried the EXACT same code on the Samsung Galaxy 10.1 tablet and it works fine. Looking at the packet trace there are no Extra DNS calls from the tablet.
I don't have control over the server, and had the packets sent to me. Does anyone have any suggestions? I'm guessing that it is a server related issue. If anyone has any ideas, it is much appreciated.
Thanks!
Are you using 4g or 3g? I had a very similar experience today, and noticed the delay was only present when using 4g. 3g seems to be fine.
AFAIK, 4g uses ipv6 addresses by default, while 3g uses ipv4 by default. That delay is ipv6 failing, getting to the failover, and finally being rerouted. For me, it took about an extra 20 seconds.
Currently, I am waiting on feedback from the server team to see if our DNS is set up to handle ipv6 properly.
But honestly, this is only as far as I got, sorry it is nothing definite.
We have figured this out. Turns out on the servers side, there were issues routing DNS packets and they kept timing out which they have fixed.
Something we added to help make sure that this issue doesn't happen again, is manually setting the DNS timeouts so that after failing once, the DNS packet timeout will be almost instant. Using InetSocketAddress is the key.
Here's some sample code to get this working.
int timeout = 0; //How ever long you want to set the timeout to.
somePageParameters = "Parameter String";
InetSocketAddress isock = new InetSocketAddress(ip, 1000);
Socket s = null;
s = new Socket();
s.connect(isock,timeout);
OutputStream os = s.getOutputStream();
String myString = "";
myString = "GET "+"/"+ somePageParameters+" HTTP/1.0\n\r\n\r";
System.err.println("Hitting with: "+myString);
byte outbuf[] = myString.getBytes();
os.write(outbuf);
os.flush();
Then use this socket as you normally would.
Hopefully this helps someone.
I am trying to download a xml text file from a web server using this method:
static void download (String url , String fileName) throws IOException{
FileWriter xmlWriter;
xmlWriter = new FileWriter(fileName);
System.out.println("URL to download is : " + url);
// here Exception is thrown/////////////////////////////////
BufferedReader inputTxtReader = new BufferedReader
(new BufferedReader(new InputStreamReader(addURL.openStream())));
////////////////////////////////////////////////////////
String str ;
String fileInStr = "";
str = inputTxtReader.readLine();
while (!(str == null) ){///&& !(str.equals("</tv>"))
fileInStr += (str + "\r\n");
str = inputTxtReader.readLine();
}
xmlWriter.write(fileInStr);
xmlWriter.flush();
xmlWriter.close();
System.out.println("File Downloaded");
}
Sometimes this exception is thrown (where I specified is code):
java.net.SocketException: Network is unreachable: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.Socket.connect(Socket.java:518)
at java.net.Socket.connect(Socket.java:468)
at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:389)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:516)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.http.HttpClient.New(HttpClient.java:318)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:788)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:729)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:654)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:977)
at java.net.URL.openStream(URL.java:1009)
at MessagePanel.download(MessagePanel.java:640)
at WelcomThread.run(MainBody2.java:891)
Please guide me
Thank you all.
You are facing a connection breakdown. Does this happen in 3G, WiFi or "plain" connection on a computer?
Anyway, you must assume that the connection may be lost from time to time, when writing your app. For example, with mobiles, this happens frequently in the tube, in basements, etc. With PC apps, this is less frequent but occurs sometimes.
A retry can be a good solution. And a clean error message that explains the network is not available at this moment too.
I faced situation of getting java.net.SocketException not sometimes but every time. I've added -Djava.net.preferIPv4Stack=true to java command line and my program started to work properly.
"Network is unreachable" means just that. You're not connected to a network. It's something outside of your program. Could be a bad OS setting, NIC, router, etc.
I haven't tested with your code so it would be totally different case though, still I'd like to share my experience. (Also this must be too late answer though, I hope this answer still would help somebody in the future)
I recently faced similar experience like you such as some times Network is unreachable, but sometimes not. In short words, what was cause is too small time out. It seems Java throws IOException with stating "Network is unreachable" when the connection fails because of it. It was so misleading (I would expect something like saying "time out") and I spent almost a month to detect it.
Here I found another post about how to set time out.
Alternative to java.net.URL for custom timeout setting
Again, this might not the same case as you got experienced, but somebody for the future.
this just happened to me. None of the answers helped, as the issue was I have recently changed the target host configuration and put incorrect host value there. So it could just be wrong connection details as well.
I faced this error after updating my network adapter configuration (migration to a NIC coupled network by PowerShell commandlet New-NetSwitchTeam). My guess is, that something in the java configuration must be adapted to reflect this change to the java system. But it is unclear where the changes should take place. I am investigating further.
I'm using Clojure, but I can read Java, so this isn't a Clojure specific question. This doesn't even seem to be working from Java.
I'm trying to implement a bit of a 'ping' function using isReachable. The code I'm using is this:
(.isReachable (java.net.InetAddress/getByName "www.microsoft.com") 5000)
Translated to Java by a good friend of mine:
public class NetTest {
public static void main (String[] args) throws Exception{
String host = "acidrayne.net";
InetAddress a = InetAddress.getByName(host);
System.out.println(a.isReachable(10000));
}
}
Both of these return false. I suppose I must be doin' it wrong, but Google research is telling me differently. I'm confuzzled!
Updated in response to comment that this is wrong:
Using Unix/Linux??
http://bordet.blogspot.com/2006/07/icmp-and-inetaddressisreachable.html says:
Linux/Unix, instead, supports an ICMP "ping" system call. So the implementation of java.net.InetAddress.isReachable() first tries to perform the "ping" system call**; if this fails, it falls back trying to open a TCP socket on [sic - to] port 7, as in Windows.
It turns out that in Linux/Unix the ping system call requires root privileges, so most of the times java.net.InetAddress.isReachable() will fail, because many Java programs are not run as root, and because the target address unlikely has the echo service up and running. Too bad.
The comment below from #EJP indicates the part of about the echo service is wrong, wrong wrong:
That's not correct. isReachable returns true if it gets a ConnectException trying to connect to port 7, as that proves that the host is up and able to send RST segments.
In cases like these, I use a packet sniffer like WireShark, tcpdump (WinDump on Windows) or snoop (Solaris) to confirm what is really happening on the wire.
The correct answer is not actually correct I think. Microsoft.com simply ignore ICMP requests, probably to avoid basic ping flood attacks. As for the second host I've no idea what the problem with the ping might be, but I'm using GNU/Linux and isReachable works just fine.