I am having an issue with my remote SSH connection in VSCode. When I try to connect, it appears to be working normally, but after a short time it becomes disconnected and I need to reconnect. I have tried to look for a solution on how to reconnect the remote connection in VSCode, but so far I have not been successful. I would like to be able to reconnect to the remote connection in order to continue working on my processes without having to restart them. I have checked my ssh_config file, but I am not sure if there is a setting I need to change in order to reconnect to the remote connection. I would like to post this issue on StackOverflow to see if there is a solution that can help me reconnect to the remote connection in VSCode. I've checked this post but the "Reaload" functión will throw all my variables and procecess. Can someone help me please? Or at least tell me "there is not a solutión, just reload an change your ssh config file like the previous post".
Related
I'm using Paramiko interactive shell (invoke_shell() method) for opening session between my local host and remote host and sending commands to it.
Sometimes I need to open from my remote host an ssh connection to another remote host (this is like a bridge or SSH via SSH from local host to remote_host_2 via remote_host_1).
For better understanding this is a picture how it should behave:
| local host | -(1)-> | remote host | -(2)-> | destination host |
1: is open using invoke_shell() paramiko method so it will create some paramiko.Channel obj.
2: is open using Linux command i.e. ssh remote_user#remote_ip
This is done this way because sometimes I have no direct connection to destination host and in case you are opening it via remote host then user decision should be made (entering another password, answering yes/no on some questions etc..)
Now my question is regarding keeping my connection alive.
I read on keep_alive mechanism that paramiko has but it doesn't do what I want because paramiko knows my connection ends in remote_host and not in destination host so in case destination host is dead I will not receive any notification regarding it and commands execution will fail.
The only solution that came to my mind is sending empty command on this channel (\n) and trying to read the output from the channel before executing the desirable command on it. but this means that I can affect my channel on one hand and my command execution time is now twice longer.
Now my question is, is there another way to perform this connection so this keep alive mechanism will work?
p.s. I read that there exists some ServerAliveInterval=30 flag that can help me to keep my ssh interactive connection alive but I don't understand how can I use it to validate it doesn't became dead.
The correct way is to implement the jump using port forwarding.
See Connecting to a server via another server using Paramiko
Then you will have a complete control over both connections and you will be able to use native Paramiko features to keep the connection alive and to check its status.
I'm working a tutorial in Python 3.8 that involves sockets and networking. There is a server.py file and a client.py file. I took example code straight out of the Python doc for sockets to see if that would work, and it does not. The server starts and creates a socket and listens for the connection, but I get WinError 10061, the one where the target machine refuses the connection. My OS is Windows 10 and I'm using IDLE. I've looked at my Firewall and set a permission for pythonw.exe to be allowed through, but that has not helped. Anybody have any fixes for me to try? I can't really proceed until I can get the client and server connected.
I think I know what I’ve been doing wrong. I have been running both server and client files in the same console. I think I have to open two consoles and run one file in each so they can communicate.
(Doh!)
I’m at work so I can’t test it right now. Just in case anyone else has been befuddled by this .
Yes, I did not realize that each file had to run in its own instance of IDLE, but that makes perfect sense now. A socket won’t connect to itself!
I've been trying to connect to first an out-of-band management server (in my case, since I'm connecting to DELL-servers, an iDRAC) and through that connect to the main server itself. I've got it to work when I do it manually, using, in the (windows) terminal:
putty.exe -ssh 'username'#'iDRAC-IP'
followed by PuTTY window opening where I type in the password, followed by
connect
which connects to the server itself, and then I type in the username and password for the server, completing the process.
When I've been writing my script in python, I'm using paramiko, http://www.paramiko.org/, suggested here on stackoverflow, and following this example: https://www.ivankrizsan.se/2016/04/24/execute-shell-commands-over-ssh-using-python-and-paramiko/, and it works just splendid for the iDRAC (the first server I connect to). It also works when I type in
stdin, stdout, stderr = ssh_client.exec_command('connect')
because I am still in my first server (ssh_client) (I can tell this is working because when I try to connect to the server manually afterwards, it is occupied). But after that it stops working, since when doing 'connect' I am no longer in ssh_client, but in a different server.
So my question is - how do I connect to a server from another server (in this case being the out-of-band management server) and log in to this one?
You can use ssh tunnel to do so.
this post may resolve your problem:
PyCharm: Configuring multi-hop remote Interpreters via SSH
This is funny because most questions I found are the other way around.
I have a flask server that I start on debugging mode, listening on port 6000.
I have a script that accesses the url through localhost which sends back a JSON reply.
When I do an ssh tunnel using ssh -L and run the script that sends a request in my machine to localhost everything works fine. When I run the script directly from the server it also works fine. However, since I want to ultimately use a big data set, I setup a cron job. When the cron runs, the script crashes with connection refused error. I can see that the server is running after the script crashes and the port is correct on both.
I have no idea why this can happen. Any ideas are appreciated.
Solution
I couldn't figure out the reason or how to solve it, so I decided to run manually and put it in the background.
How to make a programme continue to run after log out from ssh?
A week back I posted this, and about 2 weeks back I posted this, both pertaining to an ever evolving issue with my Python sockets. I've also asked a question on the Wireshark Stack Exchange, and even after all 3 posts, I haven't been able to reach any conclusion. So heres the problem:
When I start up IDLE, it returns an error: IDLE Subprocess: socket error: No connection could be made because the target machine actively refused it and then I press ok on the dialogue boxes and it closes it out. But the issue isn't only limited to IDLE.
Any TCP ports I try to connect to via the Python socket module will fail. For example, if I run a server and a client (server first, of course, and both have the current IP of my computer and the port 45002) locally on my computer it returns: [Errno 10061] No connection could be made because the target machine actively refused it. So someone on an earlier post told me that running both the programs locally on a Windows machine will not work because it doesn't have a loopback interface (although when I look at my network interfaces via CMD, Loopback interface is listed for some reason). So then I went ahead and put the server on the computer with the issue, and the client on another computer on the network. It returns: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. But for some reason, if I run the server on the other computer and then run the client on the computer with the issue, the server and client actually connect, and then the server fails claiming: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied, even though I made sure to include a .connect(addr) in the client program. Sorry if this is getting confusing, but it's going to get a bit more complicated than this.
After this mess of running servers/clients on different computers, per request of another answerer on an early post, I installed Wireshark and began looking for packets being sent on the ports I am using. The results were a little weird. When I run the server on the computer with the issue, and the client on the other computer, Wireshark catches absolutely no packets on the port of the server, and the client fails with the errno 10060, as I said earlier. If I run the server from the other computer and the client from the computer with the issue, Wireshark catches some ordinary SYNs and ACKs. I only get an RST when I end the Python.exe process (this info is really only important to people who are good with Wireshark/networking, it's really only extra info).
I've also tried capturing packets from the IDLE program with Wireshark after I pinpointed the range of ports that it tends to use. When I start it up, no packets on any port in the range. Again, this is really just extra info to any of you who may be able to make something of it.
Again, sorry for the confusion, but I must go on.
And now, the issue that brought all this up. The problem that really caused me to ask myself "Hmm... Why is Python not working correctly?", my Twisted server was not able to import the reactor module returning the errno 10061. If you don't use/know of Twisted, all that's really needed to understand the problem is that, to import the reactor module, Twisted has to setup a Python TCP port, which is not working.
This issue has left me (and presumably everyone who's read it) scratching their head. I am sure that the ports I am using are not used. I'm sure the IP addresses are correct and matching. I am sure that there is no firewall blocking the connections, since I have tried running the programs without a firewall on with no luck. I am running a Wireless Windows 64bit laptop.
Heres my client program, and here's my server program, if they help at all.
Any suggestions, ideas, or answers are welcomed. At this point, any at all. Thanks.
P.S. If there is any info I can provide, or anything I can do to help find the solution to this issue, please tell. Also, I know this question is barely a programming one, so if there is somewhere else I am supposed to put this, please say and I'll move it over.
EDIT: Seems like I solved one piece of the puzzle. I can now connect a client on the computer with the issue to the computer with no issue, but not the other way around (running the client on the good computer, and running server on the broken one). I had to switch servSock.recv(buff) to cliSock.recv(buff) on the server side.