Permissions to allow PHP run a BAT whom runs WinSCP
Posted by: cesarf (---.prod-infinitum.com.mx)
Date: May 21, 2015 11:00PM

hi all,
i've a PHP permissions issue, running WampServer64 v2.2 on Windows Server 2008 R2
i need to run from PHP a windows batch file which in turn runs WinSCP to upload a file

if i try c:\test03.bat on command line, it ran ok
if i try c:\wamp\bin\php\php5.4.3\php c:\www\test03.php in command line, it ran ok
but if try http:\\localserver\test03.php, php WinSCP ran but file is not uploaded

we suspect, problem is related to apache or php or wamp permissions to interact with windows folders.

can you advice us?


br,
cesar.

Options: ReplyQuote
Re: Permissions to allow PHP run a BAT whom runs WinSCP
Posted by: cesarf (---.prod-infinitum.com.mx)
Date: May 22, 2015 01:35AM

hi again,

I already tried this ones, but no luck:
1) services.msc | wampapache | log on | check on "Allow this service to interact with desktop"
2) found a post here about manually starting apache as administrator, but can't manage to do it
3) adding read permissions to "everyone" in windows to my folders


br,
cesar.



Edited 1 time(s). Last edit at 05/22/2015 01:56AM by cesarf.

Options: ReplyQuote
Re: Permissions to allow PHP run a BAT whom runs WinSCP
Posted by: Otomatic (Moderator)
Date: May 22, 2015 10:29AM

Hi,

What you want to do is impossible.
From PHP, you can run some Windows commands, even some executable in the background with its outputs redirected to a file, but you cannot run a GUI program.
See PHP documentation about commands:
- exec
- passthru
- system
- backticks (``) operator
and you will see that what you want to do is impossible with PHP.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: Permissions to allow PHP run a BAT whom runs WinSCP
Posted by: cesarf (---.prod-infinitum.com.mx)
Date: May 22, 2015 07:44PM

hi Otomatic,

actually, i'm trying to do exactly the opposite.
i don't wanna run any guy but publish a pdf file in a server using a php file to run winscp in batch mode.

if i open a cmd window and type "php myscript.php", everything runs as it should be, without any other output but the one in the php file, so i think i'm not asking for some kind of technical imposibility but a sort of permissions translated from windows to apache in order to run my batch.

because when i ran it from cmd, permissions came from my windows user, allowing full php "execution" of the php script

maybe i'm not explaining myself clear, but i need to do the same in a browser executed php script.

anyway, i'll take a look on the commands you mentioned.


br,
cesar.

Options: ReplyQuote
Re: Permissions to allow PHP run a BAT whom runs WinSCP
Posted by: Otomatic (Moderator)
Date: May 23, 2015 09:29AM

Hi,

>... using a php file to run winscp in batch mode.
Does "winscp" able to run on command line with parameters?
If yes, you can use passthru, like this:
$command = 'start /b /wait c:/prog/winscp/winscp.exe parameter1 parameter2 parameter3';
ob_start();
passthru($command);
$output = ob_get_contents();
ob_end_clean();
And then you treat the contents of the variable $output.

---------------------------------------------------------------
Documentation Apache - Documentation PHP - Documentation MySQL - Wampserver install files & addons

Options: ReplyQuote
Re: Permissions to allow PHP run a BAT whom runs WinSCP
Posted by: cesarf (---.prod-infinitum.com.mx)
Date: May 27, 2015 05:32AM

hi again Otomatic,

yeap, winscp has a strong batch mode, i recomend it.
i tested your code and result is the same.
i strongly suspect problem has to be with apache permissions... but i'm a win man, sorry but a complete rookie regarding apache and wampserver, that's why i need help.

this is the previous php's exec():
exec( 'c:\www\pdt\bat\publicar_WebVmg.bat c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf' );

this is the tested $command:
$command = 'start /b /wait c:\www\pdt\bat\publicar_WebVmg.bat c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf';

this is my winscp log file when ran with passthru() the same as exec():
21:26:07.63 
. 2015-05-26 21:26:08.244 --------------------------------------------------------------------------
. 2015-05-26 21:26:08.244 WinSCP Version 5.7.2 (Build 5316) (OS 6.1.7601 Service Pack 1 - Windows Server 2008 R2 Enterprise)
. 2015-05-26 21:26:08.244 Configuration: HKCU\Software\Martin Prikryl\WinSCP 2\
. 2015-05-26 21:26:08.244 Log level: Normal
. 2015-05-26 21:26:08.244 Local account: GFS\GFS-PCD$
. 2015-05-26 21:26:08.244 Working directory: C:\www
. 2015-05-26 21:26:08.244 Process ID: 3272
. 2015-05-26 21:26:08.244 Command-line: c:\WinSCP\WinSCP.exe  /script=C:\www\pdt\bat\publicar_WebVmg.wscp /log=C:\www\pdt\bat\publicar_WebVmg_script.log /parameter // c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf
. 2015-05-26 21:26:08.244 Time zone: Current: GMT-5, Standard: GMT-6 (Hora estándar central (México)), DST: GMT-5 (Hora de verano central (México)), DST Start: 05/04/2015, DST End: 25/10/2015
. 2015-05-26 21:26:08.244 Login time: martes, 26 de mayo de 2015 09:26:08 p.m.
. 2015-05-26 21:26:08.244 --------------------------------------------------------------------------
. 2015-05-26 21:26:08.244 Script: Retrospectively logging previous script records:
> 2015-05-26 21:26:08.244 Script: option batch abort
< 2015-05-26 21:26:08.244 Script: batch           abort     
< 2015-05-26 21:26:08.244 Script: reconnecttime   120       
> 2015-05-26 21:26:08.244 Script: option echo on
< 2015-05-26 21:26:08.244 Script: echo            on        
> 2015-05-26 21:26:08.244 Script: option confirm off
< 2015-05-26 21:26:08.244 Script: option confirm off
< 2015-05-26 21:26:08.244 Script: confirm         off       
> 2015-05-26 21:26:08.244 Script: open publicar_WebVmg
< 2015-05-26 21:26:08.244 Script: open publicar_WebVmg
. 2015-05-26 21:26:08.244 --------------------------------------------------------------------------
. 2015-05-26 21:26:08.244 Session name: publicar_WebVmg (Ad-Hoc site)
. 2015-05-26 21:26:08.244 Host name: publicar_WebVmg (Port: 22)
. 2015-05-26 21:26:08.244 User name:  (Password: No, Key file: No)
. 2015-05-26 21:26:08.244 Tunnel: No
. 2015-05-26 21:26:08.244 Transfer Protocol: SFTP (SCP)
. 2015-05-26 21:26:08.244 Ping type: -, Ping interval: 30 sec; Timeout: 15 sec
. 2015-05-26 21:26:08.244 Disable Nagle: No
. 2015-05-26 21:26:08.244 Proxy: none
. 2015-05-26 21:26:08.244 Send buffer: 262144
. 2015-05-26 21:26:08.244 SSH protocol version: 2; Compression: No
. 2015-05-26 21:26:08.244 Bypass authentication: No
. 2015-05-26 21:26:08.244 Try agent: Yes; Agent forwarding: No; TIS/CryptoCard: No; KI: Yes; GSSAPI: No
. 2015-05-26 21:26:08.244 Ciphers: aes,blowfish,3des,WARN,arcfour,des; Ssh2DES: No
. 2015-05-26 21:26:08.244 KEX: dh-gex-sha1,dh-group14-sha1,dh-group1-sha1,rsa,WARN
. 2015-05-26 21:26:08.244 SSH Bugs: A,A,A,A,A,A,A,A,A,A,A
. 2015-05-26 21:26:08.244 Simple channel: Yes
. 2015-05-26 21:26:08.244 Return code variable: Autodetect; Lookup user groups: A
. 2015-05-26 21:26:08.244 Shell: default
. 2015-05-26 21:26:08.244 EOL: 0, UTF: 2
. 2015-05-26 21:26:08.244 Clear aliases: Yes, Unset nat.vars: Yes, Resolve symlinks: Yes
. 2015-05-26 21:26:08.244 LS: ls -la, Ign LS warn: Yes, Scp1 Comp: No
. 2015-05-26 21:26:08.244 SFTP Bugs: A,A
. 2015-05-26 21:26:08.244 SFTP Server: default
. 2015-05-26 21:26:08.244 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2015-05-26 21:26:08.244 Cache directory changes: Yes, Permanent: Yes
. 2015-05-26 21:26:08.244 DST mode: 1
. 2015-05-26 21:26:08.244 --------------------------------------------------------------------------
. 2015-05-26 21:26:08.244 Looking up host "publicar_WebVmg"

this is what i run from windows:
c:
cd \wamp\bin\php\php5.4.3
php.exe C:\www\test03.php

this is my batch file, i didn't comented before because it ran at least from win as expected:
echo %time% > C:\www\pdt\bat\publicar_WebVmg_script.log
c:\WinSCP\WinSCP.exe /script=C:\www\pdt\bat\publicar_WebVmg.wscp /log=C:\www\pdt\bat\publicar_WebVmg_script.log /parameter // %1

and this is my winscp command file (also hadn't included because it ran from win, and stored login sesion worked both from gui & php called from win as expected):
option batch abort
option echo on
option confirm off
open publicar_WebVmg
put -preservetime %1%
close
exit


i noticed my www folder isn't under wamp as is mentioned in several examples.
maybe we did something wrong when installed, i don't know.
do you think something like "virtual hosts" could do the job, regarding permissions?




and this is the log i got then, when file is uploaded as expected:
21:50:51.80 
. 2015-05-26 21:50:51.900 --------------------------------------------------------------------------
. 2015-05-26 21:50:51.900 WinSCP Version 5.7.2 (Build 5316) (OS 6.1.7601 Service Pack 1 - Windows Server 2008 R2 Enterprise)
. 2015-05-26 21:50:51.900 Configuration: HKCU\Software\Martin Prikryl\WinSCP 2\
. 2015-05-26 21:50:51.900 Log level: Normal
. 2015-05-26 21:50:51.900 Local account: GFS-PCD\cesarf
. 2015-05-26 21:50:51.900 Working directory: C:\wamp\bin\php\php5.4.3
. 2015-05-26 21:50:51.900 Process ID: 3308
. 2015-05-26 21:50:51.900 Command-line: c:\WinSCP\WinSCP.exe  /script=C:\www\pdt\bat\publicar_WebVmg.wscp /log=C:\www\pdt\bat\publicar_WebVmg_script.log /parameter // c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf
. 2015-05-26 21:50:51.900 Time zone: Current: GMT-5, Standard: GMT-6 (Hora estándar central (México)), DST: GMT-5 (Hora de verano central (México)), DST Start: 05/04/2015, DST End: 25/10/2015
. 2015-05-26 21:50:51.900 Login time: martes, 26 de mayo de 2015 09:50:51 p.m.
. 2015-05-26 21:50:51.900 --------------------------------------------------------------------------
. 2015-05-26 21:50:51.900 Script: Retrospectively logging previous script records:
> 2015-05-26 21:50:51.900 Script: option batch abort
< 2015-05-26 21:50:51.900 Script: batch           abort     
< 2015-05-26 21:50:51.900 Script: reconnecttime   120       
> 2015-05-26 21:50:51.900 Script: option echo on
< 2015-05-26 21:50:51.900 Script: echo            on        
> 2015-05-26 21:50:51.900 Script: option confirm off
< 2015-05-26 21:50:51.900 Script: option confirm off
< 2015-05-26 21:50:51.900 Script: confirm         off       
> 2015-05-26 21:50:51.900 Script: open publicar_WebVmg
< 2015-05-26 21:50:51.900 Script: open publicar_WebVmg
. 2015-05-26 21:50:51.900 --------------------------------------------------------------------------
. 2015-05-26 21:50:51.900 Session name: publicar_WebVmg (Site)
. 2015-05-26 21:50:51.900 Host name: ftp.gf-sistemas.com.mx (Port: 21)
. 2015-05-26 21:50:51.900 User name: publicar@gf-sistemas.com.mx (Password: Yes, Key file: No)
. 2015-05-26 21:50:51.900 Transfer Protocol: FTP
. 2015-05-26 21:50:51.900 Ping type: C, Ping interval: 30 sec; Timeout: 15 sec
. 2015-05-26 21:50:51.900 Disable Nagle: No
. 2015-05-26 21:50:51.900 Proxy: none
. 2015-05-26 21:50:51.900 Send buffer: 262144
. 2015-05-26 21:50:51.900 UTF: 2
. 2015-05-26 21:50:51.900 FTP: FTPS: Explicit TLS; Passive: Yes [Force IP: A]; MLSD: A [List all: A]
. 2015-05-26 21:50:51.900 Session reuse: Yes
. 2015-05-26 21:50:51.900 TLS/SSL versions: TLSv1.0-TLSv1.2
. 2015-05-26 21:50:51.900 Local directory: C:\Users\cesarf\Documents, Remote directory: /vmg, Update: Yes, Cache: Yes
. 2015-05-26 21:50:51.900 Cache directory changes: Yes, Permanent: Yes
. 2015-05-26 21:50:51.900 Timezone offset: 0h 0m
. 2015-05-26 21:50:51.900 --------------------------------------------------------------------------
. 2015-05-26 21:50:51.900 Connecting to ftp.gf-sistemas.com.mx ...
. 2015-05-26 21:50:51.963 Connected with ftp.gf-sistemas.com.mx, negotiating TLS connection...
< 2015-05-26 21:50:52.025 220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
< 2015-05-26 21:50:52.025 220-You are user number 2 of 50 allowed.
< 2015-05-26 21:50:52.025 220-Local time is now 05:50. Server port: 21.
< 2015-05-26 21:50:52.025 220-This is a private system - No anonymous login
< 2015-05-26 21:50:52.025 220-IPv6 connections are also welcome on this server.
< 2015-05-26 21:50:52.025 220 You will be disconnected after 15 minutes of inactivity.
> 2015-05-26 21:50:52.025 AUTH TLS
< 2015-05-26 21:50:52.103 234 AUTH TLS OK.
. 2015-05-26 21:50:52.337 Verifying certificate for "" with fingerprint dc:42:80:ab:b4:0f:8f:00:28:e7:47:de:31:8e:ba:af:f0:75:ce:d5 and 20 failures
. 2015-05-26 21:50:52.353 Certificate verified against Windows certificate store
. 2015-05-26 21:50:52.353 Certificate for "" matches cached fingerprint and failures
. 2015-05-26 21:50:52.353 Using TLSv1.2, cipher TLSv1/SSLv3: DHE-RSA-AES256-GCM-SHA384, 2048 bit RSA
. 2015-05-26 21:50:52.353 TLS connection established. Waiting for welcome message...
> 2015-05-26 21:50:52.353 USER [email]publicar@gf-sistemas.com.mx[/email]
< 2015-05-26 21:50:52.415 331 User [email]publicar@gf-sistemas.com.mx[/email] OK. Password required
> 2015-05-26 21:50:52.415 PASS ******************
< 2015-05-26 21:50:52.540 230 OK. Current restricted directory is /
> 2015-05-26 21:50:52.540 SYST
< 2015-05-26 21:50:52.602 215 UNIX Type: L8
> 2015-05-26 21:50:52.602 FEAT
< 2015-05-26 21:50:52.665 211-Extensions supported:
< 2015-05-26 21:50:52.665  EPRT
< 2015-05-26 21:50:52.665  IDLE
< 2015-05-26 21:50:52.665  MDTM
< 2015-05-26 21:50:52.665  SIZE
< 2015-05-26 21:50:52.665  MFMT
< 2015-05-26 21:50:52.665  REST STREAM
< 2015-05-26 21:50:52.665  MLST type*;size*;sizd*;modify*;UNIX.mode*;UNIX.uid*;UNIX.gid*;unique*;
< 2015-05-26 21:50:52.665  MLSD
< 2015-05-26 21:50:52.665  AUTH TLS
< 2015-05-26 21:50:52.665  PBSZ
< 2015-05-26 21:50:52.665  PROT
< 2015-05-26 21:50:52.665  TVFS
< 2015-05-26 21:50:52.665  ESTA
< 2015-05-26 21:50:52.665  PASV
< 2015-05-26 21:50:52.665  EPSV
< 2015-05-26 21:50:52.665  SPSV
< 2015-05-26 21:50:52.665  ESTP
< 2015-05-26 21:50:52.665 211 End.
> 2015-05-26 21:50:52.665 PBSZ 0
< 2015-05-26 21:50:52.727 200 PBSZ=0
> 2015-05-26 21:50:52.727 PROT P
< 2015-05-26 21:50:52.789 200 Data protection level set to "private"
. 2015-05-26 21:50:52.789 Connected
. 2015-05-26 21:50:52.789
--------------------------------------------------------------------------
. 2015-05-26 21:50:52.789 Using FTP protocol.
. 2015-05-26 21:50:52.789 Doing startup conversation with host.
> 2015-05-26 21:50:52.789 PWD
< 2015-05-26 21:50:52.836 257 "/" is your current location
. 2015-05-26 21:50:52.836 Changing directory to "/vmg".
> 2015-05-26 21:50:52.836 CWD /vmg
< 2015-05-26 21:50:52.899 250 OK. Current directory is /vmg
. 2015-05-26 21:50:52.899 Getting current directory name.
> 2015-05-26 21:50:52.899 PWD
< 2015-05-26 21:50:52.961 257 "/vmg" is your current location
. 2015-05-26 21:50:52.961 Startup conversation with host finished.
< 2015-05-26 21:50:52.961 Script: Active session: [1] publicar_WebVmg
> 2015-05-26 21:50:52.961 Script: put -preservetime c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf
< 2015-05-26 21:50:52.961 Script: put -preservetime c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf
. 2015-05-26 21:50:52.961 Copying 1 files/directories to remote directory "/vmg"
. 2015-05-26 21:50:52.961   PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: 
. 2015-05-26 21:50:52.961   TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; InclM: ; ResumeL: 0
. 2015-05-26 21:50:52.961   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2015-05-26 21:50:52.961 File: 'c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf' [2010-07-24T19:15:55.109Z] [27056]
. 2015-05-26 21:50:52.961 Copying "c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf" to remote directory started.
. 2015-05-26 21:50:52.961 Binary transfer mode selected.
. 2015-05-26 21:50:52.961 Starting upload of c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf
> 2015-05-26 21:50:52.961 TYPE A
< 2015-05-26 21:50:53.023 200 TYPE is now ASCII
> 2015-05-26 21:50:53.023 PASV
< 2015-05-26 21:50:53.086 227 Entering Passive Mode (75,102,24,200,184,237)
> 2015-05-26 21:50:53.086 MLSD
< 2015-05-26 21:50:53.211 150 Accepted data connection
. 2015-05-26 21:50:53.211 Session ID reused
. 2015-05-26 21:50:53.211 TLS connection established
< 2015-05-26 21:50:53.273 226-Options: -a -l 
< 2015-05-26 21:50:53.273 226 113 matches total
. 2015-05-26 21:50:53.273 type=cdir;sizd=12288;modify=20150527025045;UNIX.mode=0755;UNIX.uid=620;UNIX.gid=632;unique=805g1840692; .

<... lot of files in ftp server ...>

. 2015-05-26 21:50:53.273 type=file;size=990;modify=20140513042213;UNIX.mode=0644;UNIX.uid=620;UNIX.gid=632;unique=805g1840da1; index.htm
> 2015-05-26 21:50:53.273 TYPE I
< 2015-05-26 21:50:53.335 200 TYPE is now 8-bit binary
> 2015-05-26 21:50:53.335 PASV
< 2015-05-26 21:50:53.398 227 Entering Passive Mode (75,102,24,200,177,11)
> 2015-05-26 21:50:53.398 STOR __GFs_SYS_GFP_Prueba_VMG.pdf
< 2015-05-26 21:50:53.523 150 Accepted data connection
. 2015-05-26 21:50:53.585 Session ID reused
. 2015-05-26 21:50:53.585 TLS connection established
< 2015-05-26 21:50:53.819 226-File successfully transferred
< 2015-05-26 21:50:53.819 226 0.182 seconds (measured here), 145.18 Kbytes per second
> 2015-05-26 21:50:53.819 MFMT 20100724191555 __GFs_SYS_GFP_Prueba_VMG.pdf
< 2015-05-26 21:50:53.881 213 UTIME OK
. 2015-05-26 21:50:53.881 Upload successful
. 2015-05-26 21:50:53.881 Transfer done: 'c:\www\pdt\bat\pdf\__GFs_SYS_GFP_Prueba_VMG.pdf' [27056]
> 2015-05-26 21:50:53.881 Script: close
< 2015-05-26 21:50:53.881 Script: close
. 2015-05-26 21:50:53.881 Disconnected from server



br,
cesar.



Edited 1 time(s). Last edit at 05/27/2015 05:34AM by cesarf.

Options: ReplyQuote


Sorry, only registered users may post in this forum.