I am trying to write a script to use when connecting remotely to various computers in my office. We also use VNC to allow us to see the user desktops. I have been trying to find a script that would allow me to do this, but I have had no luck. Right now, we use the SSH command in Terminal (we all use Macs), which looks like the following:
ssh "hostname" -L 5901:127.0.0.1:5900
This then requires RSA fingerprint and user password. Username is never requested as it is the same as the user profile on the computer. 5901 can also be 5902, 5903, etc, depending on which display port is specified in our VNC client.
I would ultimately like to created a script that would prompt for hostname and display port, assuming username and password can be stored permanently in the script. If not, we would need prompts for those as well. Is this even possible?
I while ago had a similar use case so I put together this script:
http://code.activestate.com/recipes/576810-copy-files-over-ssh-using-paramiko/
To tunnel VNC over SSH you would need to forward port 5900 for connecting to the real xorg instance, e.g. via x11vnc, or port 5901 to connect to the first virtual xorg (e.g. via vncserver), 5902 to connect to the second xorg, etc.
I am not aware of paramiko being able to forward ports but there seems to be a pure Python module that does just that https://gist.github.com/1399529
Related
Good Day
I work for an ISP and we basically manage all our switches and routers via the CLI from a Jumpbox.
I would like to automate some of my work on these devices by writing Python scripts, etc.
However, this Jumpbox (Linux), is quite old and the Python version is old. I cannot add Ansible, Netmiko, etc. Plus I'm not an Admin for that box so can't upgrade it.
My question is, if I set up my own Linux VM with all the required tools, how would I be able to access these routers and switches from my local Linux VM?
I tried setting up a Local/Remote/Dynamic SSH Tunnel to the Jumpbox, but I always end up on the Jumpbox SSH session itself.
You can use the jumpbox as a bastion host. Copy your public keys to both hosts (the jumpbox and the devices) and in your inventory file use the ansible_ssh_common_args option to set it up, like this:
[switches]
switch-01 ansible_host=192.168.0.1 ansible_ssh_common_args='-o ProxyCommand="ssh -W %h:%p -q user#ip-bastion"'
Note: you must be running Ansible version 2.
Best regards.
Using PyCharm remote debugging is one of my favorite choices when doing deep learning jobs on Server platform. But recently I face a problem is that I have to first use SSH to login the platform then I will need another SSH to access the computing node. I may have to do this using my shell.
ssh myname#myip
ssh mynode
python myfile.py
Thus, usually when I use PyCharm. I can only do as the following:
ssh myname#myip
python myfile.py
My question is: how I can use PyCharm to double my SSH operation?
Generally, I use MobaXterm as the intermediate jump tools.
Choose Tunneling toolbar, you will get a dialog like this 1st step
Click the gear in settings box,and then edit the local port forwarding like this 2nd step
The final step, adding remote interpreter in pycharm:
3.1. Choose ssh interpreter
3.2. The host should be localhost, and port is the one mapped to your own PC. Then enter your username and follow the dialog.
Here is the figure of 3rd step
I need to create a terminal/console program on my server compute and I want to connect to it with ssh from client. And I want to do it from a python script. So, I figured that I need to somehow have this script run the program (subprocess maybe?) and put it out on the socket.
How can I put out a certain program on a specified socket for ssh connection?
Can I even ssh to a certain program run in a console, not to a whole PC? I need client to have only acces to what I set up.
How can I put out a certain program on a specified socket for ssh connection?
I think that what you actually want to do is changing what is run when logging in with ssh.
You can do this in /etc/passwd by changing /bin/bash to the program you want to run when you log in. Do this for the user that you want to log in to via ssh.
I didn't understand if you wanted to log in from a python script or log in to a python program in an interactive shell, so:
You can use the paramiko library to log into a machine with ssh. (if that is what you want)
I need to create a terminal/console program on my server compute and I want to connect to it with ssh from client.
You can look at this project https://github.com/python-cmd2/cmd2 to build interactive console programs in python.
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 isn't tied to a specific code sample that I have, but many of them. I'm using Python Fabric to make connections to various servers over SSH from a single host that I'm running my script on. Sometimes it connects just fine and that is the end of it. However, sometimes it can make a connection but gives me a "[serverName] Login password for 'userUser'" error. I don't have access to this server, so even though the connection is being made, I can't authenticate to it. Is there any way to make Bash / Fab / Python realize that I can't connect?
I would investigate the following ssh_config options:
KbdInteractiveAuthentication no
PasswordAuthentication no
See man ssh_config for more info on these options.
You can set these options in your ~/.ssh/config file or on the ssh command line using -o. I imagine using the ~/.ssh/config file would be transparent to Fabric.