Run a script in remote UNIX using windows command prompt - shell

I currently connect to my UNIX installed on my remote server using Secure CRT. I have a script(sh) on my UNIX which generates a file. I need to su(switch user) before executing the script.
I then ftp to this server(using ftp commands in command prompt on windows) and get the file generated by the above shell script.
However, I would like to automate this process and create a windows .bat file which would connect me to the UNIX server, su and run the required shell script and fetch the file.
I have found that I cannot run the shell script on UNIX using ftp. Could you please suggest me alternate ways to automate this?
Thanks

Thanks for your help guys :) I created a script on secure CRT and while running the application using the bat file, I'm calling this script using the CRT command line options

Related

Opening Cygwin with Windows bat file and running script file

I require a tunnel between my windows machine to a UNIX server and wish to automate the process so that on startup the tunnel will be generated for me.
I installed Cygwin with ssh and autossh to connect to the remote server, built up the connection manually, and have confirmed that the connection works. The process involves 3 commands, which isn't a lot but something that would be great to have automated.
After creating a .sh script file, which includes my autossh connection commands, and saving it using Notepad ++ as a UNIX document (to avoid any potential conflicts regarding the file ending), I can navigate to this script in Cygwin and call bash script.sh. After which the connection is made and I can work on my server.
My problem comes when creating my bat file:
start /d "C:\cygwin\bin\" mintty.exe "C:\Users\user\Documents\Dev\" script.sh
The first part up to and including the .exe file works to open the Cygwin window, but I have been unsuccessful in feeding the script into it. I even tried including a --bash command before referencing the script file as follows, but I received an error that the command is unknown:
start /d "C:\cygwin\bin\" mintty.exe --bash "C:\Users\andrew\Documents\Development\" tunnel.sh
Does anyone know if and how it is possible to open a Cygwin window and call a script file within this window? This is my first time creating a bat file, so I hope this is perhaps a newbie problem that no one even bothers to post a solution online for...
you don't need start.
assuming your Cygwin is in C:\cygwin
you need just:
chdir c:\cygwin\bin
mintty /usr/bin/bash -l -c /cygdrive/c/Users/user/Documents/Dev/script.sh

Download files from Unix server using WinSCP with get command

I have program in Unix that generates files 8 files at 8:30 pm everyday.
I need to download the files into my Windows machine after the files are generated using WinSCP.
I can drag and drop those but its time consuming, I need to automate this process.
I cannot use .Net assembly to download those.
I have tried to use the get command, but its throwing error: Not an SCCS file.
Let me know how can I solve this.
--Thanks
To automate a task using WinSCP, use its scripting interface from a batch file (e.g. download.bat).
I assume you want to use SFTP, as you are connecting to a *nix server.
The simplest download batch file is like:
winscp.com /log=c:\path\to\log\winscp.log /command ^
"open sftp://username:password#example.com/ -hostkey=""xxx""" ^
"get /path/to/file.ext c:\path\to\download\to\" ^
"exit"
Replace the username, password and example.com with the actual connection details. Get the value of -hostkey switch from your GUI session. Use real paths.
Though it's easier to have WinSCP generate the batch file for you.
For details see a guide to automating file transfers from SFTP server using WinSCP.
Once you have the batch file working, schedule the the batch file execution using Windows scheduler.
See a guide to scheduling file transfers from SFTP server.

Daily upload of file automation using batch script and WinSCP

So I do have a file that I generate weekly from a server using crontab in Linux side and transfer it to my PC. However, I am having a problem when try to send the file that I generate from a different server on Windows side using task scheduler.
Your command-line syntax is wrong.
I'm assuming the \ftpBinverlog_%yyyy%-%mm%-%dd%.txt is the file, you want to download.
It won't work, if you just specify it on command-line like you did.
Also neither Windows scheduler, nor command-interpreter, nor WinSCP understand syntax like %yyyy%.
The path to the remote file does not look good either. *nix systems use forward slashes, not backslashes.
So just keep your /script and /log arguments:
/script=C:\batchrun\Binver\script.tmp /log="C:\BIN VERIFICATION\ftplog"
And make sure the script.tmp looks like:
open sftp://user#example.com
get /ftpBinverlog_%TIMESTAMP#yyyy-mm-dd%.txt C:\target_path\
exit
References:
Guide to automating file transfer from SFTP server
%TIMESTAMP syntax.
Develop a batch file which will download/upload the required file using the SCP command
check this for more details.
check this for more details about the scp command parameters.
Make sure that you are able to run the batch with a successful result, then configure it within a scheduled task.
I hope this could help.

How to change remote file permission via FTPClient

I need to handle remote file on a ftp server via commons-net-1.4.1.jar,class org.apache.commons.net.ftp.FTPClient.It's seems that no api available to change a file's permission,just like unix shell command chmod.
Anyone know any solution to resolve this issue?
FTP doesn't have a command for such operations, since the operation is platform-specific. If your library supports this, you can try using SITE command and pass the command line as a parameter of SITE command. Some servers support this trick to run shell commands.

BAT script to copy files from Windows to remote Linux systems

Is there anyway to copy files from Windows machine to a remote Linux machine with a DOS command/other command-line tool (by specifying username and password in the command). I normally do this using WinSCP and would like to write a script (BAT) to automate this.
You could use the command line version of PuTTY, pscp.exe.
WinSCP scripting command-line to upload a file is like:
winscp.com /command "open sftp://username#example.com/" "put d:\www\index.html" "exit"
See the guide to WinSCP scripting.
Easier is to use a Generate transfer code function to have WinSCP GUI generate a script (or even a complete batch file) for a transfer.
Download a copy of pscp.exe (the PuTTY scp companion). If you have setup SSH keys on the Linux server, which you can do with PuTTY on Windows, you can setup password-less copy to Linux machines from Windows.
Install cygwin and you can use scp, ssh etc just like you would on linux. Besides, you can use ordinary bash scripts instead of crappy bat-files.
If anyone is looking to do this in 2022, Windows 10 now comes with scp. You can do
scp path/localfile.txt remote-user#host:/home/path
or the recursive version for directories
scp -r localfolder remote-user#host:/home/path
Of course with scp you'll run into issues if you have a large number of files. It copies everything as opposed to only changed / new files only.
Then you'll need a tool like rsync, which is available through WSL (windows subsystem linux).
rsync -r localfolder remote-user#host:/home/path
(I personally hesitate to install new tools for a job, hence my desire to stick with what's already available)

Resources