putty from a batch file and a script?

19,788
  1. If your goal is to execute shell commands remotely through putty, you should probably look at plink (putty without the gui, in other words an ssh client for windows) and then apply the standard here-doc techniques to plink.

  2. plink is part of the putty collection and is also downloadable from the same page as putty.

  3. If you want to execute a local script you would use

    plink user@host -m local_script.sh

  4. For instance. Assuming you're running on some Windows box (fyi the putty suite also runs on Linux) and want to execute a batch of commands on some remote box you would create a shell script on your local machine (say mount.sh) and run it like this:

    C:\> type mount.sh  
    whoami  
    hostname  
    /usr/sbin/mount -t iso9660 -o ro /dev/cdrom /mnt  
    /usr/sbin/mount | grep mnt  
    C:\> plink remoteuser@remotehost -pw secret -m mount.sh  
    remoteuser  
    remotehost  
    /dev/cdrom on /mnt type iso9660 (ro)
    
  5. Also, it's probably better to copy your public key over so that the password is not coded in some batch file.

  6. Finally, be aware that not all environment variable defined in an interactive shell process will be available in the remote shell process. You might need to 'source' some profile script at the beginning of your script.

Share:
19,788
user3314404
Author by

user3314404

Updated on June 17, 2022

Comments

  • user3314404
    user3314404 about 2 years

    I have a batch file that opens putty just fine. c:\putty.exe [email protected] -pw boyhowdy. But to make this work for me I need to understand how to include a script of a command so it will run under the putty tool. Like mount –o remount,rw / . Or is this something I can do with a tool called pscp. I am a nube to these tools and really could use some guideance. I have a bunck of these scripts and would really like to automate them. Thankyou

  • user3314404
    user3314404 over 10 years
    Sorry but Im taking baby steps here, plink [email protected] -pw boyhowdy remote mount –o remount,rw / or do i need to create a text file for everyone of my command sets example "mount –o remount,rw /" and name it "Script.sh
  • Alain Pannetier
    Alain Pannetier over 10 years
    @user3314404, I've added more detail closer to your use case.
  • user3314404
    user3314404 over 10 years
    Alain Thank you, You have forgot more that I will ever know. I realize I'm getting into something that I don't fully understand just yet so my questions may sound nieve. It is a windows box and the idea is to just drop everything into the cdrive via batch file and run the routines. No concern about the pw its a formality and not a security issue in this case. Why is plink a better solution than pscp. Finally can you suggest reading material? thanks
  • Alain Pannetier
    Alain Pannetier over 10 years
    I was under the impression you were argeting a Unix box because you mention mount –o remount,rw which is not working on Windows. If you want to backup things on c: all you need is xcopy. Also pscp is used to copy files around, and plink is to start an interactive remote shell. Depending on what you want to achieve you will need, one or the other or even both.
  • user3314404
    user3314404 over 10 years
    Sorry about that, The target is a linux system I'm in search of the knowlege to run a batch file from windows and putty, with a command and then pscp with another command. I don't know why I need the two differnt tools, but I get real tired of banging out the descreet commands. The must be a better way, so I am off researching..
  • user3314404
    user3314404 over 10 years
    Is there a way to create a windows batch file, that will run putty and have putty run the mount –o remount,rw / command, close putty, then run a pscp command?