I have followed many question related to this topic.
My scenario:
Local host -> Gateway -> Remote host
I am using env.gateway variable to specify gateway host.
sample code
env.user = "ec2-user"
env.key_filename = ["/home/ec2-user/.ssh/internal.pem","/home/roshan.r/test.pem","/home/ec2-user/.ssh/test2.pem"]
env.hosts = ['x.x.x.244', 'x.x.x.132']
env.gateway = 'x.x.x.189'
def getdate():
content = run('date')
My problem is with pem key path.
/home/roshan.r/test.pem is located in current directory. which is used for login into gateway server.
Other two mentioned pem files are located in gateway server.
When i run this program i'm getting file not found error.
Thanks for any help !!
I havn't had to do this yet, but what about having a function that fecth those pem file ? something like :
#'x.x.x.189'
def get_pem():
env.key_filename.append(get("/home/ec2-user/.ssh/internal.pem")
env.key_filename.append(get("/home/ec2-user/.ssh/test2.pem")
Also, I could you try something ? i guess you got a fiel not found because fabric is looking for the /home/ec2-user/.ssh/internal.pem on your computer. It has no way knowing it's on a remote host. What if you try with :
x.x.x.189:/home/ec2-user/.ssh/internal.pem
I just changed path of the .pem file and It works. See below suggestion:
Keep gateway and app server .pem file in your local machine and try to execute it. See below my code.
from fabric.api import *
env.user = "ubuntu"
env.key_filename = ["~/folder/sub_folder/gate_way_instance.pem", "~/folder/sub_folder/test_server_ssh-key.pem"]
env.hosts = ['XX.XX.XX.XXX']
env.gateway = 'XX.XX.XX.XXX'
def uptime():
content = run('cat /proc/uptime')
print content
content = run('ls -la')
print content
Related
I am new to Python and sorry for my bad english.
I'm trying to save a file "toto.txt" from my HDD "d:" to my Synology NAS.
So I'll use paramiko for this, here is the code :
import paramiko
import os
ip_address = "my nas ip"
username = "my username"
password = "mypass"
utilisateur = os.getenv("USERNAME") // to get Windows username
remote_path = f"{utilisateur}/test.txt" // the file downloaded
local_path = "d:/toto.txt" //the file stored on my pc
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname=ip_address,username=username,password=password)
print("Connexion OK >", ip_address)
stdin, stdout, stderr = ssh_client.exec_command(f'mkdir {utilisateur}') //creating folder for the
user
sftp = ssh_client.open_sftp()
sftp.put(local_path,remote_path) // trying to send the file
sftp.close()
ssh_client.close()
i am not getting error but nothing is happening.
The folder is successful created but no file is sending in it.
Have someone an idea?
thanks a lot
If Paramiko does not throw any error, the upload was successful. The file just possibly ended in different location than you wanted/than you look to.
At least for a test, if not permanently, try an absolute absolute path. Make no guesses, use the exact path you see in your GUI SFTP client.
Another possibility is that the server automatically processes the uploaded file somehow and moves it away.
i am writing a python script that helps me download files from sftp server to my local folder.
when i run the script it just downloads the blank document
i keep on trying but i am failing
i made a sftp server to test the code and the path specified in remote path is server root directory
i giving code below
import pysftp
myHostname = "192.168.56.1"
myUsername = "new45"
myPassword = "146515"
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection(host=myHostname, username=myUsername, password=myPassword,cnopts=cnopts) as sftp:
# Define the file that you want to download from the remote directory
remoteFilePath = 'C:\\Users\\Simon\\Desktop\\ftp\\testfile.txt'
# Define the local path where the file will be saved
# or absolute "C:\Users\sdkca\Desktop\TUTORIAL.txt"
localFilePath = 'C:\\Users\\Simon\\Desktop\\ftp2\\textfile.txt'
sftp.get(remoteFilePath, localFilePath)
# connection closed automatically at the end of the with-block'''
please tell me whats the error why is blank file is being downloaded
Login to that server using any GUI SFTP client. And observe what path syntax your server is using. It's definitely not C:\Users\Simon\Desktop\ftp\testfile.txt. SFTP uses forward slashes. So it can be like /C:/Users/Simon/Desktop/ftp/testfile.txt. But even something completely different, particularly if the server or your account are chrooted.
You are probably trying to use the path syntax, which you would have used, were you directly on that target Windows system. But your SFTP server presents the files using a different path syntax. You need to adhere to that syntax.
I am using pysftp on Python and am trying to run a loop for a certain directory in the sftp server.
I don't know how to write the directory paths on sftp servers. I thought that connecting to the server and just writing the directory as below would work, but it doesn't. Please let me know how to write sftp paths so that python can read them.
sftp = pysftp.Connection('128.59.164.112', username = '', password = '');
source = sftp.u\'weatherForecast\'/dataRAW/2004/grib/tmax/
Try this:
import pysftp
sftp = pysftp.Connection('hostname', username='me', password='secret')
sftp.chdir('/home/user/development/stackoverflow')
ls = sftp.listdir()
for filename in ls:
print filename
You should read this: http://pysftp.readthedocs.org/en/latest/index.html
PS1: ; is optional in Python, but not Pythonic.
After enough trial and error I figured it out.
source = 'weatherForecast/dataRAW/2004/grib/tmax/'
destination= 'sftp.u\'weatherForecast\'/csv/2004/tmax'
This works.
I am working on a project that requires us to upload a vile via SFTP to a remote server, and we are having troubles doing this. We tried following this youtube guide, but we are having some issues.
We are getting a "no such file" error when we run the script, and we know for sure that the file exists and that we have given the python script the right name and location for the file.
This is the script as we have it right now:
import pysftp as sftp
def sftpTry():
try:
s = sftp.Connection(host='babbage.cs.missouri.edu', username ='<username>', password = '<password>')
remotepath = '~it3001s14grp1/videos/newVideo/new.avi'
#localpath = '/etc/motion/capture/hello.txt'
localpath = '/etc/motion/capture/06--2014-05-15---16-16-25.avi'
s.put(localpath, remotepath)
s.close()
except Exception, e:
print str(e)
sftpTry();
You should begin your remote path with a forward slash "/". Also, check the directory you are specifying in the remotepath. You should try to do a pwd in the directory when you login into the server (say using ssh). The remote-path should be specified exactly like that.
Although you do have the filename name in the remote path, it would throw an error if you specify just the folder's name.
Another tip would be to use getpass instead of hard-coding the password:
passwd = getpass.getpass()
s = sftp.Connection(host='<host>', username = '<username>', password = passwd)
I am sure this has been resolved before but I cannot seem to find a similar Q&A (newbie)
Using Windows XP and Python 2.5, I m trying to use a script to connect to an FTP server and dowload files. It should be simple but following the instructions of similar scripts I get the errors:
ftp.login('USERNAME')
File "C:\Python25\lib\ftplib.py", line 373, in login
if resp[0] == '3': resp = self.sendcmd('PASS ' + passwd)
File "C:\Python25\lib\ftplib.py", line 241, in sendcmd
return self.getresp()
File "C:\Python25\lib\ftplib.py", line 216, in getresp
raise error_perm, resp
error_perm: 530 User USERNAME cannot log in.
The script I use is:
def handleDownload(block):
file.write(block)
print ".",
# Create an instance of the FTP object
# FTP('hostname', 'username', 'password')
ftp = FTP('servername')
print 'ftplib example'
# Log in to the server
print 'Logging in.'
# You can specify username and password here if you like:
ftp.login('USERNAME', 'password')
#print ftp.login()
# This is the directory
directory = '/GIS/test/data'
# Change to that directory.
print 'Changing to ' + directory
ftp.cwd(directory)
# Print the contents of the directory
ftp.retrlines('LIST')
I appreciate this might be a trivial question, but if anyone can provide some insights it would be very helpful!
Thanks, S
I can't understand which library are you using. Python standard urllib2 is sufficient:
import urllib2, shutil
ftpfile = urllib2.urlopen("ftp://host.example.com/path/to/file")
localfile = open("/tmp/downloaded", "wb")
shutil.copyfileobj(ftpfile, localfile)
If you need to login (anonymous login isn't sufficient), then specify the credentials inside the url:
urllib2.urlopen("ftp://user:password#host.example.com/rest/of/the/url")
ftp.login('USERNAME', 'password')
Replace this with real data. According to the error you are trying to login as "USERNAME" with the password "password" which obviously won't work.
Also, replace servername in ftp = FTP('servername')
with the hostname of the server you want to connect to.
the first trivial check would be to open an interactive session (i.e. ftp yourself to this server with the same credentials), to be sure that this is not a permission issue..
Another source of failure, you might need to give your username as domain\username when connecting to a MS ftp server.
Maybe that helps ?