I want to write a python script that connects to the remote server and creates a zip file in the remote server which consists of specific files present in remote server itself. I have written the below script :
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect('15.100.1.1', username='user', password='123')
sftp=c.open_sftp()
c.exec_command("cd 'C:\\Program Files\\temp'")
filenames = ['a.txt','b.txt','c.txt']
zf = zipfile.ZipFile('files.zip', mode='w')
for fname in filenames:
zf.write(fname)
zf.close()
sftp.close()
c.close()
But instead of creating zip file in remote server, the zip file gets created in the local machine itself. Can anyone please help me in this.....
When you create the zip-file you refer to a local file files.zip:
zf = zipfile.ZipFile('files.zip', mode='w')
You never tell it to create the file remotely. But you could probably (haven't tried it myself) create a remote file and send the handle to ZipFile. The issue with that is that you will run the zipping on you local machine instead of on the remote machine. That will have all your files be moved over network back and forth to no use other than creating the remote file. Try to do the zipping directly on the remote machine instead!
As UlfR says,
"Try to do the zipping directly on the remote machine".
Since he (she?) didn't say how to do that, I suggest something like
c.exec_command("zip files.zip a.txt b.txt c.txt")
As suggested by G-Man Says 'Reinstate Monica';
I would like to add that in order to create a zip in a specific location; use the following code:
import paramiko
host = "" #Enter host name
port = "" #Enter port number
username = "" #Enter username
password = "" #Enter password
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(host, port, username, password)
sftp = ssh.open_sftp()
Now, we can either zip a group of files, single file or a directory.
#1 to Zip a single file
ssh.exec_command("zip /location_to_save_zip/zipname.zip /location_of_file/file1.txt")
#2 To zip multiple files
ssh.exec_command("zip /location_to_save_zip/zipname.zip /location_of_file/file1.txt /location_of_file/file2.txt")
#3 To zip a folder
ssh.exec_command("zip -r /location_to_save_zip/zipname.zip /location_of_directory")
Close the SFTP and Paramiko SSH Client connection
sftp.close()
ssh.close()
Related
I am developing a WPF application, that has a function to run the "CMD" in user pc and navigate to one folder "platform-tools" that included in the application files and execute a command.
string Request = " /c" + "cd../../&cd platform-tools& adb reboot ";
Process procc = new Process();
ProcessStartInfo procStartInfo2 = new ProcessStartInfo(#"cmd.exe", Request);
As we know, when the user install the application in his PC, the application will be located in C:\Users\""UserName\AppData\Local\Apps\2.0\"random name like: JBJHV6HG7HG"
so its difficult to know where is exactly the "platform-tools" will be installed.
My question is: is there any way i can know how to reach the platform-tools folder by my "Request" in order to rung the adb command?
or
is there a way to install the "platform-tools" in different location like "User's desktop" then i can change the "CD" command in CMD to navigate to user's desktop or C drive?
Using the ClickOnce, you won't be able to change the destination install folder due some security features and so on...
you can find out more Here
You can get the path of your executing process, and concatenate the folder name to reach the "platform-tools" folder:
//There's two ways to get the current file address from your application (in the end both ends in the same):
//There's two ways to get the current file address from your application (in the end both ends in the same):
//getting the filename by the process
var cc = Process.GetCurrentProcess().MainModule.FileName;
//getting the filename by the executing assembly
var dd = System.Reflection.Assembly.GetExecutingAssembly().Location;
//getting the path for that file name
string assemblyPath = Path.GetDirectoryName(cc);
var platformTools = string.Concat(assemblyPath, #"/platform-tools/someprocess.exe");
Process.Start(platformTools);
I have some movie files stored on a FileFub Plus which is a wifi hub with a sd card – which I can access over my LAN. Once I connect to the FHP’s wifi (entering password etc) I can access the files through my Win7 File Manager using the address \\10.10.10.254.
What I want to be able to do is run a cmd batch file to output all the file names to a txt file on my PC’s hard drive.
Normally, it w/b easy to use a cmd such as, (which FYI is the cmd I use to output my MyCloud movie files),
dir \\192.168.1.111\moviez /s > c:\”MyCloud_Movies.txt”
So I tried the following;
dir \\10.10.10.254\SDCard_Volume1\Movies /s > c:\”FileHub Plus Movies.txt”
FYI – the ‘root’ (share) directory of the FHP is labeled SDCard_Volume1 and then the movies are in a sub dir called Movies.
Also FYI my connection to the FHP, using File Manager, does not display a drive letter.
When I run the above cmd it doesn’t output the file names to the txt file – and I get an error “Logon failure; unknown user or bad password” (even though I have the \\10.10.10.254 location already open in File Manager on my notebook)…
So I guess my first questions are what am I doing wrong and how would I input the password so I can run this batch fle to create a list of my movies on the FHP…?
I have two test Machines. i don't know if this is possible but here is what im trying to do
Machine1: has DBServer setup
where i am currently logged in
where the file C:\test_IDNumber1.txt is located
where i use SSMS. i connect to Machine2\SQLServer
Machine2: has DBServer setup
where the dbserver for DBTest is located
DBTest is where the Filename records are stored
First i need to do some select script from DBTest to get the IDNumber for the Filename
SELECT FileName FROM tblFiles where...... (results to "IDNumber1")
Then i'll check the existence of the file with the same IDNumber (e.g: "C:\test_IDNumber1.txt") using this script
EXEC xp_fileexist 'C:\test_IDNumber1.txt', #exists OUTPUT
If the file exists, i need to delete the record from DBtest on Machine2 and the file from Machine1
DELETE FROM tblFiles where Filename = 'IDNumber1'
EXEC master.dbo.xp_cmdshell 'del ''C:\test_IDNumber1.txt'''
but the result for existence is always 0 because it is checking the C drive of Machine2 where the DBTest is located.
Is there a way to use a select script from a db from Machine2 and check the existence of a file from Machine1?
thank you very much
Several possible solutions can exist. One of the least expensive is:
Make file storage on Machine1 shared, visible to Machine2 and mapped there as network drive (e.g. Z:) with read+delete permissions.
Machine1: C:\Shared\ (mapping)→ Machine2: Z:\
Then you can use SQL commands similar to what you already show in steps 4 and 5 in order to test the existence of Z:\test_IDNumber1.txt and delete the file.
I am trying to use a cmd file to run a batch file that uploads a file of mine to a directory of my website. The batch file looks like this:
open ip address
username
password
option confirm off
cd \public_html\wp-content\uploads\2014\07
put "C:\Users\a\Documents\pdf\Contact Info.pdf"
quit
The cmd file looks like this:
ftp -s:c:\Users\a\Documents\upload.bat
When I run the cmd file I get output in command prompt that says that my username and password are ok and then tells me that option confirm off is an invalid command and that the cd filepath is a prohibited file name. It then says my file is uploaded but I can't find where the file is put. Is there a reason why this is happening? Is it possible to upload the file to that directory?
Use winscp. Download: http://winscp.net/eng/download.php
option confirm off is a valid WinSCP command, see http://winscp.net/eng/docs/scriptcommand_option
Example: Uploading a single file with WinSCP
http://winscp.net/eng/docs/script_upload_single_file
Step 1
create file: winscp-upload-script.txt
option batch abort
option confirm off
open sftp://username:password#example.com/
put "C:\Users\a\Documents\pdf\Contact Info.pdf" \public_html\wp-content\uploads\2014\07
exit
Step 2
create file: do-upload.bat
"C:\Program Files (x86)\WinSCP\winscp.com" /script=winscp-upload-script.txt
Step 3
Go scheduler, create new task, like described here (http://www.sevenforums.com/tutorials/12444-task-scheduler-create-new-task.html) and use do-upload.bat and configure event timing.
-Done-
me again :P
I have some problems with some bat file, that bat file , connects to and ftp and download all files from a remote folder, then delete it, but my problem/question is: i need a txt(log) of every file before download it from the remote server, here is my file
bat file:
ftp -i -s:ftpfile.txt site.com
txt file
user-name
user-pass
lcd c:\localfolder\some\folder
cd remotefolder-name
mget .
mdelete \\remotefolder-name\ .
quit
If i use the >>mylog.txt on this line:
ftp -i -s:ftpfile.txt site.com>>mylog.txt
I get some extra data that i dont want to. I just need the file names before download it, something like this:
log.txt
file001x.xml
filedfx.xml
file023x.xml
filed33x.xml
Ps:sorry for my english ;)
You may redirect ls output to local file. Syntax is: ls pattern local_file. An example would be: ls * ftplist.txt or ls . ftplist.txt. Both will produce a file ftplist.txt (in local current directory if you do not specify path) with a list of files (and possibly subdirs) in current directory on remote server (in slightly different formats)