oreomike
asked on
Submitting a file to a form on unix via cron
All,
I have a windows web server and a unix db server, separated by a firewall. I am looking to use a web form to copy 2 files hourly from the unix server to the windows server via an automated cron job.
The unix server has netscape and lynx, but lynx does not appear to work. Perl is available, as is java. I HAVE NO ROOT PRIVILEGES ON THIS BOX AND CANNOT GET ANY NEW SOFTWARE ADDED TO IT. (I really need wget, but I doubt it is available for AIX on RISC anyway)
What I need is a perl script, or another way to automate a form submission that includes files. I cannot use FTP because 1) it is blocked at the firewall and 2) I need to process the files on the web server to append them to an Access db.
So,
Does anyone know how to simulate submitting files via a form with a single URL call?
my web page only outputs: <SCRIPT>window.close()</sc ript> to the client.
My form is simple:
<form name=dbupload enctype="multipart/form-da ta" action="index.php" METHOD="POST">
Dump: <INPUT TYPE=file NAME=dbdump><br>
Tracks: <INPUT TYPE=file NAME=dbtracks><br>
<INPUT TYPE=submit value="Upload Files"><br>
</form>
HELP!!!!!!!!!!!!!!!!!!
I have a windows web server and a unix db server, separated by a firewall. I am looking to use a web form to copy 2 files hourly from the unix server to the windows server via an automated cron job.
The unix server has netscape and lynx, but lynx does not appear to work. Perl is available, as is java. I HAVE NO ROOT PRIVILEGES ON THIS BOX AND CANNOT GET ANY NEW SOFTWARE ADDED TO IT. (I really need wget, but I doubt it is available for AIX on RISC anyway)
What I need is a perl script, or another way to automate a form submission that includes files. I cannot use FTP because 1) it is blocked at the firewall and 2) I need to process the files on the web server to append them to an Access db.
So,
Does anyone know how to simulate submitting files via a form with a single URL call?
my web page only outputs: <SCRIPT>window.close()</sc
My form is simple:
<form name=dbupload enctype="multipart/form-da
Dump: <INPUT TYPE=file NAME=dbdump><br>
Tracks: <INPUT TYPE=file NAME=dbtracks><br>
<INPUT TYPE=submit value="Upload Files"><br>
</form>
HELP!!!!!!!!!!!!!!!!!!
Can you use SSH? If so, you should be able to schedule an scp or rsync command to do the copy, both programs are available in most distributions. rsync would have the added advantage that it would only copy if files had been changed.
To persevere on the HTTP route from PHP, you can use CURL to submit form data via POST. First of all, get your file contents into a variable, then submit it as post data:
<?php
$data = urlencode(file_get_content s('myuploa dfile1.txt '));
$URL="http://www.example.com/upload.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data");
curl_exec ($ch);
curl_close ($ch);
?>
You'll need to tweak this to match the upload script on your windows box.
To persevere on the HTTP route from PHP, you can use CURL to submit form data via POST. First of all, get your file contents into a variable, then submit it as post data:
<?php
$data = urlencode(file_get_content
$URL="http://www.example.com/upload.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$data");
curl_exec ($ch);
curl_close ($ch);
?>
You'll need to tweak this to match the upload script on your windows box.
Revised version to match your form example:
<?php
$dbdump = urlencode(file_get_content s('dbdump' ));
$dbtracks = urlencode(file_get_content s('dbtrack s'));
$URL="http://www.example.com/index.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "dbdump=$dbdump&dbtracks=$ dbtracks") ;
curl_exec ($ch);
curl_close ($ch);
?>
<?php
$dbdump = urlencode(file_get_content
$dbtracks = urlencode(file_get_content
$URL="http://www.example.com/index.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "dbdump=$dbdump&dbtracks=$
curl_exec ($ch);
curl_close ($ch);
?>
ASKER
so I do not have PHP available either on the unix box. Do you know if perl might have a similar mod as curl?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
probably, but I didn't see a perl forum. Thanks anyway. I'll probably try a java cli instead.
ASKER
The unix box does not have the LWP:UserAgent package installed.
You'd be much better off going with a shell script than Java. Java is monstrously inefficient for small operations like this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
More on installing perl modules as non-root:
http://www.bugzilla.org/docs/tip/html/nonroot.html#AEN931
http://www.bugzilla.org/docs/tip/html/nonroot.html#AEN931
ASKER
All of your suggestions were good, but given my limited ability to do anything on the box, including a limited FS size for the home directories, I was forced to go java.
I have included the code that I modified from http://java.sun.com to only upload a single file to a form with a single element. Currently it only will work well with text files and may need a change to \n's to make them \r\n's. Haven't had time to fully check it out. Also, I am sure there is a better way to send contents of a file to an InputStream, but this gets my file there and I am happy.
Thanks for all of your quick and thorough help.
************************** ********** ********** ********** *
import java.io.*;
import java.net.*;
public class uploadFile {
public static void writeFile(DataOutputStream dstream,String filename,String formname) throws Exception {
File f0 = new File(filename);
try {
FileInputStream fi = new FileInputStream(f0);
dstream.writeBytes("Conten t-Disposit ion: form-data; name=\""+formname+"\"; filename=\""+f0.getName()+ "\"\r\n");
dstream.writeBytes("Conten t-Type: text/plain\r\n\r\n");
int cnt = fi.read();
while(cnt!=-1){
dstream.write(cnt);
cnt = fi.read();
}
fi.close();
dstream.writeBytes("\r\n-- ****4353\r \n");
} catch (FileNotFoundException e) {
System.out.println("File not found: " + f0.getName());
}
}
public static void main(String[] args) throws Exception {
if (args.length != 5) {
System.err.println(
"Usage:\njava uploadFile http://this/url.php formInputBoxName /path/and/file.name");
System.exit(1);
}
String strURL = args[0]
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("C onnection" ,"Keep-Ali ve");
conn.setRequestProperty("H TTP_REFERE R",strURL) ;
conn.setRequestProperty("C ontent-Typ e","multip art/form-d ata; boundary=****4353");
DataOutputStream dstream = new DataOutputStream(conn.getO utputStrea m());
dstream.writeBytes("--**** 4353\r\n") ;
writeFile(dstream,args[1], args[2]);
dstream.writeBytes("\r\n-- ****4353-- \r\n");
dstream.flush();
dstream.close();
try{
DataInputStream in = new DataInputStream( new BufferedInputStream(conn.g etInputStr eam()));
String sIn = in.readLine();
boolean b = true;
System.out.println(sIn);
while(sIn.compareTo("windo w.close(); ")!=0){
sIn = in.readLine();
if(sIn!=null){
System.out.println(sIn);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I have included the code that I modified from http://java.sun.com to only upload a single file to a form with a single element. Currently it only will work well with text files and may need a change to \n's to make them \r\n's. Haven't had time to fully check it out. Also, I am sure there is a better way to send contents of a file to an InputStream, but this gets my file there and I am happy.
Thanks for all of your quick and thorough help.
**************************
import java.io.*;
import java.net.*;
public class uploadFile {
public static void writeFile(DataOutputStream
File f0 = new File(filename);
try {
FileInputStream fi = new FileInputStream(f0);
dstream.writeBytes("Conten
dstream.writeBytes("Conten
int cnt = fi.read();
while(cnt!=-1){
dstream.write(cnt);
cnt = fi.read();
}
fi.close();
dstream.writeBytes("\r\n--
} catch (FileNotFoundException e) {
System.out.println("File not found: " + f0.getName());
}
}
public static void main(String[] args) throws Exception {
if (args.length != 5) {
System.err.println(
"Usage:\njava uploadFile http://this/url.php formInputBoxName /path/and/file.name");
System.exit(1);
}
String strURL = args[0]
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("C
conn.setRequestProperty("H
conn.setRequestProperty("C
DataOutputStream dstream = new DataOutputStream(conn.getO
dstream.writeBytes("--****
writeFile(dstream,args[1],
dstream.writeBytes("\r\n--
dstream.flush();
dstream.close();
try{
DataInputStream in = new DataInputStream( new BufferedInputStream(conn.g
String sIn = in.readLine();
boolean b = true;
System.out.println(sIn);
while(sIn.compareTo("windo
sIn = in.readLine();
if(sIn!=null){
System.out.println(sIn);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
ASKER