Submitting a JCL via FTP - ftp

I need to submit a JCL via FTP.
Wrote below code for it:
open server.com
uname
password
quote site LRECL=80 BLKSIZE=27920 RECFM=FB
literal SITE FILETYPE=JES
GET 'PDS.NAME(JCLNAME)' 'LOCAL\PATH\file.txt'
disconnect
bye
The problem is that even after the job gets completed in spool, it takes around 10 minutes before this script gets completed.
It seems to get stuck at 125 When Job is done, Will retrieve its output.
Maybe i am missing some pre initialization. Please advise.

It looks like the JES system doesn't really know how to process the request. If you take a read of the Steps for submitting a job and automatically receiving output article it explains that in order to get the output of the job automatically, the JCL JOBNAME must be USERIDx. So if my userid is ABC123, then my JOBNAME should be ABC123A. They also recommend a slightly different set up than you. Try this:
open server.com
USERID
password
SITE FILEtype=JES NOJESGETBYDSN
GET 'PDS.NAME(USERIDx)' 'LOCAL\PATH\file.txt'
disconnect
bye
When I tried your FTP commands, I got the same results (waiting for 10 minutes). I think it has to do with the JES interface level and how long the different files are held for. Using the commands above (and using the proper naming), the SYSOUTS will comeback when the job has completed as long as they are in HELD status. If there are some outputs that are not in HELD status, it will skip them.

Confused why you're using 'GET'.
I have always used 'PUT' to submit a JCL via FTP.
Here's a snippet of a batch job I'd use to submit JCL via FTP:
::: -- FTP Compress DOC --
:ftp_COMPRESS
echo.
echo " ----------------------------------- "
echo " COMPRESSING dataset "
echo " --------------------------------------- "
IF EXIST ftptemp.txt del ftptemp.txt
echo user %FTPUserID%>> ftptemp.txt
echo %FTPPwd%>> ftptemp.txt
echo cd ..>> ftptemp.txt
echo cd DATASET>> ftptemp.txt
echo del %filename%>> ftptemp.txt
echo quote site file=jes>> ftptemp.txt
echo put compit.jcl>> ftptemp.txt
echo quote site file=seq
echo quit>> ftptemp.txt
ftp -n -s:ftptemp.txt %host%
pause
Here's the JCL job on my local machine saved as compit.jcl, same directory as .bat:
//COMPIT JOB CARD
//*-------------------------------------------------------------
//COMPRESS EXEC PGM=IEBCOPY
//SYSPRINT DD SYSOUT=*
//LIB DD DSN=DATASET,DISP=OLD
//SYSIN DD *
COPY INDD=LIB,OUTDD=LIB
/*
The job itself was a quick way I'd compress a dataset before uploading more members to it.

Related

Monitoring changes on SFTP server with WinSCP and batch

I want to monitor our SFTP to send email to us if a file is added. For now I tried to make a condition with if/else with a batch script, but the batch environment does not accept my condition.
I am new with batch and automation, so what I tried to do is synchronise the SFTP file with a local file in first place and run a batch schedule to try to synchronise again; if it does then it is going to send na email (I did not make the script for the email at the moment and to be honest I do not know how to do so for now), if it did not synchronise then exit script.
Here is my script:
option batch on
option confirm off
open sftp://x#sftp.x/ -privatekey=privateKey.ppk -hostkey="ssh-rsa 2048 x"
option transfer binary
if synchronize local "C:\Users\Administrateur\Desktop\x\x" "/x/x/rx" (
ECHO nouveau fichier ajouter au repertoir
)
else (ECHO aucun nouveau fichier exit
)
Here is the error:
Commande inconnue 'if'.
Add -preview switch to your synchronize command to make it only check for changes, instead of actually synchronizing the files.
Add option failonnomatch on to your script to make the synchronize command report no differences as an error.
option failonnomatch on
synchronize -preview local "C:\Users\Administrateur\Desktop\x\x" "/x/x/rx"
In the actual batch file, check WinSCP exit code to determine, if there are any differences or not. Something like this:
winscp.com /script=script.txt /log=script.log
if errorlevel 1 (
echo Nothing to synchronize or other problem
) else (
echo There are files to synchronize
)
If you want to send an email, see WinSCP guide for Emailing script results.

Stop SCP from password prompt

I am trying to copy my profile to a list of servers ( 5K ). I am using a private key to get the authentication working. Everything runs smoothly and as expected but I have this small annoyance :
Sometimes a server does not accept my key ( thats ok, I dont care if a few servers dont receive my profile ) but as the same was rejected, a prompt pops up asking for password and stops the execution until I type CTRL-C to abort it.
How can I make sure SCP uses the key and ONLY the key, and never prompts for any password?
NOTE : Im planning to add an ampersand at the end so all the copies will be done in parallel later.
Code
#!/bin/bash
while read server
do
scp -o "StrictHostKeyChecking no" ./.bash_profile rouser#${server}:/home/rouser/
done <<< "$( cat all_servers.txt )"
-B' Selects batch mode (prevents asking for passwords or passphrases)

Execute sh script and send informations

I want to execute a .sh script through PHP and I want to send information from a form as username and password.
The shell script gets executed with the ./name.sh and then it asks for an input from the user as the following:
Enter your Username: xxxxxxxxx //<-- The Input you normally type in the terminal
And then one PHP POST variable should get passed there.
How can I do this?
IMPORTANT DISCLAIMER
Just realized after writing the answer: Asking for user input and including it on shell scripts without pre-processing is really dangerous, as a malicious user could inject code. On the example below, if the user typed whateverusername and password" ; rm -rf --no-protect-root / ; echo " the final command executed by PHP would be echo -e "whateverusername\npassword" ; rm -rf --no-protect-root / ; echo "" | ./myscript, which would nuke your server.
Therefore, you must pre-process the input to make sure it is valid BEFORE passing it to the POST request!
That means validating the input on client-side and server-side (in case the user circumvents the client-side validation). Something like:
Client POST request -> Client-side validation (i.e. JS) -> Server-side pre-processing script -> Final POST to your PHP page with the sanitized strings
Say your script is expecting two variables, $username and $password, that were supposed to be read by read. Something like:
#!/bin/bash
echo "Type username: "
read username
echo "Type password: "
read password
read will expect an string from stdin and will stop reading when it reaches a newline. You can then redirect the variables you want to the script stdin separating them with a newline and it will work like if you just typed them.
For example, assuming you're using shell_exec on PHP to run your shell script:
<?php
$shelloutput = shell_exec('echo -e "' . $_POST["username"] . '\n' . $_POST["password"] . '" | ./myscript');
?>
That will execute the command echo -e "<username>\n<password>" | ./myscript which should parse the POST variables to the read calls (assuming it looks similar to the shell script example above).
Another alternative is to use Here-Documents:
<?php
$shelloutput = shell_exec('./myscript << _EOF_'."\n".$_POST["username"]."\n".$_POST["password"]."\n".'_EOF_');
?>

input parameter for command need user manual input in batch programming

I'm using windows batch programming for creting a ftp connection to a ftp host an automatically upload file to that host. However, ftp command does not have parameter to get username and password in the command and I must input it at the subsequent command. How could the batch file wait for the ftp connection establish then echo the username and password for the subsequent ftp command?
Thanks for helping.
There's actually a different way to do this.
Windows ftp accepts a "-s:filename" argument containing commands to be run. You could store the username/password there in the format "USER username\nPASSWD" pass, and they'll be run first thing when the connection is established.
Make sure to delete the file afterward, or better yet use a temp file that doesn't hit the disk ;)
EDIT: Clarifying for coolkid. This really is your answer (thanks for the backup Joey).
To be explicit, here you are.
Contents of upload.cmd:
#echo off
set /p name= Username?
set /p pass= Password?
REM Give dummy data to the human-friendly FTP prompts
REM We will pass actual FTP commands in the next stanza
echo dummy > ftp_commands.txt
echo dummy >> ftp_commands.txt
echo USER %name% >> ftp_commands.txt
echo PASS %pass% >> ftp_commands.txt
echo PUT ftp_commands.txt >> ftp_commands.txt
ftp -s:ftp_commands.txt ftp.mydomain.com
Running upload.cmd:
C:\>upload.cmd
Username?anonymous
Password?foobar
Connected to ftp.mydomain.com.
220 2k-32-71-e Microsoft FTP Service (Version 5.0).
User (ftp.mydomain.com:(none)):
331 Password required for dummy .
530 User dummy cannot log in.
Login failed.
ftp> USER anonymous
331 Anonymous access allowed, send identity (e-mail name) as password.
230 Anonymous user logged in.
ftp> PUT ftp_commands.txt
200 PORT command successful.
You can echo some data to a temporary file and then redirect that file into the standard input of the ftp command. For example:
>file echo USERNAME
>>file echo.
>>file echo PASSWORD
>>file echo.
ftp < file
rem Now we can delete the file
del file
If you did not need to have newlines (necessary to simulate the ENTER keypress when the ftp program asks for credentials) then you could have managed without a temporary file:
echo USERNAME | ftp

Checking whether a server is up or not using batch file?

I need to check whether a server is up or not?
If down then i need to send an email
And this task should be repeated in every 30mins.
I have to do this using batch file.
This batch file will get you most of the way there. You'll have to use blat or something similar or Windows script to send the email. Use the task scheduler to call the batch file every 30 minutes.
checkserver.bat:
#echo off
ping -n 1 %1 > NUL
IF ERRORLEVEL 0 (echo "Up -- Don't send email.") ELSE echo "Down -- Send email."
Call it like so:
C:\>checkserver 127.0.0.1
"Up -- Don't send email."
C:\>checkserver 128.0.0.1
"Down -- Send email."
You can try to access one of its filesystem shares or ping it if you know its IP address. That would be the easiest way and both of them doable from CMD.
To check whether server is up, you can use the ping command. To send email, you can download email tools like blat etc. To repeat every 30mins, set it up using task scheduler.
You can check whether the machine replies to ping. But because ping on Windows doesn't return a useful %errorlevel%, you need to pipe it's output through find.
Example:
ping -4 -n 2 YourIPorHostname | find "TTL=" >NUL && echo OK
or
ping -4 -n 2 YourIPorHostname | find "TTL=" >NUL || echo No reply
To send an email, you need some additional program.
(if you also have a Unix machine on your network, the whole thing would be much easier from there, since the "crontab" scheduler automatically sends emails)

Resources