I want to create a batch file in windows to do specific commands on redhat linux server
When user click on batch file
1- login to server using root user
2- restart some service
3- logout from server
Am able to do cron job to do these commands but I want to restart service manually.

Dani AI

Generated

A practical, safe pattern is to have a Windows script call SSH on the Red Hat box to run the restart, but avoid embedding a root password or enabling password-based root SSH. ' plink tip and ’s Cygwin/OpenSSH idea are both valid delivery options; demonstrated the basic remote-exec approach. The recommended workflow is: create an SSH key on Windows, put the public key on the server, give a non-root user permission to restart only the needed service via sudo, then wrap the remote command in a small batch or PowerShell file.

Create a limited sudo rule on the server (edit with visudo or place a file in /etc/sudoers.d). Replace /path/to/systemctl with the exact path from command -v systemctl on your machine:

# /etc/sudoers.d/restart_httpd   (edit with visudo -f /etc/sudoers.d/restart_httpd)
deploy ALL=(root) NOPASSWD: /path/to/systemctl restart httpd.service
Defaults:deploy !requiretty

Example Windows batch (using OpenSSH client). Put your key in a safe place and test manually first:

@echo off
set SERVER=192.168.1.10
set USER=deploy
set KEY=C:\Users\you\.ssh\id_rsa

ssh -i "%KEY%" %USER%@%SERVER% "sudo systemctl restart httpd.service"

Notes and troubleshooting: test the key-based login interactively before automating. If sudo complains about “no tty”, either add Defaults:deploy !requiretty or call ssh with -t. Use -o BatchMode=yes to make the client fail fast when keys are wrong. If you prefer PuTTY tools, use plink with a .ppk and Pageant for key handling. Never store an unencrypted private key on shared machines, and avoid enabling PermitRootLogin over SSH — use a limited sudo rule instead.

Recommended Answers

All 3 Replies

It looks more like a windows question than a linux question. I think the plink.exe tool can execute remote commands through ssh.

You can also do this with the bash shell and ssh from Cygwin on Windows. I used to use those tools extensively for management of Linux servers as well as software development for Linux on Windows.

  • You can run commands on a remote server using SSH.

Example: To restart httpd service on a server with IP: 192.168.1.10 using SSH, you could do the following:

ssh -t root@192.168.1.10 "service restart httpd"

-You could have the above in a bash script named "restart-remote-service.sh" with the following:

#!/bin/bash
ssh -t root@192.168.1.10 "service restart httpd"

-Make sure the script is executable:

chmod +x restart-remote-service.sh

-For a user to execute, it he could run this script from Mobaxterm/WSL in his machine and it will restart ssh on the remote server.

./restart-remote-service.sh

PS: Just saw this is an old post now. LOL!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.