I need to send numbers to a text file located on a remote windows machine , I'm using plink (putty command line-I have installed a ssh server on the windows machine) to establish the connection and it's successful but I'm having problems when I want to write numbers :
#echo off
cmd /c C:\Users\rob\Desktop\plink.exe -l user -pw password -ssh IP-remoteserver echo 10>> C:\Users\ser\Desktop\test.txt
EXIT
C:\Users\ser\Desktop\test.txt--- the file where I want to save the results.
Any ideas of how can I solve the problem? , not sure how can I use the echo function ,I just one to run this command on the remote server:
echo 10>> C:\Users\ser\Desktop\test.txt
But not sure how to add this part to the command where I run plink.exe
Any help will be appreciated.
Thanks and regards.
According to these instructions:
http://the.earth.li/~sgtatham/putty/0.60/htmldoc/Chapter7.html
you can set up a saved session, then call plink with the session ID followed by your chosen command.
Related
so far what I have is
plink -batch root#1.1.1.1 -pw password COMMAND & plink -batch root#1.1.1.1 -pw password COMMAND
the problem is after the first command starts the batch waits for a response from the server to stop the session and go to the next command, I cant seem to find a way to get around this, I'm looking to have it send the command and immediately start the next command on the next server
You can have the server itself execute the command in the background. So you can use the same techniques as with OpenSSH ssh, as you will be using server-side functionality, unrelated to the local SSH client you are using:
Getting ssh to execute a command in the background on target machine
So something like this:
plink -batch root#1.1.1.1 -pw password "nohup COMMAND > foo.out 2> foo.err < /dev/null &"
Or use local techniques to execute the command asynchronously:
Running Windows batch file commands asynchronously
So something like this:
start "title" /b plink -batch root#1.1.1.1 -pw password COMMAND
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.
I'm having some trouble and thought maybe someone could help me. This is what I have so far:
call date2.cmd
md %DT_MM%-%DT_DD%-%DT_YYYY%
cd %DT_MM%-%DT_DD%-%DT_YYYY%
psftp servername.com -P port -l username -pw pass -b script to run while in server
So, I run date2 which gives me the correct date output it starts psftp but stops there. I want to do an mget to grab all the files on the server and place them into the folder that I just created. If anyone can help steer me in the right direction that would be appreciated. I am sure I forgot some details, please let me know if there is any more information required.
(Untested)
Make a script called commands.txt and change your psftp line to actually use it:
psftp servername.com -P port -l username -pw pass -b commands.txt
Then put some test commands in the script and see what happens:
pwd
quit
or maybe
mget *
quit
I would recommend you use Putty plink.exe . I think it's much easier and it is scriptable and is able to handle or ignore prompting during the SSH session. Would also work on linux.
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
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