Get Windows Username during SSH connection - python

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.

Related

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

Fabric using SSH key to connect the ec2 instance

I'm learning the fabric to automatically connect the ec2 instance which is already created. I set a ssh_config in the ssh folder
Home myhostname
Hostname 52.62.207.113
User ubuntu
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile ~/.ssh/mykey-pem
And I wrote a python file to test
from fabric import Connection
c = Connection('52.62.207.113')
result = c.run('uname -s')
The terminal response
paramiko.ssh_exception.SSHException: No authentication methods available.
I'm not sure what happens. I try to manually
ssh -i mykey.pem ubuntu#52.62.207.113
It is successfully connecting the EC2 instance
Home myhostname
Hostname 52.62.207.113
...
c = Connection('52.62.207.113')
I'm not a fabric user, but I guess you're expecting fabric to make use of the entry from your ssh_config file here? I can see two likely problems:
You have Home myhostname. The correct keyword here is Host, not Home:
Host myhostname
Hostname 52.62.207.113
If you want fabric to use the Host section for myhostname, you probably have to tell it to connect to myhostname:
c = Connection('myhostname')
You're telling it to connect to an IP address, and it probably wouldn't relate that to the host section
The actual error that you're getting, "No authentication methods available", is probably because fabric didn't apply the Host section from ssh_config, and it doesn't know of any key files that it should use for the session.
I think you missed PreferredAuthentications options.
And you typed your key file name incorrectly.
Change the config file as shown below and try connecting again.
Home myhostname
Hostname 52.62.207.113
User ubuntu
PreferredAuthentications publickey
IdentityFile ~/.ssh/mykey.pem

Python - Alternatives to hardcoding FTP password

I'm writing a script that will connect to a remote FTP server and login using my username and password and then upload a file from my HDD to the server.
I feel like there can be alternatives to hardcoding my sensitive information (password and username) in the code. Any suggestions? What's the best thing that can be done?
import ftplib
ftp = ftplib.FTP('ftp.mydomain.com')
ftp.login('username', 'password')

How to connect pika to rabbitMQ remote server? (python, pika)

In my local machine I can have:
connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
for both scripts (send.py and recv.py) in order to establish proper communication, but what about to establish communication from 12.23.45.67 to 132.45.23.14 ? I know about all the parameters that ConnectionParameters() take but I am not sure what to pass to the host or what to pass to the client. It would be appreciated if someone could give an example for host scrip and client script.
first step is to add another account to your rabbitMQ server. To do this in windows...
open a command prompt window (windows key->cmd->enter)
navigate to the "C:\Program Files\RabbitMQ Server\rabbitmq_server-3.6.2\sbin" directory ( type "cd \Program Files\RabbitMQ Server\rabbitmq_server-3.6.2\sbin" and press enter )
enable management plugin (type "rabbitmq-plugins enable rabbitmq_management" and press enter)
open a broswer window to the management console & navigate to the admin section (http://localhost:15672/#/users with credentials "guest" - "guest")
add a new user (for example "the_user" with password "the_pass"
give that user permission to virtual host "/" (click user's name then click "set permission")
Now if you modify the connection info as done in the following modification of send.py you should find success:
#!/usr/bin/env python
import pika
credentials = pika.PlainCredentials('the_user', 'the_pass')
parameters = pika.ConnectionParameters('132.45.23.14',
5672,
'/',
credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello W0rld!')
print(" [x] Sent 'Hello World!'")
connection.close()
Hope this helps
See http://pika.readthedocs.org/en/latest/modules/parameters.html, where it says 'rabbit-server1' you should enter the remote host name of the IP.
Be aware that the guest account can only connect via localhost https://www.rabbitmq.com/access-control.html

Can't connect to server through SSH in Django application

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)

Categories