putty from a batch file and a script? - batch-file

I have a batch file that opens putty just fine.
c:\putty.exe root#192.168.12.34 -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

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.
plink is part of the putty collection and is also downloadable from the same page as putty.
If you want to execute a local script you would use
plink user#host -m local_script.sh
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)
Also, it's probably better to copy your public key over so that the password is not coded in some batch file.
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.

Related

Running a HTA file remotely using PSTools

Its almost certain that HTA files are obsolete, but i've found that they are much better than net send / msg.
I'm trying to run a HTA file on a remote machine using PSTools, but instead of it running, it brings back a broken window:
Running the HTA file using CMD (locally) works perfectly though.
My PsExec line:
PsExec.exe -accepteula -i -d \\itwall cmd 'mstha \\intranet\Downloads\VisitorSystemNewMessage.hta asd'
I even tried to run the HTA from a Batch file, but the exact same thing happens.
Any ideas?
It's because the account running the command cannot interact with the session of the remote user.
Use the -s switch to run the HTA using the system account of the remote computer.
Also, you shouldn't need to run cmd. You should be able to just specify mshta.exe then your arguments.
PsExec.exe -accepteula -s -i -d \\itwall mshta.exe \\intranet\Downloads\VisitorSystemNewMessage.hta asd
Edit: To illustrate that this is not an HTA issue. Run the following command:
PsExec.exe -accepteula -i -d \\itwall notepad.exe
Notice you'll have the same black window showing.

Execute sftp commands on remote server using batch file and PuTTY

I have to transfer a file from one server to another. I log in to the first one with PuTTY and then type this:
sftp -v -oIdentityFile=path username#host
cd path
put file
Everything works perfectly! Now I'm trying to do it with a batch file. In the .bat I have:
putty.exe -ssh host1 -l username1 -pw password1 -m script.txt
In the script.txt file:
sftp -v -oIdentityFile=path username2#host2
cd path
put file
exit
It connects to the server number two but then it stops. The prefix sftp> does not appear and it does not read the following lines. Do you have any suggestion?
The remote shell takes the commands and executes them one by one. So it executes the sftp, waits for it to exit (what it never does) and only then it would execute the cd command (but in shell, not in the sftp), the put (failing as that's not a shell command), etc.
If your intention was to simulate typing the commands as on a terminal, use Plink and input redirection.
The Plink (PuTTY command-line connection tool) is a tool from PuTTY package that works like PuTTY, but it is a console, not GUI, application. As such it can use input/output redirection. And anyway, the Plink is the tool to automate tasks, not PuTTY.
plink.exe -ssh host1 -l username1 -pw password1 < script.txt
For more details, see How to type commands in PuTTY by creating batch file? on Super User.

How to use make scp prompt for password when executed with Plink

I want to write a batch script which will run scp command in remote server, using Plink from my Windows.
The code is:
plink.exe -ssh 10.168.5.15 -l username -pw passwd scp abc.txt xuser#10.168.10.108:/home
What I'm expecting is a prompt for password for the remote machine to where I'm coping file in my batch window. When I run above code my batch window shows, "Permission Denied, please try again" three times and "lost connection" message. I also tried scp with PasswordAuthentication=yes and BatchMode=yes but no use.
And I'm sure that Plink is interactive.
Any suggestions please?
The PLink runs the command (scp) in a non-interactive session by default. The scp won't even prompt for password in the non-interactive session for security reasons.
Use the -t switch to force the interactive session.
plink.exe -t -ssh 10.168.5.15 ...
Quoting section 3.8.3.12 of PuTTY/Plink manual:
3.8.3.12 -t and -T: control pseudo-terminal allocation
The -t option ensures PuTTY attempts to allocate a pseudo-terminal at the server, and -T stops it from allocating one. These options are only meaningful if you are using SSH.
These options are equivalent to the ‘Don't allocate a pseudo-terminal’ checkbox in the SSH panel of the PuTTY configuration box (see section 4.24.1).

windows, Putty, pscp combined in to a single batch file, is it possible?

I'm having trouble getting an answer to my question, in laymens terms. It is probably my lack of knowledge on the subject so, I'm dumbing down the question. I have a windows machine that I run the putty tool from and connect to a linux box. I run " killall /bob/bin/myfile.out " then close putty then type in a cmd prompt pscp.exe myfilet.out.2.3.4 root#192.168.1.1:/bob/bin/myfile.out . Can someone show me how to combine these into a single windows batch file? thank you
You could use the free command line tool Plink to run commands on external servers via SSH.
#echo off
Plink root#192.168.1.1 "killall /bob/bin/myfile.out"
pscp.exe myfilet.out.2.3.4 root#192.168.1.1:/bob/bin/myfile.out || echo an error occurred when copying the file.
the command after || on the second line will only run if an error level is set by the previous command.
I can't add comments yet, but can you elaborate on how you login with putty, but not do the exact same thing with plink? Plink not only accepts all the same options as putty, but if you have a saved session in putty, you can access it from plink. Without any subcommands, plink should essentially make you CMD shell look like a crude putty window, with subcommands, it will execute them and return:
C:\Users\riglerjo>plink savedputtysession
Using username "rigler".
# hostname
s9-chicago.accountservergroup.com
-bash-3.2$ exit
logout
Run the remote command as an option on plink:
C:\Users\riglerjo>plink savedputtysession hostname
s9-chicago.accountservergroup.com

How to send password using sftp batch file

I'm trying to download a file from sftp site using batch script. I'm getting the following error:
Permission denied (publickey,password,keyboard-interactive).
Couldn't read packet: Connection reset by peer
When running the command:
sftp -b /home/batchfile.sftp <user>#<server ip>:<folder>
the batchfile.sftp includes these data:
password
lcd [local folder]
cd [sftp server folder]
get *
bye
Note: It's working when running at the prompt as
sftp <user>#<server ip>:<folder>
But I need the ability to enter the password automatically.
You'll want to install the sshpass program. Then:
sshpass -p YOUR_PASSWORD sftp -oBatchMode=no -b YOUR_COMMAND_FILE_PATH USER#HOST
Obviously, it's better to setup public key authentication. Only use this if that's impossible to do, for whatever reason.
If you are generating a heap of commands to be run, then call that script from a terminal, you can try the following.
sftp login#host < /path/to/command/list
You will then be asked to enter your password (as per normal) however all the commands in the script run after that.
This is clearly not a completely automated option that can be used in a cron job, but it can be used from a terminal.
I advise you to run sftp with -v option. It becomes much easier to fathom what is happening.
The manual clearly states:
The final usage format allows for automated sessions using the -b
option.
In such cases, it is necessary to configure non-interactive authentication
to obviate the need to enter a password at connection time (see
sshd(8) and ssh-keygen(1) for details).
In other words you have to establish a publickey authentication. Then you'll be able to run a batch script.
P.S.
It is wrong to put your password in your batch file.
You mention batch files, am I correct then assuming that you're talking about a Windows system? If so you cannot use sshpass, and you will have to switch to a different option.
Two of such options, that follow diametrically opposite philosophies are:
psftp: command-line tool that you can call from within your batch scripts; psftp is part of the PuTTY package and you can find it here http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html
Syncplify.me FTP Script: a scriptable FTP/S and SFTP client for Windows that allows you to store your password in encrypted "profile files"; check it out here http://www.syncplify.me/products/ftp-script/
Either way, switching from password to PKI authentication is strongly recommended.
PSFTP -b path/file_name.sftp user#IP_server -hostkey 1e:52:b1... -pw password
the file content is:
lcd "path_file for send"
cd path_destination
mput file_name_to_send
quit
to have the hostkey run:
psftp user#IP_SERVER
You need to use the command pscp and forcing it to pass through sftp protocol. pscp is automatically installed when you install PuttY, a software to connect to a linux server through ssh.
When you have your pscp command here is the command line:
pscp -sftp -pw <yourPassword> "<pathToYourFile(s)>" <username>#<serverIP>:<PathInTheServerFromTheHomeDirectory>
These parameters (-sftp and -pw) are only available with pscp and not scp. You can also add -r if you want to upload everything in a folder in a recursive way.
This command will help you
sshpass -p MYPASSWORD sftp MYUSERNAME#HOST

Resources