Python - Enter password when prompted - python

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?

Related

Failing to start an .exe to a remote Windows server using Python and paramiko

I have written a simple script that I want to run remotely to a Windows server using Python and Paramiko. The script should execute commands in the cmd on the remote Windows server and I want it to start up an .exe program for a starter. Here is what I have up to now:
import paramiko
hostname = "IP_of_server"
username = "username"
password = "password"
command = "start Full_Path_To_Application\Program.exe"
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname, username=username, password=password, look_for_keys=True, allow_agent=False)
print("Connected to %s" % hostname)
except paramiko.AuthenticationException:
print("Failed to connect to %s due to wrong username/password" %hostname)
exit(1)
try:
stdin, stdout, stderr = ssh.exec_command(command)
for line in stdout.readlines():
print(line)
print("Application has been started")
except:
exit(2)
What I get is:
Connected to IP_of_server
Application has been started
When I check on the server using the same username and password the application is not running. Running it manually starts it up.
I have confirmed that the command has been sent to the server by replacing it with "ipconfig" and I get the correct information.
Any idea why the application is not starting? Once it starts it should open a separate cmd with all the logs that I'm not seeing.
Thanks a lot.

Get Windows Username during SSH connection

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.

ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: YES) on Windows

MySQL was working fine yesterday but today when I tried connecting to MySQL with Python, I recieved this error. I tried to open MySQL Command Line but it closes right after I input the password even though MySQL service is running. So I can't run any MySQL queries through the command line.
I am definitely sure the password for the root account is correct, but I tried leaving the password and the user blank but it still gives the same error. I tried to change the password through the Installer, but whenever I try to configure the password it asks me for my old password, when I enter it this error shows up:
Hovering over the X the error states
MySQL Server is not running, a connection cannot be established.
As I said before, MySQL service is actually running. I am unable to change the password through cmd as well, through
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqladmin -uroot -p password
Enter password: *****
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'#'localhost' (using password: YES)'
I have tried using the password blank as well as the user blank in all of these as well. Port is 3306. Any help would be appreciated. Thanks in advance.
EDIT: The Python code I used to connect was:
qtdb = mysql.connector.connect(host = "localhost", user = "root",
password = "mysql", database = "misc", port = "3306")
Ok, so I figured it out. I used this guide to change my password:
https://dev.mysql.com/doc/mysql-windows-excerpt/5.7/en/resetting-permissions-windows.html

Cmd not capturing username in python

#!/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.

Automate Install mysql-server and postfix remotely

I am running a Python script with Fabric which sent a bash script over remotely and execute.
The script will have to run over multiple remote servers automatically (without user interference).
But when I am installing these 2 package, there is a small complication of GUI interface prompt in the command line. This cause the bash script to 'hang', awaiting for user input to continue.
mysql-server (Prompting user for root password)
postfix (Prompting for some configuration setting)
Is there an alternative to configure the process without the GUI interface prompt in the command line?
In http://www.muhuk.com/2010/05/how-to-install-mysql-with-fabric/ it's described a solution to the installation of mysql-server, basically what you have to do is:
# password prompt
while True:
mysql_password = getpass('Please enter MySQL root password: ')
mysql_password_confirmation = getpass('Please confirm your password: ')
if mysql_password == mysql_password_confirmation:
break
else:
print "Passwords don't match"
# set the value in debconf
with settings(hide('warnings', 'running', 'stdout', 'stderr'),
warn_only=True):
if not run('dpkg-query --show mysql-server'):
sudo('echo "mysql-server-5.1 mysql-server/root_password password '
'%s" | debconf-set-selections' % mysql_password)
sudo('echo "mysql-server-5.1 mysql-server/root_password_again '
'password %s" | debconf-set-selections' % mysql_password)

Categories