Unix shell script with Iseries command - shell

I am trying to ftp a file from unix to as400 and executing iseries command in the script.
ftp is working fine,I am getting an error in jobd command as
HOST=KCBNSXDD.svr.us.bank.net
USER=test
PASS=1234 #This is the password for the FTP user.
ftp -env $HOST << EOF
# Call 2. Here the login credentials are supplied by calling the variables.
user $USER $PASS
# Call 3. Here you will change to the directory where you want to put or get
cd "\$QARCVBEN"
# Call4. Here you will tell FTP to put or get the file.
#Ebcdic
#Mode b
quote site crtccsid *user
quote site crtccsid *sysval
put prod.txt
quote rcmd sbmjob cmd(call pgm(pmtiprcc0) parm('prod' 'DEV')) job(\$pmtiprcc) jobd(orderbatch)
550-Error occurred on command SBMJOB cmd(call pgm(pmtiprcc0)) job($pmtiprcc) jobd(orderbatch).
550 Errors occurred on SBMJOB command..
221 QUIT subcommand received.

I cannot help thinking since visiting this link that you could be missing this...
QUOTE RCMD OS/400 CL command | program [parameter1, parameter2, . . .parameterx] (Remote Command)
Should your FTP command script be...
quote os/400 cl sbmjob cmd(call pgm(pmtiprcc0) parm('prod' 'DEV')) job(\$pmtiprcc) jobd(orderbatch)

Related

Error when run simple shell script on opensuse15.1: SubEntry: number of args (2) is invalid

The following error occurred every time I tried to run a simple shell script(test.sh):
SubEntry: number of args (2) is invalid
This single line code is in the test.sh:echo -e 'open 192.168.1.123 \nuser root pass \nput test.csv \nquit'|ftp -inv
If I run the code line directly in the command line works OK: the file test.csv is transferred successfully via FTP on server 192.168.1.123.
Any one know why I get that error when I run the shell script?!Thank you!
I found another solution for sending files via FTP using a script.
I use curl to send the file like this:
curl -T your_file ftp://your_IP
This command can be put in a script and automatic run that script.Works for me.

store ftp command output in a variable

I am using bash a script to connect to an FTP server for deleting a file.
I would like to store the output message and code of the delete command executed on the FTP server into a variable of my script.
How could I do this ?
Here is my snippet :
...
function delete_on_ftp{
ftp -i -n $ftp_host $ftp_port <<EOF
quote USER $ftp_login
quote PASS $ftp_pass
delete $1
quit
EOF
}
output_cmd=$(delete_on_ftp $myfile)
...
By the way I do above I only get the message, no way to get the returned code. Is there another way allowing to get the code and the message, in 1 or 2 variables ?
Thanks, Cheers
I just tested the following curl command, which make your task easy.
curl --ftp-ssl -vX "DELE oldfile.pdf" ftp://$user:$pass#$server/public_html/downloads/
Please do not forget the slash at the end of your directory, it is necessary.
curl: (19) RETR response: 550
550 oldfile.pdf: No such file or directory
curl: (19) RETR response: 250
250 DELE command successful
curl is available at http://curl.haxx.se/.
One of the ways to get FTP to act automatically is to use a Netrc file. By default, FTP will use $HOME/.netrc, but you can override that via the -N parameter. The format of a netrc file is fairly straight forward. A line is either a Macrodef or a line that contains login information. Here's an example below:
Netrc File
mysystem login renard password swordfish
another login renard password 123456
default login renard password foofighter
macdef init
binary
cd foo
get bar
delete bar
quit
macdef fubar
...
The three first lines are the logins for various systems. The default is a login for any system which you don't define a particular login for. The lines that start with marcodef are macros you define to do a series of steps for you. The init macro automatically runs right after login. If the last line is quit, it will quit out of FTP for you. There should be a blank line to end the macro, (although most systems will take an End of the File as the end of the macrodef too).
You can create a Netrc file on the fly, enter your FTP command in that, and then, run your FTP command with that Netrc file:
cat > $netrc_file <<<EOF
$ftp_host login $ftp_login password $ftp_password
macdef init
delete $my_file
quit
EOF
ftp -N $netrc_file
You can capture the output via STDOUT, or in a variable and then parse that for what you need:
ftp -N $netrc_file | tee $ftp_output
Other answers on this question should provide you what you want.
However, if you are keen on specifically using ftp command, you can use expect command for the same...
Note, that this is not the best way to achieve what you are trying.
expect -c "log_user 0;
spawn ftp -i -n $ftp_host $ftp_port;
expect \"<add ftp login username: prompt details here>\"
send \"quote USER $ftp_login\r\n\"
expect \"<add ftp login password: prompt details here>\"
send \"quote PASS $ftp_pass\r\n\"
expect \"<add ftp shell prompt details here>\"
log_user 1; send \"delete $1\r\n\"
log_user 0;
expect \"<add ftp shell prompt details here>\"
send \"quit\r\n\";
interact"
You may need to add some more lines in the above for the login & shell prompts returned by the ftp command.

Handle FTP error in shell script

I am trying to do an ftp from a shell script, called by another one(parent script). the code is something like this:
ftp -inv <<EOF
open $hostname
user $username $password
binary
cd $dir
put $renamed_file
bye
EOF
when I check for the return code like:
exitStatus=$?
it always returns 0, even if the ftp fails. I am new to shell scripting and is struggling with how to resolve this. Can someone please help me out?
Thanks!
You aren't going to get back the response you want if you look at the bash (or whatever shell) exit status. Bash thinks the command is working just fine - even if it's really an error. Your best bet is to use "batch mode" (your FTP program should have something like that). Capture any error output to a file or STDERR and parse to find your errors.

Telnet inside a shell script

How can I run telnet inside a shell script and execute commands on the remote server?
I do not have expect installed on my solaris machine because of security reasons.
I also do not have the perl net::telnet module installed.
So with out using expect and perl how can I do it?
I tried the below thing but its not working.
#!/usr/bin/sh
telnet 172.16.69.116 <<!
user
password
ls
exit
!
When I execute it, this is what I am getting:
> cat tel.sh
telnet 172.16.69.116 <<EOF
xxxxxx
xxxxxxxxx
ls
exit
EOF
> tel.sh
Trying 172.16.69.116...
Connected to 172.16.69.116.
Escape character is '^]'.
Connection to 172.16.69.116 closed by foreign host.
>
Some of your commands might be discarded. You can achieve finer control with ordinary script constructs and then send required commands through a pipe with echo. Group the list of commands to make one "session":-
{
sleep 5
echo user
sleep 3
echo password
sleep 3
echo ls
sleep 5
echo exit
} | telnet 172.16.65.209
I had the same issue...however, at least in my environment it turned out being the SSL Certificate on the destination server was corrupted in some way and the server team took care of the issue.
Now, what I'm trying to do is to figure out how to get a script to run the exact same thing you're doing above except I want it to dump out the exact same scenario above into a file and then when it encounters a server in which it actually connects, I want it to provide the escape character (^]) and go on to the next server.

Error logging in via FTP script in KornShell

I am trying to FTP a file using a script in KornShell (ksh) and I am getting a login failed message. I can login manually just fine but when I try the automated script, it does not like the password portion of the login information.
Here's my script:
ftp -n ftp.stmp.com <<EOF
user quser pass Sky3s3ch
binary
hash
prompt
put chr*.dat
EOF
And this is the error that I get:
dns: /u04/lms/ora_shell/clients/STMP > LMS_STMP_ECHI_FTP.ksh
Not logged in.
Login failed.
Please login with USER and PASS.
Hash mark printing on (1024 bytes/hash mark).
Interactive mode off.
Please login with USER and PASS.
Please login with USER and PASS.
I would appreciate any help I can get in figuring this out. Thanks in advance.
there are many different ftp clients, but I'm not familiar with one that requires the word pass as part of a single line login like you are using. Try
ftp -n ftp.stmp.com <<EOF
user quser Sky3s3ch
. . .
EOF
Another common form is to move the hostname inside the ftp input stream, i.e.
ftp -in <<EOF
open ftp.stmp.com
quser Sky3s3ch
. . .
EOF
I don't have my sample code availab.e You may need user on the 2nd line of input, but I don't think so.
Edit
Finally, I noticed you have put chr*.dat in your input script. To transfer multiple files at the same time, you'll need the mput command instead.
I hope this helps.

Resources