How can i run the shell script from ABAP programming language

I am in need to execute the Files transferring's shell script from ABAP programming language. it would be highly appreciated if you help me as quickly as possible. Thank you

Calling Unix Shell Scripts From ABAP

Hi, Thank you for sending me the link. I just followed the process as per the link that you sent it to me. I have my SAP on UNIX and it doesnt have a ZSHELL Command but there is a other command ZTELNET which was created by a ECC guys for their purpose. plz let me know your idea that i can create a command like zshell and use it for the configuration purpose? Thank you

This guy uses no ZSHELL: The SAP Fan Club Forums � View topic - howto run ksh shell script in a ABAP program?

How are you buddy? Again buzzing you for another doubt...
i was told to drop the flat file and script file in the same (Ex: /usr/sap/xxx/comm/interface/FI/CO)directory in application server.
My question is:
Is it a good idea to drop the shell script and moving the test file to the same directory in application server?
Because i need to load the flat file from the same directory to SAP BI?
and again i need to execute my ABAP program to run the shell script for transfer the file from FTP server to the same directory in Application server.
If i keep the flat file and shell script in one directory, would i get any problem in going forward? like security/permissions or any conflicts between this etc? also, if you had experienced any of the other problems then plz share with me. Thank you.

It doesn't matter where the file is at all as long as the user running the script can read it.

You can control file and directory permissions individually, so just having them in the same directory isn't necessarily a security problem. What permissions the files and directory are set to, and who can read or modify them, is more important.

Just dumping them all in the same dir may not be the most organized way to do it either, though.

Without seeing this script I'm not sure, but it's quite possible it'll either accept it from anywhere or can be modified to do so.

Please see the below script. also, plz suggest me if that is the proper order, if so, would it work for all of my requirements like connecting to FTP server(windows) and sending it to application server (linux), then converting the file from .xls into .csv and finally sending a email with the file attachment to the user. It would be higly appreciated if you can help me. also plz suggest me how i can give you the points if any points could be assigned to your help. Thank you again.

#!/usr/bin/expect -f
# SETUP MY ENVIRONMENT
set force_conservative 0; 
# set to 1 to force conservative mode even if script wasn�t run (# # # )
# Conservatively originally.
If {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}
# CONNECT TO FTP SERVER. After connecting, we can view the flat file #on main page itself. FYI.. It is not put under some folder in the FTP #server.
Set timeout -1
spawn $env(SHELL)
match_max 100000
send -- "sftp FICO@ABC.YAHOO.com\r"
expect -exact "Connecting to ABC.YAHOO.com...\r
FICO@ABC.YAHOO.com's password: "
send -- "ABC123\r"
expect -exact "\r
sftp> "
# COMMANDS TO EXECUTE
# FOR MOVING THE FILE FROM THE ABOVE connected server (FTP Server)
send -- "mget GET_FILE* /usr/sap/NXA/COMM/interface/FICO/"
expect -exact "mget GET_FILE* /usr/sap/NXA/COMM/interface/FICO/�
Send -- "\r"
expect -exact "\r
sftp> "
# FOR CONVERTING THE FILE FROM .XLS TO .CSV
send �- �for I in GET_FILE.*\r�
expect �exact �for I in GET_FILE.*\r�
send �- �do mv $i GET_FILE_123${i#GET_FILE}.csv\r�
expect �exact �do mv $i GET_FILE_123${i#GET_FILE}.csv\r�
send �- �done\r�
expect �exact �done\r�
send �- �\r�
expect �exact �\r
sftp> �
# SEND AN EMAIL TO THE PARTICULAR USER THAT FILE HAS BEEN MOVED #TO THE /usr/sap/NXA/COMM/interface/FICO/. PLZ SEE THE DIRECTORY.
send -- "uuencode GET_FILE_123.CSV GET_FILE_123.CSV.MAIL | mail -s "GET_FILE_123.CSV" VENKAT@yahoo.com\r"
expect -exact "uuencode GET_FILE_123.CSV GET_FILE_123.CSV.MAIL | mail -s "GET_FILE_123.CSV" VENKAT@yahoo.com\r"
send �- �\r�
expect �exact �\r
sftp> �
# SHOULD REMOVE THE FILE FROM FTP SERVER AFTER MOVED TO THE #APPLICATION SERVER
send -- "rm GET_FILE*"
expect -exact "rm GET_FILE*"
send -- "\r"
expect -exact "\r
sftp> "
send -- "bye\r"
expect -exact "bye\r"
send -- "exit\r"
expect eof

This script doesn't look right.

First of all, it's got a password kludged in with expect. Having to use expect to do it is a subtle hint, in mile-high neon letters, that you really shouldn't be doing this. ssh, sftp, scp, su, and sudo all require a tty for passwords as a security precaution, not because they're badly written. They did this because because stored passwords are an extremely bad idea. ssh also provided a safe alternative, shared keys.

If you use shared keys, you won't have to kludge anything with expect. Just having the right files in the right places on the client and server will allow sftp username@host to connect without prompting. There's articles all over the net for passwordless ssh/scp/sftp (all the same protocol connecting the same way). It'd make your code much less convoluted, too.

I also see what you mean about having the files in the same directory, now. Your script is downloading these files, it's not fixed data. It should be putting them in a separate directory somewhere, because you don't want the script to have write-access to the directory it's running itself from. It'd be capable of accidentally deleting itself if something really messed up.

Also, just renaming xls to csv doesn't convert the data from xls to csv any more than putting a book in a different cover changes the contents. The usual way to convert excel to csv usually involves perl scripts. I'll see if I can find the right module.

#!/bin/sh

sftp username@host <<EOF
cd /remote/path/
lcd /local/path/
mget GET_FILE*
EOF

# This doesn't do what you think it does.  Renaming a file doesn't change
# the contents, it's still an Excel file!
for I in GET_FILE.*
do
        mv $I GET_FILE_123${I#GET_FILE}.csv
done

# SEND AN EMAIL TO THE PARTICULAR USER THAT FILE HAS BEEN MOVED #TO THE /usr/sap/NXA/COMM/interface/FICO/. PLZ SEE THE DIRECTORY.
# Not sure these filenames are quite right.  And isn't there actually more than one?
uuencode GET_FILE_123.CSV GET_FILE_123.CSV.MAIL | mail -s "GET_FILE_123.CSV" VENKAT@yahoo.com

---------- Post updated at 10:58 AM ---------- Previous update was at 10:49 AM ----------

The obvious thing to search for was xls2csv, and here it is.

I understand that expect is a patched solution with no.of alternatives to avoid a stored passwords.
Since you are throwing a constructive answers for my problem, could you plz make me to stand out of the crowde while writing the complete code in reply using the shared keys (SSH)?
As you are aware, the previous script helps you in giving you the complete file name, Host names, passwords (if required in ssh - I beleive this is a password less) and the exact requirements as mentioned the pseudo code in comments.
I am really nagging myself and in confusion mode to take a right decision.

Also, as conversion of the extension is one of my requirement, i need it in .csv why because SAP BI accepts .CSV only ofcourse accepts others too but risky procedure.

Question: Let us assume, if we dont mention the password in the SSH (script) then how could it be login to FTP Server and moves it to the application server. Hope it make you a clear. i am only looking for the complete code at the moment instead. Thank you.

That's the beauty of it, it is the complete code. You don't have to tell sftp, scp, and ssh to use keys, they check for you. Just having the right files in the right places is enough.

On the client: $ ssh-keygen -t rsa and just keep hitting enter, letting it save in the default location of ~/.ssh/id_rsa and giving it no password.

Then ssh-copy-id username@server . It will prompt you for a password, log into the server, and copy the necessary bits of information into that users ~/.ssh/ directory on the server.

ssh/sftp/scp username@host should now go through without prompting whenever you're logged into that account.

If the program assumes the files are comma-seperated values, it will either crash, give up, or produce garbage when you feed it that excel spreadsheet. Renaming xls to csv doesn't make the binary data inside become comma-separated text, the same way that renaming .jpg to .mpg doesn't turn a still picture into an animated feature.

You'll have to use something more sophisticated, something that understands xls files and can output comma-separated text, such as the program I linked you.

I am now confusing that understanding of the solution. is it possible for you to drop your contact number? i really need to speak to you to figure out the stuff ASAP. Thanks

I've given you all the details I have to give. I've given you all the resources needed to finish. I don't know where you're confused.

No. My cell phone connection stinks anyway.

It'd help if you responded to this topic more often.

First i should say you very sorry to keep on troubling you. Because this is the real time application i am here working on. i am scrwed up here at this step.

Ok now i go by simple questions. I am expecting the 100% practicle code instead of theriotical explanation:

Q1) #!/bin/sh

sftp FICO@share.dteenergy.com<<EOF
cd /remote/path/ (as i written in previous code -
"
# CONNECT TO FTP SERVER.
Set timeout -1
spawn $env(SHELL)
match_max 100000
send -- "sftp FICO@share.dteenergy.com\\r"
expect -exact "Connecting to share.dteenergy.com...\r
FICO@share.dteenergy.com's password: "
send -- "ABC123\r"
expect -exact "\r
sftp> "
"
I am also not sure that i have a remote path. i asked my team no.of times for the same. They said like below:
(The above FTP server doesnt have a remote path (/remote/path/) because the above code uses the user ID and PW along with the Host name and log into it. after you login, you will have only one file. that is a "FLAT FILE" (test file). so finally we will get(move) that file from that remote server to the local path.
-----------------------------------------------------------------------
lcd /usr/sap/NXA/COMM/interface/FICO/
mget GET_FILE*
EOF

Q2) plz suggest me how could i detect the remote path if you think that we can find the same after log in to FTP server? means step by step process. FYI, this is a tool on windows O.S. and file here is with .xls extension (GET_FILE.xls)

Q3) After we find the remote path, the below code is enough to go head and push the file (GET_FILEP) from remote path to local path. FYI, local path is on Linux O.S.

Code:
sftp FICO@share.dteenergy.com <<EOF
cd " " (remote path)
lcd /usr/sap/NXA/COMM/interface/FICO/ (local path)
mget GET_FILE*
EOF

Q4) since i should convert the flat file (GET_FILE.xls) extension into .csv, could you plz help me in sending the extensions conversion code and how i should club these both (moves the file and conversion) together to get the proper out put?

I feel it is not only help but more than that. i am certainly here to help you in return if i can. Thank you so much again. venkat

what is all the expect and spawn stuff for?

The code for sftp with shared keys is easy.

 sftp username@host <<EOF
stuff
EOF

no expect madness required. That's the whole point.

Then don't use one. I was just trying to be complete.

Do you actually need a remote path? Can you just sftp into username@server then MGET *.xls? Have you actually tried? Try just typing it into a terminal and see what happens.

I linked the code to use for that several posts ago. You have been reading the things I linked to you, right?

I am sorry if i frustrate you at the repeated questions.

Then don't use one. I was just trying to be complete.
I am reallly not sure. because we are very new to this FTP server. They just provided me a GUI tool (FTP server's flat files stores in the main folder under main object) to login to it with the User ID and PW and then i can see it. I wants now to ask you that plz suggest me that how i can find the remote path to put in SSH code?

Do you actually need a remote path? Can you just sftp into username@server then MGET *.xls? Have you actually tried? Try just typing it into a terminal and see what happens.

I tried it long time ago, it was saying that EXPECT is not installed in your system. also, as you said "no expect madness required", that is the reason i wants to switch it to "SSH"
So, i wants to check with you finally the below code is a complete code that you are suggesting me to compile and execute? or does it need any changes if i did wrong specifics?. I am only looking for the code at the moment that can push the file from FTP server (Windows OS) to Application server (Linux OS - /usr/sap/NXA/COMM/interface/FICO/)
_____________________________________________________________
sftp FICO@share.dteenergy.com <<EOF
cd /usr/sap/NXA/COMM/interface/FICO/
lcd /usr/sap/NXA/COMM/interface/FICO/ (local path)
mget GET_FILE*
EOF
_____________________________________________________________

I have absolutely no idea what you tried, but whatever it is, it's not what I suggested.

sftp username@host <<EOF
mget GET_FILE*
EOF

Type it into your shell.

Does it work or not?

you can stop putting expect junk back into it, then :wink:

It's a shell script. There's no compile, just execute.

I have no idea if it needs changes. That depends on what files are where and where you are when you login and where you want to store the files. There's no way for me to do everything for you. You're going to have to start trying things, and start reading instead of copypasting. It'll help to do things in parts, instead of trying everything at once. Get your sftp working first, if that doesn't go then the rest is pointless.

Your sftp server being Windows could make it tougher to use keys though. Depending on what it is, it might still be possible, but copying the keys might not work the same way. try ssh-copy-id first though and if it works, it works.

As i mentioned, i am very new to this subject. if possible, please provide me the code otherwise, let us pass it to the other users if somebody wants to help me.
I am a SAP guy. not a Linux. I am trying to move the flat file from FTP server (It is on Windows O.S) to Application server (Linux O.S - This i know the path to where it should get). Infact, i dont have a access to execute a single commands or entire script at linux level. for that i again have to write a ABAP program to execute that script. So plz help if you can. Thank you so much.

It'd be nice to have known that you don't have shell access days ago...

I'm not stopping other users from contributing. I presume they're not because they lack the same information I and perhaps you do.

I can't give you a canned script for all the reasons I mentioned before. These reasons apply to other folks as well I think, they're not psychic either.

I have no idea how they expect you to write a UNIX script without shell access. It's going to be a monumental task to troubleshoot it and/or install the xls converter without it.

Are you sure you don't have access? You think you'd need shell access to run shell scripts. Try logging in with putty, same username/password as your windows ABAP thing is using to connect to the linux server.

It'd be nice to have known that you don't have shell access days ago...
Yes you will get to know in going forward that once you get a chance to work on three tier architecture.

I'm not stopping other users from contributing. I presume they're not because they lack the same information I and perhaps you do.
Thanks for not going to stop other users from contributing. I don think so. I hope everybody is not here for needing the information. sometimes others may come up with the updated information as they might have already faced the same kind of problems and solved it by theirselves and the same can be passed to us.

I can't give you a canned script for all the reasons I mentioned before. These reasons apply to other folks as well I think, they're not psychic either.
No problem. I can understand the reason why you cannot give and what kind of psychic you are.. I dont think that other folks as well, may be let us wait and watch the game. in worst cases, if that doesnt happen then i will recommend my PM to call a shell script developer who has worked in multiple environments.

I still beleive this forum that we have very good experts like DgPicket and etc. Hope this will get resolved.

I have no idea how they expect you to write a UNIX script without shell access. It's going to be a monumental task to troubleshoot it and/or install the xls converter without it.
As far as i presume that you have a good experience in your subject but that wont be enough to contribute in forums atleast before commenting against others questions. let us wait and watch the game if somebody come up with the solution or i may post the answer too after a while.

Are you sure you don't have access? Try logging in with putty, same username/password as your windows ABAP thing is using to connect to the linux server.
Yes i am sure that we dont have a access to execute the commnd wise/scriptwise at unix level. i have a option in SAP that i can develop a own command like telnet (Ex:) to test the single commands and need to write a ABAP program to execute the entire script.

The reason there is an sftp command is to support old FTP scripts (and those so old they cannot learn a new command?). For new work, scp seems much simpler and more powerful.

If you use relative paths in sftp, you can use the same relative paths in scp.