Running Curl command in python - python

I'm running the following curl command in CGI script in python using os.system call as below:
os.system('curl -u username:password -X PUT example.com/data/data1.txt -T /diskless/desktop/file.txt\r\n')
Getting the below error when I run the CGI script:
'!rl: Can't open '/diskless/desktop/file.txt') curl: try 'curl
--help' or 'curl --manual' for more information.
Any suggestions please?

you can use subprocess
import subprocess
command = 'curl -u username:password -X PUT example.com/data/data1.txt -T /diskless/desktop/file.txt\r\n'
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()

Related

subprocess command execution

What is the best way to execute the below command in Python in a single line?
echo $(readlink /sys/dev/block/$(mountpoint -d /))
Tried using individual os.system(cmd) by separating - "mountpoint -d /" first and taking the output and appending to "readlink /sys/dev/block/${0}".format(out.strip()) and doing an echo works. Tried using subprocess and subprocess.Popen and subprocess.check_output but it raises raise CalledProcessError
cmd = "echo $(readlink /sys/dev/block/$(mountpoint -d /))"
You have to call the subcommand separately. And you can use python methods to read the link:
import subprocess
import os
path = "/"
device = subprocess.run(["mountpoint", "-d", path], stdout=subprocess.PIPE, encoding="utf8").stdout.strip()
link = os.readlink("/sys/dev/block/" + device)
print(link)
You probably want to use something like the following:
cmd = "bash -c 'echo $(readlink /sys/dev/block/$(mountpoint -d /))'"
echo doesn't substitute $() blocks, that's what your shell does, so you have to call the shell. os.system(cmd) should work then.

How do I run a curl command in python

This is the command I am running in bash:
curl -d "username=xxxxxx" -d "password=xxxxx" <<address>> --insecure --silent
How can I run this using Python?
Try Subprocess:
import subprocess
# ...
subprocess.call(["curl -d \"username=xxxxxx\" -d \"password=xxxxx\" https://xxxxx.com/logreg/login/ --insecure --silent" , shell=True)
I did not try what I wrote, but the essential is here.
Check this page for more info : Subprocess management
I hope you don't want to explicitly run curl, but you just want to get the same result.
>>> import requests
>>> r = requests.get('https://example.com', auth=('user', 'pass'), verify=False)
>>> print(r.status_code)
200

cgi script faild to call another python script

I have a cgi script and it's calling another python script but while running via browser the second script not working.
subprocess.Popen([python, 'symantec.py', '-k', "test3.example.com.com.key", '-c',
"test3.example.com.com.csr", '-n', "test3.example.com.com",
'-o', "'IT'", '-p', "war.eagle", '-t', "Server", '-s', "F5",
'-y', "1", '-f', "test3.example.com.com.crt", '-g', "johny", '-l',
"mon", '-e', "johny.mon#example.com.com", '-b', "test3.example.com.com"],
shell=True)
Can someone help me to identify ?
Path to second script symantec.py
https://github.com/ericjmcalvin/verisign_purchase_ssl_certificate
You should first test the command.
python symantec.py -k test3.example.com.com.key -c test3.example.com.com.csr -n test3.example.com.com -o 'IT' -p war.eagle -t Server -s F5 -y 1 -f test3.example.com.com.crt -g johny -l mon -e johny.mon#example.com.com -b test3.example.com.com`
The script is expecting absolute paths as arguments to -k and -c arguments. The help specifies:
-h, --help show this help message and exit
-k KEY_FILE, --key-file=KEY_FILE
Full location to save key file. REQUIRED.
Once you made sure it is working, call it from your script:
command = "python symantec.py -k test3.example.com.com.key -c test3.example.com.com.csr -n test3.example.com.com -o 'IT' -p war.eagle -t Server -s F5 -y 1 -f test3.example.com.com.crt -g johny -l mon -e johny.mon#example.com.com -b test3.example.com.com"
subprocess.Popen(command.split(), shell=False)
Another way to accomplish this is to import the Symantec script and call its main function, providing, naturally, the expected arguments.

Python subprocess output read error

I have a command which works great at the terminal:
sudo tshark -V -l -i "any" -f 'udp port 4729'
I trying to read the output from my python script:
import subprocess
command = ['tshark', '-V', '-l', '-i', '"any"', '-f', '"udp port 4729"'] # the shell command
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=None)
output, error = process.communicate()
print output
It does not work. Maybe it's some troubles with writing a command in the list.
I receive the error:
gooman#ubuntu:~/workspace/glade_tests/src$ sudo ./main.py
tshark: Lua: Error during loading:
[string "/usr/share/wireshark/init.lua"]:45: dofile has been disabled
Running as user "root" and group "root". This could be dangerous.
Capturing on "any"
tshark: The capture session could not be initiated (No such device exists).
Please check to make sure you have sufficient permissions, and that you have the proper interface or pipe specified.
0 packets captured
Thy this:
import subprocess
command = "sudo tshark -V -l -i "any" -f 'udp port 4729'"
try:
output = subprocess.check_output(command, shell=True)
except subprocess.CalledProcessError as e:
print "An error has been occured", e
raise
print "The subprocess output:", output
Maybe, it will be needed to add stdout=subprocess.PIPE argument.

Subprocess calls acting differently in shell and interpretor

When I run the following commands in Python interpreter, all is good, but fail when run as a script. Somehow the opened tunnel is not recognized. Any pointers will be appreciated.
Interpreter:
gateway = subprocess.Popen("ssh -N sshgw", shell=True)
scp_prod = "scp -r server:/NFS/{0} .".format(filePath)
subprocess.call(scp_prod, shell=True)
gateway.kill()
Script:
try:
gateway = subprocess.Popen("ssh -N sshgw", shell=True)
scp_prod = "scp -r server:/NFS/{0} .".format(filePath)
subprocess.call(scp_prod, shell=True)
finally:
gateway.kill()

Categories

Resources