paramiko sftp.get - python

I am trying to use paramiko to download a file via SFTP. I create the SFTP object like this:
transport = paramiko.Transport((sftp_server, sftp_port))
transport.connect(username = sftp_login, password = sftp_password)
sftp = paramiko.SFTPClient.from_transport(transport)
sftp.get("file_name", '.', None)
and, I get the exception:
Exception python : Folder not found: \\$IP_ADDRESS\folder_1/folder_2\file_name.
I'm running paramiko to connect to a client chrooted SFTP. The file, 'file_name', is located at the root of my client's chroot.
I don't get why I have this error showing apparently the full path (outside the chroot) of my client's server.
I don't know why my dummy file is not going to be downloaded :O
I will provide any necessary information.

The following code worked for me in Ubuntu 11.10:
sftp.get("file_name", "file_name")
I just made a couple of changes that shouldn't affect to your problem:
localpath: Used full path to the local file name instead of just '.' (directories aren't allowed)
callback: Removed it since None is already the default value and that's not really needed
Since I'm not getting the same error you're getting regarding the remotepath parameter, I guess you might be using a different sftp server that has a different behaviour.
My advice would be to:
Verify with another client, for example the sftp command, that the file you're looking for is really where you are trying to get it.
Use sftp.chdir just to make sure that the default directory being used is the one you expect.
I hope this helps.

Related

Connect vici socket to a particular network namespace

I am able to use python3-vici in the global namespace, suppose I want to route it through a particular namespace say, /var/run/x/x/vpn, how do I do that?
I have charon.ctl, charon.pid, ipsec.conf, ipsec.d, starter.charon.pid, strongswan.conf
files in the vpn folder but not charon.vici. I tried installing vici in the namespace, but I don't see a charon.vici file there.
Anything I'm missing here?
Another thing:
I'm not able to map the certificates I have loaded using vici.Session().load_cert() with a particular connection. Using a 'cert' attribute in a connection dictionary inside 'local' throws an error like:
vici.exception.CommandException: Command failed: b'unknown option: certs, config discarded'
Although, if you load the connection using swanctl.conf, and then retrieve information using vici, you can see the cert field being populated on doing a list_conns().

Connect another computer in local network

I'm trying to connect another computer in local network via python (subprocesses module) with this commands from CMD.exe
net use \\\\ip\C$ password /user:username
copy D:\file.txt \\ip\C$
Then in python it look like below.
But when i try second command, I get:
"FileNotFoundError: [WinError 2]"
Have you met same problem?
Is there any way to fix it?
import subprocess as sp
code = sp.call(r'net use \\<ip>\C$ <pass> /user:<username>')
print(code)
sp.call(r'copy D:\file.txt \\<ip>\C$')
The issue is that copy is a built-in, not a real command in Windows.
Those Windows messages are awful, but "FileNotFoundError: [WinError 2]" doesn't mean one of source & destination files can't be accessed (if copy failed, you'd get a normal Windows message with explicit file names).
Here, it means that the command could not be accessed.
So you'd need to add shell=True to your subprocess call to gain access to built-ins.
But don't do that (security issues, non-portability), use shutil.copy instead.
Aside, use check_call instead of call for your first command, as if net use fails, the rest will fail too. Better have an early failure.
To sum it up, here's what I would do:
import shutil
import subprocess as sp
sp.check_call(['net','use',r'\\<ip>\C$','password','/user:<username>'])
shutil.copy(r'D:\file.txt,r'\\<ip>\C$')
you need make sure you have right to add a file.
i have testted successfully after i corrected the shared dirctory's right.

How to debug / correctly setup git-multimail

I would like to use git-multimail as post receive hook in one of my git repositories (no gitolite used). Unfortunately, I cannot get it work, and I have hardly any experience using Python.
What I did so far:
I added the following block to the project.git/config file:
[multimailhook]
mailingList = email#example.com
from = email#example.com
envelopeSender = email#example.com
mailer = smtp
smtpServer = smtp.mydomain.com
smtpUser = myUser
smtpPass = myPassword
Please note that I do not know whether "smtp", which is defined in the mailer variable, is installed on my machine.
I copied the current git_multimail.py file into project.git/hooks.
I created a project.git/hook/post-receive file with the following content. The file is executable, I copied this from https://github.com/git-multimail/git-multimail/blob/master/git-multimail/post-receive.example
#! /usr/bin/env python
import sys
import os
import git_multimail
config = git_multimail.Config('multimailhook')
try:
environment = git_multimail.GenericEnvironment(config=config)
#environment = git_multimail.GitoliteEnvironment(config=config)
except git_multimail.ConfigurationException:
sys.stderr.write('*** %s\n' % sys.exc_info()[1])
sys.exit(1)
mailer = git_multimail.choose_mailer(config, environment)
git_multimail.run_as_post_receive_hook(environment, mailer)
What happens:
When I push a change, a file project.git/hooks/git_multimail.pyc is created, but no email is sent.
Doing a configuration test using GIT_MULTIMAIL_CHECK_SETUP=true python git_multimail.py as described on https://github.com/git-multimail/git-multimail/blob/master/doc/troubleshooting.rst tells me that git-multimail seems properly set up
Is there a way to log something like an output of the script? What can I do to find out what is not working? Are there even errors in my files?
Thanks in advance.
Using post-receive.example is by far not the simplest way to set up git_multimail.py: as the header of post-receive.example script says:
The simplest way to use git-multimail is to use the script
git_multimail.py directly as a post-receive hook, and to configure it
using Git's configuration files and command-line parameters.
In other words, just
cp /path/to/git_multimail.py /path/to/project.git/hooks/post-receive
and you're all set (since you already have project.git/config filled-in). See Invocation in git-multimail's README for a bit more detail.
(note: admitedly, the doc is not so clear for beginners, I'll try to improve that when I get time)
OK guys, the error was probably as little as it could be. I did just one very little mistake in the post receive hook file: The sys.exit(1) command is not indented.
So, the WRONG version from my question:
try:
environment = git_multimail.GenericEnvironment(config=config)
except git_multimail.ConfigurationException:
sys.stderr.write('*** %s\n' % sys.exc_info()[1])
sys.exit(1)
CORRECT is (compare last line):
try:
environment = git_multimail.GenericEnvironment(config=config)
except git_multimail.ConfigurationException:
sys.stderr.write('*** %s\n' % sys.exc_info()[1])
sys.exit(1)
Like I said, I hardly know Python, so I did not pay attention to the indents. After correcting this, the email was sent, so feel free to use the above steps as a little tutorial for setting up git-multimail the "easiest" way. (I did not find a tutorial for this exact solution.)

how to call function in paramiko

for transfer entire folder to server using sftp with paramiko. I copy this code from stackoverflow
but my doubt is how to call that function, I put like this ..
sftp = paramiko.SFTPClient.from_transport(t)
M = MySFTPClient()
M.put_dir()
M.mkdir()
but Its throwing this error:
*** Caught exception: <type 'exceptions.TypeError'>: __init__() takes exactly 2 arguments (1 given)
The error message indicates that the function you are calling takes two arguments while you are sending zero. Try doing something like this instead:
t = paramiko.Transport(("ftpexample.com", 22))
t.connect(username = myusername, password = mypassword)
sftp = paramiko.SFTPClient.from_transport(t)
Use the sftp client to upload your file at localpath (e.g. /usr/tmp/test.png") to your remote path:
sftp.put("localpath","remotepath")
I haven't used Paramiko, but reading the source code it seems that you can already use the sftp object returned from the from_transport method. So no need to create another MySFTPClient()
In a Python console try reading help(paramiko.SFTPClient) and help(paramiko.SFTPClient.from_transport). Also browsing sftp.py seems helpful as the list of available commands is in the beginning (put_dir does not seem to be one of them).

Paramiko path specification

I have the following code for a GUI that I am developing:
sftp = self.ssh.open_sftp()
try:
localpath="/Users/..../signals.txt"
remotepath = "/data1/.../sd_inputs.txt"
sftp.put(localpath, remotepath)
The 'localpath' is my laptop but since this is a GUI and I am developing it for users who have their own laptops/computers, is there a command in Paramiko that lets me avoid or circumvent the localpath specification, in much the same way that os.system does for python?
From your example
os.system("SD_%s.xls" % (self.input2.GetValue()))
there is nothing special about os.system here. You call self.input2.GetValue() to format a string that you pass to os.system. You can do something similar with paramiko except you have to deal with the problem that the local and remote paths are different. Assuming you have a GUI form that gives both pieces of information, it will look something like:
sftp.put(self.localpath.GetValue(), self.remotepath.GetValue())
Actually I just discovered that I don't have to find a command in Paramiko that can circumvent or avoid the local path. Rather Python can find the local path as follows:
import os
path = os.getcwd()
localpath = path + "/signals.txt"
print localpath
I just have to put this stuff in the try block.

Categories

Resources