Putty script + command file
scriptPutty
-
ryko1820 Posted messages 1633 Registration date Status Member Last intervention -
ryko1820 Posted messages 1633 Registration date Status Member Last intervention -
Hello,
I have a script to run from Windows that is supposed to connect to an Unix machine (Red Hat Linux Enterprise), upload a text file, launch a bash script on the server, and download the log file back to Windows.
I was thinking of scripting putty and passing a .sh file as an argument which would be executed on the Unix machine.
In my .bat:
start /w putty.exe -load "SESSION" -l user -pw mdp -m commandesPutty.sh
If I remove the -m parameter, putty launches and I am authenticated, the connection is fine.
Now, when I include the -m parameter, my putty window opens with:
Using username "user".
and closes immediately. My commandesPutty.sh file:
#!/bin/bash
cd /logiciels
I don't know how to get this file executed. It is found correctly (if I put a different name, it returns an error). If this file is empty, the window remains in:
Using username "user".
without ever closing.
Thank you for your ideas.
Configuration: Windows Vista / Chrome Indeterminable
I have a script to run from Windows that is supposed to connect to an Unix machine (Red Hat Linux Enterprise), upload a text file, launch a bash script on the server, and download the log file back to Windows.
I was thinking of scripting putty and passing a .sh file as an argument which would be executed on the Unix machine.
In my .bat:
start /w putty.exe -load "SESSION" -l user -pw mdp -m commandesPutty.sh
If I remove the -m parameter, putty launches and I am authenticated, the connection is fine.
Now, when I include the -m parameter, my putty window opens with:
Using username "user".
and closes immediately. My commandesPutty.sh file:
#!/bin/bash
cd /logiciels
I don't know how to get this file executed. It is found correctly (if I put a different name, it returns an error). If this file is empty, the window remains in:
Using username "user".
without ever closing.
Thank you for your ideas.
Configuration: Windows Vista / Chrome Indeterminable
2 answers
Hello,
it seems to me that you want to do 2 things at the same time with your putty:
run a command via ssh (ok) and a file transfer (via sftp) (not ok).
Why not use the command line tools provided in the putty directory (plink and psftp or pscp) in a batch?
This will create a .bat that looks like
(the -batch is just to say "no interactive display because we're in script mode")
with a parameter file "c:\your_local_path\commandes_sftp.cmd" that will contain:
Also check that the command "commandesPutty.sh" you are running on the server is executable and that you have the rights.
EDIT: Is the "commandesPutty.sh" file on the server?
(Otherwise use the -m option of plink by passing it a local file like with the "-b c:\temp\test.cmd of psftp" = "C:\Program Files (x86)\PuTTY\plink" 192.168.1.XX -l yourusername -pw yourpassword -batch -m c:\your_local_path\commandesPutty.sh)
Finally, if this is a sensitive topic, in order to avoid having passwords in plain text in a batch, set up the public/private key system between your server and your client (plus, no need to type the password every time, you’ll see, it prevents forgetting it :) although after typing it many times … )
it seems to me that you want to do 2 things at the same time with your putty:
run a command via ssh (ok) and a file transfer (via sftp) (not ok).
Why not use the command line tools provided in the putty directory (plink and psftp or pscp) in a batch?
This will create a .bat that looks like
@echo off "C:\Program Files (x86)\PuTTY\plink" 192.168.1.XX -l yourusername -pw yourpassword -batch /home/toto/commandesPutty.sh "C:\Program Files (x86)\PuTTY\psftp" 192.168.1.XX -l yourusername -pw yourpassword -batch -b c:\your_local_path\commandes_sftp.cmd
(the -batch is just to say "no interactive display because we're in script mode")
with a parameter file "c:\your_local_path\commandes_sftp.cmd" that will contain:
cd /logiciels get my_file_to_retrieve
Also check that the command "commandesPutty.sh" you are running on the server is executable and that you have the rights.
EDIT: Is the "commandesPutty.sh" file on the server?
(Otherwise use the -m option of plink by passing it a local file like with the "-b c:\temp\test.cmd of psftp" = "C:\Program Files (x86)\PuTTY\plink" 192.168.1.XX -l yourusername -pw yourpassword -batch -m c:\your_local_path\commandesPutty.sh)
Finally, if this is a sensitive topic, in order to avoid having passwords in plain text in a batch, set up the public/private key system between your server and your client (plus, no need to type the password every time, you’ll see, it prevents forgetting it :) although after typing it many times … )
Thank you for the response.
The sh script is on the Windows side, hence the -m option. But I saw in examples online that it could be in txt format. It just contains Unix commands to execute.
So initially, I would like to be able to execute Unix commands via my Windows batch.
What I don't understand is why the commands specified in my file are not being executed.
If you have any ideas, I'm all ears.
Thank you.
The sh script is on the Windows side, hence the -m option. But I saw in examples online that it could be in txt format. It just contains Unix commands to execute.
So initially, I would like to be able to execute Unix commands via my Windows batch.
What I don't understand is why the commands specified in my file are not being executed.
If you have any ideas, I'm all ears.
Thank you.
Plink and psftp only execute the commands they find in the file passed as a parameter (with -m or -b). It must be a simple text file (created with Notepad, Notepad+, vi, or other "clean" editors to avoid unwanted characters).
To debug this, you need to manually run the commands from the parameter files in a classic Putty session (copying/pasting the command into the terminal) to see if they execute normally.
File transfer is impossible in Putty (unless there is an sftp or ftp server on the client, in which case you use the server's client to send, but that's another story :))) but there could be a permissions or path issue with the commands executed by your script on Linux.
To debug this, you need to manually run the commands from the parameter files in a classic Putty session (copying/pasting the command into the terminal) to see if they execute normally.
File transfer is impossible in Putty (unless there is an sftp or ftp server on the client, in which case you use the server's client to send, but that's another story :))) but there could be a permissions or path issue with the commands executed by your script on Linux.
with a parameter file "c:\your_local_path\put_sftp.cmd" containing:
and a parameter file "c:\your_local_path\get_sftp.cmd" containing:
(the .cmd extension for parameters files is optional, you can put whatever you want...)