#!/usr/bin/python3
import subprocess
config = subprocess.check_output(["openvpn","credentials.ovpn"])
if config == ("Enter Auth Username:"):
subprocess.call("Ru6cEvx6bx")
Hi guys, just making very small script to connect to openvpn. Where i don’t have to put username and password every-time to connect to the vpn.
Related
I'm going to ssh to the server using python and kivy in order to make an ssh tunnel. What I've done is execute this command: ssh -vND port user#my-server-ip.
import os
connect_command = "ssh -vND port user#my-server-ip"
returned_value = os.system(connect_command)
password = "something-got-from-user-input"
It is connecting correctly but the problem is that I can't enter the password when user#my-server-ip's password: prompted.
What I have done for this, is:
text = "user#my-server-ip's password:"
if (returned_value.startswith(text)):
os.system(password + "\n")
but it doesn't work.
So how can I enter the password I got from the user when it needs the password to be entered in order to connect?
I have a python app that runs on an SSH connection through MobaXTerm. Many people use the app, the current version has each one log in with a username & password. I am trying to skip the login process all together and just have a list of authorized users' windows login usernames. The only ways I know how to retrieve the username will return the username of the current SSH connection which is the same for everyone. Is there a way to get the Windows username during an SSH connection?
os.getlogin()
os.path.expanduser('~')
os.environ.get('USERNAME')
getpass.getuser()
pwd.getpwuid(os.getuid())[0]
These all return the SSH username. Any help would be much appreciated.
I am trying to use Paramiko to make an SSH communication between 2 servers on a private network. The client server is a web server and the host server is going to be a "worker" server. The idea was to not open up the worker server to HTTP connections. The only communication that needs to happen, is the web server needs to pass strings to a script on the worker server. For this I was hoping to use Paramiko and pass the information to the script via SSH.
I set up a new user and created a test script in Python 3, which works when I run it from the command line from my own user's SSH session. I put the same code into my Django web app, thinking that it should work, since it tests OK from the command line, and I get the following error:
Server 'worker-server' not found in known_hosts
Now, I think I understand this error. When performing the test script, I was using a certain user to access the server, and the known hosts information is saved to ~/.ssh/known_hosts even though the user is actually a 3rd party user created just for this one job. So the Django app is running under a different user who doesn't find the saved known hosts info because it doesn't have access to that folder. As far as I can tell the user which Apache uses to execute the Django scripts doesn't have a home directory.
Is there a way I can add this known host in a way that the Django process can see it?
Script:
import paramiko
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('worker-server', 22, 'workeruser', 'workerpass')
code = "123wfdv"
survey_id = 111
stdin, stdout, stderr =
client.exec_command('python3 /path/to/test_script/test.py %s %s' % ( code, survey_id ))
print( "ssh successful. Closing connection" )
stdout = stdout.readlines()
client.close()
print ( "Connection closed" )
output = ""
for line in stdout:
output = output + line
if output!="":
print ( output )
else:
print ( "There was no output for this command" )
You can hard-code the host key in your Python code, using HostKeys.add:
import paramiko
from base64 import decodebytes
keydata = b"""AAAAB3NzaC1yc2EAAAABIwAAAQEA0hV..."""
key = paramiko.RSAKey(data=decodebytes(keydata))
client = paramiko.SSHClient()
client.get_host_keys().add('example.com', 'ssh-rsa', key)
client.connect(...)
This is based on my answer to:
Paramiko "Unknown Server".
To see how to obtain the fingerprint for use in the code, see my answer to:
Verify host key with pysftp.
If using pysftp, instead of Paramiko directly, see:
PySFTP failing with "No hostkey for host X found" when deploying Django/Heroku
Or, as you are connecting within a private network, you can give up on verifying host key altogether, using AutoAddPolicy:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(...)
(This can be done only if you really do not need the connection to be secure)
I wanted to know whether there is a possibility to use a python script in order to connect to a router and control the interface (shut down, restart wireless network etc..) with an ssh connection.
SO far I wrote these lines,but still it does not work. When i look to the terminal I see that everything is blocked at the point when my script should echo the password for the router to finalize the connection. How can I correct this please ?
Here are the lines :
import os, urllib, urllib2, re
def InterfaceControl():
#os.system("echo training")
os.system("ssh -l root 192.168.2.1")
os.system("echo yes")
os.system("echo My_ROUTER_PASSWORD")
os.system("shutdown -r")
def main():
InterfaceControl()
if __name__=="__main__":
main()
Thank you so much in advance
You can use paramiko which is a python library that abstracts remote shell connections through ssh with several options allowing users to use authentication with rsa keys, etc. This is a sample code you can reuse to solve your problem:
import paramiko
ssh = paramiko.SSHClient()
ssh.connect( 'hostname', username = 'username', password = 'password' )
ssh.exec_command( 'ls -al' )
By the way paramiko can be easily added to your python environment if you're running your script from a virtual environment (virtualenv).
plumbum is what you're looking for. (remote commands)
I'm writing fairly simple application which connects to server through SSH (using paramiko), does something and writes output to web page. I wrote a script which works well when I run it from command line. However, if I run it in Django application, it can't get through connect part.
SSH connect part:
transport = paramiko.Transport((host, port))
# application cannot get through this line
transport.connect(username = '***', password = '***')
output = ...
View:
def ssh_output(request):
return HttpResponse(output)
Any idea why does it behave like this? Is there any way to fix it?
I'm guessing your Django app may be running under a different user than the user you're running your command line script under. Also, I'm guessing it might be the first time the Django app user is trying to ssh to the host, so it may be hanging on some sort of 'is it OK to update ~/.ssh/known_hosts' question.
It looks like if you use SSHClient instead of Transport, then you can set the missing host key policy to automatically add the missing host keys ala
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(your_host, port=your_port, username=your_username, password=your_password)