I have been unsuccessful in finding a way to read a file from a remote server over ssh and then writing a similar file in the same way. Paramiko doesn't seem to work with 3.5 and i'm not sure what else i can do.
Example of what i'm trying to do:
from shutil import copyfile
copyfile('10.1.1.5:v3/ec/s//01_inventory.txt', '10.1.1.5:v3/ec/s//01_inventory_Bkup.txt')
You could use scp I suppose (https://ss64.com/bash/scp.html). You would have something like this to replace your copyfile call:
import subprocess
subprocess.call("scp 10.1.1.5:v3/ec/s/01_inventory.txt 10.1.1.5:v3/ec/s/01_inventory_Bkup.txt", shell=True)
You could also use scp to copy to your local host, modify it as you would normally and then replace the original.
Related
This code writes google.de has address 216.239.34.117 to the shell
import os
os.system("host google.de")
Now i want to save the IP-Address into a string variable, but i can`t figure out how i read from the command line. Or is there even an easier way to get the IP-Adress to a variable?
Thanks and Greetings,
Alex
Why won't you use socket library instead of os?
import socket
host_ip = socket.gethostbyname('google.de')
print(host_ip)
I'd like to open/display an excel file that I'm saving as part of python processing within the python script.
The save portion of the code works fine (ie, it saves successfully, and I'm open and view from a Nautilus window), but attempting to open programmatically throws errors no matter how I attempt it.
I've been using the Popen method within the subprocess package:
from subprocess import Popen
Popen('./temp/testtest.xlsx')
Gives:
PermissionError: [Errno 13] Permission denied: './temp/testtest.xlsx'
I subsequently tried changing file permissions:
import os
from subprocess import Popen
os.chmod('./temp/testtest.xlsx',0o777)
Popen('./temp/testtest.xlsx')
Which gave:
Out[127]: <subprocess.Popen at 0x7faeb22a4b00>invalid file (bad magic number): Exec format error
And against better judgement tried running as shell:
from subprocess import Popen
Popen('./temp/testtest.xlsx', shell=True)
Which gave:
invalid file (bad magic number): Exec format error
Out[129]: <subprocess.Popen at 0x7faeb22a46a0>
I also tried it with the file saved in a different directory with similar errors. If it matters, I'm using the openpyxl module to create and save the excel file, but I have the same issues even if it's an excel file I created manually.
The argument to Popen() needs to be a shell command to open the file. If you're using LibreOffice, the program is localc; if you're using OpenOffice it's oocalc.
from subprocess import Popen
import os
f = os.path.join('temp', filename)
Popen(['localc', f])
You have Many options here. You could install libreoffice, this is an open source office suite and is fairly decent you should be able to open that file directly with ooffice —-calc “filename”. If you really want to stay with python You could save the data to a .csv file, and pythons anaconda distribution has pandas library and you could read the .csv into a data frame fairly easily. import pandas as pd Then
pd.read_csv(“File_name.csv”) returns to you a dataframe, but remember to import os and os.chdir(r“/path/to/data”).
From that point pandas lets you easily access the data for plotting or manipulation.
Here is all the functionality of a data frame and see if it meets your fancy.
Python Pandas DataFrame
I am trying to make a python program that creates and writes in a txt file.
the program works, but I want it to cross the "hidden" thing in the txt file's properties, so that the txt can't be seen without using the python program I made. I have no clues how to do that, please understand I am a beginner in python.
I'm not 100% sure but I don't think you can do this in Python. I'd suggest finding a simple Visual Basic script and running it from your Python file.
Assuming you mean the file-properties, where you can set a file as "hidden". Like in Windows as seen in screenshot below:
Use operating-system's command-line from Python
For example in Windows command-line attrib +h Secret_File.txt to hide a file in CMD.
import subprocess
subprocess.run(["attrib", "+h", "Secret_File.txt"])
See also:
How to execute a program or call a system command?
Directly call OS functions (Windows)
import ctypes
path = "my_hidden_file.txt"
ctypes.windll.kernel32.SetFileAttributesW(path, 2)
See also:
Hide Folders/ File with Python
Rename the file (Linux)
import os
filename = "my_hidden_file.txt"
os.rename(filename, '.'+filename) # the prefix dot means hidden in Linux
See also:
How to rename a file using Python
I'm using paramiko to login in x machine then
after logged in machine x there are file in some path like
t="/abc/gfd/*" which
i want to copy to some path like
p=<username>#<machine-name/ip>:/dfg/hgf/kjh/.
cmd="pwd; scp {0} {1}".format(t,p)
pwd_of_machine_p="xyz"
cmd="xyz; scp {0} {1}".format(t,p)
then executing this cmd with paramiko obj like
client.exec_command(cmd)
which is not working .
when i do ls to this path "/dfg/hgf/kjh/" i get nothing in there.
You can use below simple approach without using paramiko.
import os
os.system("scp FILE USER#DEST_MACHINE:PATH")
#e.g. os.system("scp file.txt sunilt#xx.xx.xx.xx:/path/")
But before that you need to generate an ssh key on the source machine and copy it to the destination machine,so that the scp automatically gets authenticated with your public ssh key and script doesn't ask for a password.
Information about generating and copying ssh key is # ssh_keygen_exapmple
I spend a few hours writing a little script.
Basically what it does is create a new text file and fills it up with whatever.
I zip the text file --using zipfile-- and here's where my problem lies.
I want to run the Windows system command:
copy /b "imgFile.jpg" + "zipFile.zip" newImage.jpg
To merge the image "imgFile.jpg" and the zip "zipFile.zip".
So:
os.system("copy /b \"imgFile.jpg\" + \"zipFile.zip\" newImage.jpg")
When I run my script, it all seems to go fine.
But when it's done and I try to extract the 'newImage.jpg' file, it gives me:
The archive is either in unknown format or damaged
This ONLY happens when I run the system command within the script.
It works fine when I use the shell. It even works if I use a separate script.
I've double checked my zip file. Everything is in good shape.
Is there something I'm doing wrong? Something I'm not seeing?
Have you tried using shutil?
import shutil
shutil.copy(src, dst)
There may be a problem with the way Python is passing the arguments to the shell command. Try using subprocess.call. This method takes arguments as an array and passes them that way to the command:
import subprocess
subprocess.call(["copy", "/b", '"imgFile.jpg" + "zipFile.zip"', "newImage.jpg"])