I have created an .bat file with the following command with a text file which contain "sudo su - oradev2"(C.txt).
cd C:\Users\chakraborty_sayantan\Desktop
plink -ssh serverA -l username -pw password -m "C:\Users\username\Desktop\c.txt" -t
The command above completes the first step of logging into a DB and the c.txt file has the instruction to enter into sudo mode. However, post this there is an authentication. Is there a way to automate the step of entering the password? Any thoughts?
Procedure to automate
login
password
sudo su - oradev
password
echo $ORACLE_SID
sqlplus / as sysdba
create user identified by
default tablspace
The above steps need to get automate using a bat file which consist of plink/putty.
-Sayantan
You can make a password and username txt file and maybe use the IF NOT EXIST Command to check if that user and pass file exists and set some commands up to create that file
Hope this is what you meant :)
Related
I am trying to write a batch file to automate a PuTTY script. Right now the script successfully logs into the SSH server with this line
putty.exe -ssh user#ipaddress -pw password -m commands.txt > log.txt
My problem is that once it's logged in the server prompts me to hit "Ctrl+y to begin" afterwards it asks me to press "Enter". How would I imitate that in my commands.txt file?
Use Plink (PuTTY command-line tool).
It is a console application, hence it allows input redirection.
Create a text file that contains Ctrl+Y character followed by Enter character. I.e. 0x19 and 0x0A. And then follow with your commands.
Then you can do
plink -ssh user#ipaddress -pw password < input.txt
I have followed this question to build a batch file to run the PuTTYwith my username and password:
How to run a command file in PuTTY using automatic login in a command prompt?
#echo off
START putty.exe -ssh [domain] -l [username] -pw [password] -m code.txt
#echo
And the PuTTY will try to run the code.txt file, which have the following code:
HResults -p -e "???" sil -e "???" sp -L labels/test lib/words3 results/*.rec
read
It will show a matrix. I try to run the batch file, it is able to open PuTTY, login and run the command in text file. But the output in PuTTY terminal is a mess. The layout of output is fine, when I doing those things manually. Is that mean some kind of setting is missing? It's not making any sense a batch file will change the output of another application...... Thanks
The -m switch imply a non-interactive session. While, when logging in manually, an interactive mode is used by default.
It may fundamentally affect output of some applications.
Try forcing the interactive mode using the -t switch:
START putty.exe -ssh [domain] -t -l [username] -pw [password] -m code.txt
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 want to copy some file to a remote Linux system from my Windows PC using pscp (from putty). I wrote a small script that call the pscp commands in this way:
"C:\Users\hp\Desktop\pscp.exe" -scp C:\Users\hp\Desktop\scripts\* root#192.168.1.177:/root/scripts
"C:\Users\hp\Desktop\pscp.exe" -scp C:\Users\hp\Desktop\scripts2\* root#192.168.1.177:/root/scripts2
pause
But when I launch this bat script, I'm asked to input the password, so I input the password manually.
Is there a way to input the password automatically through the batch file?
pscp -pw yourPasswordHere C:\Users\testUser\Downloads\test.sh testUser#123.123.123.123:/home/testUser
I've noticed that no one posted sample of that command with -pw option.
From putty documentation
5.2.2.6 -pw passw login with specified password
If a password is required to connect to the host, PSCP will
interactively prompt you for it. However, this may not always be
appropriate. If you are running PSCP as part of some automated job, it
will not be possible to enter a password by hand. The -pw option to
PSCP lets you specify the password to use on the command line.
You can work this stuff out for yourself. Use the /? option to see the help. The pertinent parts are included below.
>pscp /?
PuTTY Secure Copy client
Release 0.60
Usage: pscp [options] [user#]host:source target
pscp [options] source [source...] [user#]host:target
pscp [options] -ls [user#]host:filespec
Options:
......
-l user connect with specified username
-pw passw login with specified password
......
Use the -l and -pw options to specify a user and password.
So whilst that answers the specific question that you asked, the real opportunity for you is to learn how to obtain documentation from command line utilities.
Use putty to set up a remote login without a password. This will involve the puttygen command. You will need a private key and the target will need a public key. Once you verified that you have a good private key file and login without typing a password, you can reference it with the "-i" private key option on pscp. You might be able to set up the key without the putty connect if it isn't allowed, but this is the easiest way. Just say no to passwords in the clear:
C:\Users\riglerjo>pscp -i .ssh\rigler_rsa.ppk test.txt rigler#rigler.org:.
test.txt | 0 kB | 0.0 kB/s | ETA: 00:00:00 | 100%
If your coping a local file to remote directory use:
pscp -pw yourPasswordHere C:\Users\testUser\Downloads\test.sh testUser#10.10.10.10:/home/testUser
If your coping a remote file to local directory use:
pscp -pr yourPasswordHere testUser#10.10.10.10:/home/testUser/test.sh C:\Users\testUser\Downloads\
sshpass -p'password' pscp -A -H "ip" -t 20 -l root /var/www/html/temp_santosh.txt /tmp/
You can use sshpass with -p option just before pscp
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