Copy files from Linux to Windows using Samba client in Python - python

I am trying to copy a file from a Linux machine to a Windows shared drive. This answer shows how perform this action using the Samba client. In my case, the connection succeeds, listShares() returns the shares on the server but when I call connection.storeFile() I get Unable to connect to shared device.
What am I missing?
The python code running on the Linux machine:
connection = SMBConnection(username='user1', password='password1', my_name='host1',remote_name='fs01',domain='domain', use_ntlm_v2=True)
connection.connect(ip, 139) # Returns true
file_obj = open(filename,"r")
connection.storeFile('\\\\fs01\\data\\folder1', file1, file_obj)
smb.smb_structs.OperationFailure: Failed to store data\folder1\file1 on fs01: Unable to connect to shared device

Related

How to send data from a file in a Python docker container to remote SFTP server?

I have a Python script I am trying to run in a Docker container to send a file that is on this container to an SFTP server.
I tried the following :
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session = ssh.connect(hostname="X", port=X, username='X', password="X")
stdin,stdout,stderr = ssh.exec_command('sftp -P X ldzdl#hostname', get_pty=True)
I also tried with paramiko transport method but didn't work from remote (docker container) to remote SFTP.
But I have the following error : paramiko.ssh_exception.AuthenticationException: Authentication failed.
How can I do this ? I don't know if my method is okay or if there is other better way to solve it (send data from container to an SFTP server).
The argument given to the exec_command function is not the command you would normally run on the local (client) host's shell, but rather attempted to be ran on the remote (server) host. While it is not likely to get an AuthenticationException by attempting to run remote commands, as you did not post a full traceback in the question - it is hard to tell for sure.
I suggest checking the following code:
import paramiko
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
session = ssh.connect(hostname="X", port=X, username='X', password="X")
### so far, same code as in the question ###
print("Auth OK")
sftp_connection = ssh.open_sftp()
sftp_connection.put("local/file/path", "remote/file/path")
If you see the "Auth OK" print - then you should be good to go, just replace the file path arguments of the sftp_connection.put() method with actual local and remote file paths.
Otherwise - there is an actual authentication issue which should be resolved.

How to communicate Python with Siemens PLC using Python-snap7?

I am trying to communicate Python with the CPU1212C PLC (using PLCSIM), but an error always occurs.
Code:
IP = '192.168.100.100'
RACK = 0
SLOT = 1
plc = snap7.client.Client()
plc.connect(IP, RACK, SLOT)
print(plc.get_cpu_state())
No handlers could be found for logger "snap7.common"
Traceback (most recent call last):
File "C:/Python27/Teste_Snap7.py", line 8, in plc.connect(IP, RACK, SLOT)
File "C:\Python27\lib\site-packages\snap7\client.py", line 25, in f check_error(code, context="client")
File "C:\Python27\lib\site-packages\snap7\common.py", line 65, in check_error raise Snap7Exception(error)
Snap7Exception: TCP : Connection timed out
Is this a problem with Windows10?
I am using Windows10 64 bit, Python 2.7.17, Snap7 1.1.0, Python-Snap7 0.10.
I copied and pasted the snap7.dll and snap7.lib file into the System32, Python27, Python27 / site-packages / snap7 folders. And I created for each folder a path in the environment variables in an attempt to work.
I followed this tutorial: https://www.youtube.com/watch?v=BKnK4AT_WKs
It's not a problem related with Windows, in the error message you can see the problem:
Snap7Exception: TCP : Connection timed out.
Verify your physical connection to the machine and then verify the client IP.
Verify:
if PLC actually ping
if Snap7 server is enabled on your PC
if rack and slot are correct, according to your PLC.
The handbook provided with snap7 is very exhaustive, please refer to it
Verify in tia portal:
properties > Protection and security > connection mechanism > and check the "permit acess with PUT/GET.."

pyfluidsynth running on RaspberryPi errors

I'm trying to play midi sounds from my python program on a RaspberryPi, however when trying to initialise fluidsynth, it errrors:
>>> import fluidsynth
>>> import time
>>> fs = fluidsynth.Synth()
>>> fs.start()
Causes the following errors:
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jackdmp 1.9.9
Copyright 2001-2005 Paul Davis and others.
Copyright 2004-2012 Grame.
jackdmp comes with ABSOLUTELY NO WARRANTY
This is free software, and you are welcome to redistribute it
under certain conditions; see the file COPYING for details
JACK server starting in realtime mode with priority 10
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
Cannot lock down 82278944 byte memory area (Cannot allocate memory)
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
jack server is not running or cannot be started
fluidsynth: error: Failed to connect to Jack server.
I think the final message can be fixed by starting the Jack server, however I can't fix the
Cannot connect to server socket err = No such file or directory
Cannot connect to server request channel
errors
Anyone know how?
The default fluidsynth settings apparently are not appropriate for the Raspberry Pi.
Change the audio driver settings so that uses alsa instead of jack.
If you don't know how to do this in Python, start fluidsynth manually from the command line, and tell your Python program to output to fluidsynth's port.

Problems in connecting to MusicBrainz database using psycopg2

I am trying to connect to the MusicBrainz database using the psycopg2 python's module. I have followed the instructions presented on http://musicbrainz.org/doc/MusicBrainz_Server/Setup, but I cannot succeed in connecting. In particular I am using the following little script:
import psycopg2
conn = psycopg2.connect( database = 'musicbrainz_db', user= 'musicbrainz', password = 'musicbrainz', port = 5000, host='10.16.65.250')
print "Connection Estabilished"
The problem is that when I launch it, it never reaches the print statement, and the console (I'm on linux) is block indefinitely. It does not even catches the ctrl-c kill, so I have to kill python itself in another console. What can cause this?
You seem to be mistaking MusicBrainz-Server to be only the database.
What's running on port 5000 is the Web Server.
You can access http://10.16.65.250:5000 in the browser.
Postgres is also running, but listens on localhost:5432.
This works:
import psycopg2
conn = psycopg2.connect(database="musicbrainz_db",
user="musicbrainz", password="musicbrainz",
port="5432", host="localhost")
print("Connection established")
In order to make postgres listen to more than localhost you need to change listen_addresses in /etc/postgresql/9.1/main/postgres.conf and make an entry for your (client) host or network in /etc/postgresql/9.1/main/pg_hba.conf.
My VM is running in a 192.168.1.0/24 network so I set listen_addresses='*' in postgres.conf and in pg_hab.conf:
host all all 192.168.1.0/24 trust
I can now connect from my local network to the DB in the VM.
Depending on what you actually need, you might not want to connect to the MusicBrainz Server via postgres. There is a MusicBrainz web service you can access in the VM.
Example:
http://10.16.65.250:5000/ws/2/artist/c5c2ea1c-4bde-4f4d-bd0b-47b200bf99d6.
In that case you might be interested in a library to process the data:
python-musicbrainzngs.
EDIT:
You need to set musicbrainzngs.set_hostname("10.16.65.250:5000") for musicbrainzngs to connect to your local VM.

How to Transfer Files from Client to Server Computer by using python script?

I am writing a python script to copy python(say ABC.py) files from one directory to another
directory with the same folder name(say ABC) as script name excluding .py.
In the local system it works fine and copying the files from one directory to others by
creating the same name folder.
But actually I want copy these files from my local system (windows XP) to the remote
system(Linux) located in other country on which I execute my script. But I am getting
the error as "Destination Path not found" means I am not able to connect to remote
that's why.
I use SSH Secure client.
I use an IP Address and Port number to connect to the remote server.
Then it asks for user id and password.
But I am not able to connect to the remote server by my python script.
Can Any one help me out how can I do this??
paramiko provides a SFTPClient that can be used to do this.
import paramiko
source = r'C:\Somedir\somefile.txt'
dest = r'/home/user/file.txt'
hostname = 'linux.server.com'
port = 22 # default port for SSH
username = 'user'
password = 'secret'
try:
t = paramiko.Transport((hostname, port))
t.connect(username=username, password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(source, dest)
finally:
t.close()
I used the same script, but my host failed to respond. My host is in different network.
WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Categories