Remote linux command from windows

Run custom command on remote Linux machine from windows machine using batch file [closed]

I want to run a custom command on remote Linux machine from my windows machine using batch file. The custom command returns integer value, which will be further used by batch file. How can I achieve this? Right now, I am invoking winscp from batch file and writing the output of command from winscp to a file remotely then using get command of winscp , I am downloading that file into my windows machine. After that I am reading that file from batch file. Okay but very long process. My aim is to get the output of command available to batch script, so that value can be used further in batch file.

Because your question doesn’t belong here but on serverfault. If the close will gain enough votes, your question will be moved from here to serverfault and not just closed. 😉

2 Answers 2

Another option is plink.exe. It is a command line version of PuTTy, and if you have PuTTy installed, then you already have plink (in same directory as PuTTy)

From Batch, this will look something like this:

%PATH_TO_FILE%\plink.exe -batch root@10.0.0.1 "remote command" SET RETVAL=%ERRORLEVEL% IF NOT ["%RETVAL"]==["0"] ( ECHO "Remote Command failed" ) ELSE ( ECHO "Command returned with exit code %RETVAL% ) 

Slav: i am using %PATHTOPUTTY%plink.exe root@192.168.10.202 -pw test@123 -m commands.txt ECHO This is out %ERRORLEVEL% In console, i am getting This is out 0, which is incorrect. I just want to assign the returned value from plink.exe to some variable, so that i can use it further.

That is the return value of your last linux command. Same if you would type echo $? after the last linux command. Maybe what you are asking for is not the return code (error code, exit code), but you want the actual output of the command? In that case, you’ve got to redirect that to a file on the Windows box. whole_plink_command_ending_in_-m_commands.txt > outFile.txt . Then read that file into a variable: SET /P Value

Читайте также:  Обновление ядра linux centos

SET /P Value

The syntax is this: FOR /F «tokens=*» %%A IN (‘any_command’) DO (SET Value=%%A) . Replace «any_command» with the whole Plink stuff, minus the file redirect. The difference (besides inability to do error handling), is that the first method will put the first line of multi-line output in variable, while this second method would pust the last line of multi-line output in variable. Doesn’t matter if your output is a single line.

Источник

How to remotely control Linux Systems from Windows

With remote access, a user can access the files and the data in another system from a remote location without having real-life physical access to the system, pretty cool. Right? Remote access is beneficial and helps a lot in saving time.

Why establish a remote connection to Linux from Windows?

Suppose you are running a business, and you have a meeting with board members in about 10 minutes. All of a sudden, you remember that your presentation file is on your home system; remote access can potentially save the day.

Or you are running a windows system in one location and your Linux system in another room, and you want to run some commands on your Linux or retrieve some files from your Linux, and your Linux is inaccessible, or you are just too lazy. Either way, in all cases, remote access provides great convenience and saves much time.

Linux Ubuntu:

Now out of all the popular Linux distros, Ubuntu is by far the most popular one. All of the other Linux distros combined make up a total of a mere 35% of Ubuntu (google search trends). Upon searching Linux, 161 million results appear, while the next most popular Linux distros i.e., the Debian Linux, has a mere 65.9 million search results, so keeping in mind the above stats, I will be using Ubuntu for this tutorial. To establish the connection, you would need to access the Ubuntu machine physically. But don’t worry, this is a one-time thing.

There are many methods through which you can remotely access your Linux from windows. Here I will be sharing three ways through which you can access remotely control Linux Systems from Windows.

Читайте также:  Что такое переменная окружения linux

Find the IP address:

Regardless of what method you use, finding the IP address of your system is required almost in every case. Following is an easy way to find the IP of your Linux system.

Go to the terminal in your Linux machine and type the following command:

Now enter the following command:

After writing the above command, your terminal window will look like this

You will find your IP address next to “inet” in the first section. In this case, as you can see from the above screenshot, the IP address is “10.0.2.15”

If the method mentioned above doesn’t work for you, you can try the following ways:

In the terminal, write the following command. Remember, this works for some of the Linux versions like Ubuntu.

Method 1:Remote Access using SSH (Secure Shell)

For this method, you should install the PuTTY software, as it’s better than using windows default SSH function. PuTTY establishes an SSH connection between your Linux and your Windows desktop, which gives you access to the Linux terminal.

After Installing the PuTTY software write the name of your Linux system, or it’s IP address under the “Host Name (or IP address)” label. Make sure to set the connection to SSH if it isn’t. Now click open. And voila, you now have access to the Linux command line.

Note: This will give only you access to the terminal, i.e., there will be no mouse control. Still, this is useful to install software using Terminal commands

Method 2:Using RDP (Remote Desktop Protocol)

RDP is the easiest method so far. The RDP tool is already installed in the windows. So the windows system doesn’t require any work (software-wise). However, this is not the case for your Linux system. On your Linux system, you will need to install XRDP tool. For this open the terminal (Ctrl+ Alt+ T) and type the following command

Press “y” and hit enter. It will then continue to install.

The first command will install the XRDP tool. The second command will allow the auto-enable on startup option. In other words, the xrdp tool will automatically start when the Linux boots. After this process completes. Go to your windows system and in search bar type “RDP.” Click on the “Remote Desktop App.”

Читайте также:  Linux bash copy directory

Type the IP address of your Linux system adjacent to the “Computer ” label, and click connect

Enter the Username and the Password of your Linux system.

TroubleShooting: Although connecting through RDP is the easiest method for remote connecting, some problems occur during connecting with Ubuntu. This started happening after the Ubuntu 18.4 LTS release. Linux doesn’t like it when it is accessed remotely when the user is logged in. So a simple solution is to try RDP after logging out of your Linux

Method 3:Setting up a connection using Virtual Network Connection (VNC)

You can also connect using VNC. As mention above, SSH provides access to the terminal (no mouse control). VNC provides access to the Linux desktop (full mouse control). However, there is some prerequisite for connecting using this method, i.e., you have to install some VNC software.

Go to your Linux system and type the following command to make sure it’s up to date

Now input the following command

Now create a password for your connection. You will be given a desktop number, mostly 1. Remember this number.

After this, install the VNC client on windows. These TightVNC tools are installed in bundles, so once installed, go to the windows search bar and search “TightVNC Viewer. “ Launch the app and enter the IP address of the Linux device.

Now enter the IP of your Linux system followed by the desktop number, as shown in the above screenshot.

Conclusion:

Now that you know these methods, you should know which way is best for your use.

  1. RDP uses xrdp which is open source
  2. SSH can be used to remote access the terminal
  3. VNC can be used instead of RDP, but it’s a little less secure

It is recommended to set up SSH regardless of what method you use as it makes the installation for the other two methods much more easier.

Ubuntu has a built-in remote desktop tool, which is VNC-compatible.

About the author

Zeeman Memon

Hi there! I’m a Software Engineer who loves to write about tech. You can reach out to me on LinkedIn.

Источник

Оцените статью
Adblock
detector