Python Forum
run command on system with multi qoutes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
run command on system with multi qoutes
#1
hello all ...
im trying yo execute this command on my system via powershell :

Quote:powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = 'myUserAgentString';$cli.DownloadFile('https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m', '%appdata%\putty.exe')"

i keeep getting this error SyntaxError: invalid syntax ... the problem from qoutes !!
can u guide me how to execute it and how to deal with multi qoutes ?
my code :

def downloadfile():
    down = subprocess.call('powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = 'myUserAgentString';$cli.DownloadFile('https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m', '%appdata%\putty.exe')"')
    time.sleep(20)
    


downloadfile()
Reply
#2
Ok,

is this the end goal?

download file: 'https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m' ?

if so, why not do the whole thing with python?
Reply
#3
(Aug-30-2018, 11:21 PM)Larz60+ Wrote: Ok,

is this the end goal?

download file: 'https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m' ?

if so, why not do the whole thing with python?
yes right ...
python download library not a friend with antivirus :)
and i need it in command line please
Reply
#4
Quote:python download library not a friend with antivirus :)
please elaborate. ? How so
Reply
#5
(Aug-31-2018, 10:45 AM)Larz60+ Wrote:
Quote:python download library not a friend with antivirus :)
please elaborate. ? How so


Sometimes anti-virus classify this behavior as suspicious because its try to download a program in the background ... but when u use powershell it will pass it because powershell comes with windows by default ( signed by microsoft )

can u help me now to execute the command
Reply
#6
Not all firewalls are written by windows.
And Even when I use MS windows, I have never had a problem downloading unless the download site was trying to prevent it.
I tested your url, and had no problem.

to use this, requests must be installed:
pip install requests
i used:
import requests

def get_file(url, savefile=None, mode='w'):
    response = requests.get(url)
    if response.status_code == 200:
        if savefile:
            with open(savefile, mode) as fp:
                fp.write(response.content)                          


if __name__ == '__main__':
    url = 'https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m'
    get_file(url, 'pink.exe', 'wb')
Reply
#7
(Aug-31-2018, 11:13 AM)Larz60+ Wrote: Not all firewalls are written by windows.
And Even when I use MS windows, I have never had a problem downloading unless the download site was trying to prevent it.
I tested your url, and had no problem.

to use this, requests must be installed:
pip install requests
i used:
import requests

def get_file(url, savefile=None, mode='w'):
    response = requests.get(url)
    if response.status_code == 200:
        if savefile:
            with open(savefile, mode) as fp:
                fp.write(response.content)                          


if __name__ == '__main__':
    url = 'https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m'
    get_file(url, 'pink.exe', 'wb')

thank u for ur help my brother but i need it via powershell :) i need to run this command on the system :
Quote:powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = 'myUserAgentString';$cli.DownloadFile('https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m', '%appdata%\putty.exe')"
Reply
#8
By system, I expect you mean command line.
if you modify the python script like:
import requests
import sys
 
def get_file(url, savefile=None, mode='w'):
    response = requests.get(url)
    if response.status_code == 200:
        if savefile:
            with open(savefile, mode) as fp:
                fp.write(response.content)                          
 
 
if __name__ == '__main__':
    url = sys.argv[1]
    save = sys.argv[2]
    get_file(url, save, 'wb')
save it as getfile.py
now you can run python getfile/py 'url' 'saveas' from the system command line and also be able to call it from
inside another python program by importing getfile, and calling get_file directly
example:
python getfile.py 'https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m' pink.exe
it will do what you want. for any url and any save name
Reply
#9
sorry my brother u did not understand me ...
i need it inside my program not with external arguments

like that ..
import subprocess


def downloadfile():
    down = subprocess.call('whoami')



downloadfile() 
i just need to replace whoami with :
Quote:powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = 'myUserAgentString';$cli.DownloadFile('https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m', '%appdata%\putty.exe')"
but becuase the command have a lot of "" i cant understand how to execute it i keep got : SyntaxError: invalid syntax
i need to execute it from cmd without any external arguments
check the photo
[Image: Untitled.png]

i mean i dont need to download the file by python ... i need to download the file by powershell command executed by python
Reply
#10
replace:
powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = 'myUserAgentString';$cli.DownloadFile('https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m', '%appdata%\putty.exe')"
with:
mycommand = "powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = {};$cli.DownloadFile('https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m', '%appdata%\putty.exe')".format(myUserAgentString)
then command is:
def downloadfile():
    mycommand = "powershell -command "$cli = New-Object System.Net.WebClient;$cli.Headers['User-Agent'] = {};$cli.DownloadFile('https://drive.google.com/uc?export=download&id=19LJ6Otr9p_stY5MLeEfRnA-jD8xXvK3m', '%appdata%\putty.exe')".format(myUserAgentString)
    down = subprocess.call(mycommand)
    time.sleep(20)

 
downloadfile()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Os.system("shutdown"); command not found cosmin1805 4 1,676 Nov-13-2022, 02:07 PM
Last Post: cosmin1805
  Help with multi-window system garynewport 3 2,375 Mar-16-2022, 03:05 AM
Last Post: deanhystad
  how to run linux command with multi pipes by python !! evilcode1 2 6,338 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,618 Jan-11-2021, 06:30 PM
Last Post: ykumar34
Question Difference between Python's os.system and Perl's system command Agile741 13 6,810 Dec-02-2019, 04:41 PM
Last Post: Agile741
  System Command sushmita13 3 3,715 Apr-25-2017, 11:53 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020