How do i make my .bat file run linux command to remote linux
You really should do man ssh
as this is explained there (and you could also make an internet search to get an answer).
But, to answer your question anyway: you should put all commands you want to run on the remote machine on the same line with the actual ssh command, for example to run directory listing and grep all files containing "foo", do: ssh <user>@<host> 'ls|grep foo'
.
How to write a batch file for running commands in a remote Unix server
There are numerous ways to accomplish this. Personally I would set up an SSH server (such as FreeSSHd http://www.freesshd.com/) on the Windows PC so that everything could be accomplished in one step using the Linux server. If you are not able to install software on your PC, you can still make this work by scheduling scripts on both the Windows PC and Linux server.
First configure SSH public-key authentication to connect from the Windows PC to the Linux server.
This is a more secure alternative to having the password in clear text in your script, and a better configuration for scheduled tasks.
Perform these steps on the Windows PC:
- Open the PuTTYgen key generation utility
- Under "Parameters" select "SSH-2 RSA" and leave the number of bits at the default 1024
- Under "Actions" click "Generate" and move your cursor around when prompted
- Leave "Key Passphrase" blank since you want to automate the process without entering a passphrase every 30 minutes
- Under "Actions", click "Save public key" and provide a file name and location for the key
- Under "Actions", click "Save private key", ensuring the "Save as type" is set to "PuTTY Private Key Files (*.ppk)"
- Open the public key saved in step 5 (VERY IMPORTANT - make sure it's the PUBLIC key, not the private key), and copy the contents into your clipboard
Perform these steps on the Linux Server:
ssh into the Linux server, and using your favorite editor open the ~/.ssh/authorized_keys file. If it doesn't exist, you may need to execute these commands
mkdir -p ~/.ssh
touch ~/.ssh/authorized_keys
vi ~/.ssh/authorized_keysPaste the contents of your Windows public key into this file, then save and close the file
Next, create a scheduled task on the Linux server to keep "myfile" updated every 30 minutes.
Open a text editor and place the contents of your script in the file e.g. "vi update_myfile.sh"
#!/bin/bash
tail -n 100000 conveyor2.log | grep -P 'curingresult OK' | sed 's/FT\ /FT/g' |awk '{print $5 $13}' |sed 's/\"//g' | uniq | sort -n | uniq >> /var/mp/95910/log/myfile.txt
(edit the path as needed)
Schedule the task to run every 30 minutes (at 15 and 45 minutes past every hour) using cron. Run the command "crontab -e", and add this line, changing the path as needed:
15,45 * * * * /home/<username>/update_myfile.sh
Finally, create a script on the Windows PC to copy over the file, at a time offset from when the Linux script runs.
In notepad create a script called copy_myfile.bat containing:
pscp user@IPADDRESS:/var/mp/95910/log/myfile.txt C:\Users\Administrator\Desktop\Myfo
You didn't mention what version of windows you are running on the PC, but if Windows 7, click Start and under Control Panel click "System and Security", then "Administrative Tools", then "Task Scheduler". If Windows 8, just click Start then type "Task Scheduler".
Within Task Scheduler, click Action, then "Create Task" and the Create Task dialog should appear
Under the General tab, give the task a Name and Description
Under Triggers, click New, and in the resulting dialog select Daily, click the "Repeat task every" checkbox and select "30 minutes" and click OK
Under Actions, click New, leave Action set to "Start a program" and browse to the copy_myfile.bat script created in step 1. Click OK
Click OK and the task should be scheduled to run automatically
So every :15 and :45 the Linux server file will be updated, and it should get copied to your Windows PC every :00 and :30. These times can be adjusted as you see fit. If the clocks on the two systems are in sync, you may not need 15 minutes of wiggle room.
Hope this helps!
Run a remote .sh script from a local .bat script
You can user WinSCP
for example put_file.bat
"C:\Program Files\WinSCP\winscp.com" /ini=nul /script="C:\Program Files\WinSCP\ticket_upload.sftp"
for example ticket_upload.sftp
open sftp://login:password@192.168.0.9/ -hostkey="ssh-rsa 2048 d9:e6:2d:ab:0c:72:86:b8:5d:02:7c:c0:80:64:0d:7d"
cd /oracle/app/oracle/bb/abak
mput "C:\bb\abak\DBF.RAR"
call /oracle/app/oracle/extract_bb.sh
exit
Best way to script remote SSH commands in Batch (Windows)
The -m
switch of PuTTY takes a path to a script file as an argument, not a command.
Reference: https://the.earth.li/~sgtatham/putty/latest/htmldoc/Chapter3.html#using-cmdline-m
So you have to save your command (command_run
) to a plain text file (e.g. c:\path\command.txt
) and pass that to PuTTY:
putty.exe -ssh user@host -pw password -m c:\path\command.txt
Though note that you should use Plink (a command-line connection tool from PuTTY suite). It's a console application, so you can redirect its output to a file (what you cannot do with PuTTY).
A command-line syntax is identical, an output redirection added:
plink.exe -ssh user@host -pw password -m c:\path\command.txt > output.txt
See Using the command-line connection tool Plink.
And with Plink, you can actually provide the command directly on its command-line:
plink.exe -ssh user@host -pw password command > output.txt
Similar questions:
Automating running command on Linux from Windows using PuTTY
Executing command in Plink from a batch file
BATCH file to connect to Linux via SSH. How?
Firstly, do not name your batch file ssh.bat
or ssh.cmd
and it will probably be best if you use the full path to the executable:
@echo off
"C:\Windows\System32\OpenSSH\ssh.exe" -p 22 root@10.10.1.100
pause
but it is probably better to use the %windir%
environment variable:
@echo off
"%windir%\System32\OpenSSH\ssh.exe" -p 22 root@10.10.1.100
pause
Related Topics
Using Ssh to Run a Cleartool Command with Agruments on Remote a Linux MAChine
When Installing Rust Toolchain in Docker, Bash 'Source' Command Doesn't Work
Making Proprietary Elf Binaries Portable on Linux
Is It Safe to Call Dlclose(Null)
Script Produces Different Result When Executed by Bash Than by Cron
Shell Script Issue with Filenames Containing Spaces
R Package Installation in Linux
Linux Split a Column into Two Different Columns in a Same CSV File
How to Extract Patterns Form a Text Files in Shell Bash
How to Get the CPU Time for a Perl System Call
Write to Port 0Cf8H Fails with Segfault
Delete Repeated Characters Without Back-Referencing with Sed
Linux Zip and Exclude Dir via Bash/Shell Script
Commands Will Not Pass to Cli After Logging into New User with Sudo Su - User
Assembly Call Subprograms Based on User Input
How to Increase the Maximum Number of Characters That Ksh Variable Accepts