I want to connect putty using plink and want to run some specific commands which is present in text file. But I can see only first command getting executed. Do I need to give any command separating literal?
I have this problem too. Solutions that I used.
Sending one line command with && or &
If script is big, send it with pscp and run on remote host with single command/
I beleive there is the third way. Just execute script under -m option :)
Related
I am utilizing bash scripts to perform auto deployment on live site with Ubuntu server.
One of the line has something like:
scp build.zip user_name#ip_address:/path/to/releases/$release
Once the Ubuntu execute this command, it will ask me for password input in the command line like:
Enter passphrase for key '/home/user_name/.ssh/id_rsa':
Is there a way to include the input in the bash file so that I will not type the password in the command line every time I run the bash script?
You could use another file in which you write the needed input lines.
./your_script.sh < your_input_file.txt
But that really don't seem like the best idea, I'd rather try to prevent the need for input in each of the sub-commands.
You can permanently remove the passphrase on your id_rsa key by running this command and setting a new blank passphrase:
ssh-keygen -p
For a generic solution, you can use an expect script to programmatically drive and interact with a terminal session.
I have to build one application which on button click starts passing command with putty.exe how can it be done with process.start ?
Process.start(#"C:\putty.exe")
ProcessStartInfo startinfo = new ProcessStartInfo();
startInfo.FileName=#"C:\putty.exe"
startInfo.Arguments = "some load session";
this is my current code but i want to push certain script and commands to putty terminal as well
It looks like PuTTY's command line support is rather minimal, but the -m option may work:
From http://the.earth.li/~sgtatham/putty/0.67/htmldoc/Chapter3.html#using-cmdline
3.8.3.6 -m: read a remote command or script from a file
The -m option performs a similar function to the ‘Remote command’ box in the SSH panel of the PuTTY configuration box (see section 4.18.1). However, the -m option expects to be given a local file name, and it will read a command from that file.
With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.
You'll have to put your command(s) in a file before and pass that to PuTTY, but for simple tasks, it could work.
As mentioned in a comment on the question, Plink sounds much more amenable to what you're trying to do, as it (appears to) support a fully interactive session via the StandardInput and StandardOutput properties on the object you'll get back from Process.Start().
This question already has answers here:
WinSCP script not executing in batch file
(2 answers)
Closed last year.
I am trying to run multiple commands in one line. The codes are below.
WinSCP.com
open user:pw#address
lcd C:\Users\xx\Desktop
get *.xlsx
exit
If I run them one by one, it will work. However, if I want to run them all together using one line, it will fail, no matter I am using ; or &.
For example, if I run WinSCP.com & open user:pw#address, only the first part will be executed.
WinSCP.com; open user:pw#address doesn't work either.
How to execute them using one line?
Thanks
This is happening, because both command line and winscp.com are programs. When you're entering commands one by one you start winscp.com and next entered commands are going directly to the WinSCP. When you're trying to execute all commands at once it is starting WinSCP and waiting for it to finish and then executing next commands.
You definitely can create a text file with your commands and save it as script.txt
open user:pw#address
lcd C:\Users\xx\Desktop
get *.xlsx
exit
And then pass it to the winscp.com application like this:
winscp.com /script=script.txt
It may be possible to send all the commands at once by using /command cmd1 cmd2 ... where cmd1,2 are your commands, but it might be tricky.
References
Command-line Options :: WinSCP
Parameters
You can put the sequence of commands in a script file used by WinSCP and call it using the command sequence:
WinSCP.com /script="C:\Users\{name}\Documents\Scripts\{SomeFileName}.scp"
I have found it easier to keep track of what you are doing and keeps the command sequence easier to read.
These are all the possible ways of running multiple commands on a single line :
When you use command1 & command2 on the command prompt, it
executes the first command, and then the second command.
command1 && command2 it runs the first command, and then runs the second command only if the first command completed successfully.
command1 || command2 it runs the first command, and then runs the second command only if the first command did not complete successfully (receives an error code greater than zero).
(command1 & command2) used to group or nest multiple commands.
Since in your case, you want to run the first command and then do the other operation with-in it; it isn't possible to use any of the above.
You have to write a script which you may call in the same line using a command.
You may do something like this : (WinSCP.com /script=<script_path>) && exit
There are many more options in WinSCP.com command.
ftp ipaddress
bin
hash
cd path
get filename
quit
i want these line to be executed in a shell script only first line is executing after entering username and password the rest line are not executing and the control is stuck at ftp> prompt
Once your script invokes the program ftp, the shell loses control until the ftp program finishes. So you need to use some new technique. One way is with the program expect, which you can get a hint about here: https://stackoverflow.com/a/12598169/4323 . Another way is to use a more "scriptable" FTP client, such as lftp on Linux, which has specific features to enable the sort of scripted use you're going for.
You need to use a non-interactive FTP client if you want to do this in a script rather than an interactive shell. ncftp is one you could check out.
Or you can use a shell "here" document:
#!/bin/bash
ftp -in <<EOS
user pswd
bin
hash
cd path
get filename
quit
EOS
The here document sends everything via the open program's stdin, just as if you typed it in at the command line. Note that ftp clients can be finicky and each one seems to have it's own set of gotchas, so some experimentation and use of man ftp will likely be required.
Looks like this has come up before on Stackoverflow, you might want to read through
how to ftp multiple file using shell script
IHTH
I am running a unix command say ftp to a remote machine through a shell script.I have to pass the userid and password through the script accordingly.How can i do that?
Best way to supply username/passwords from scripts would be to use 'expect' scripting.
Here is a small example : http://www.linuxquestions.org/questions/linux-software-2/auto-ssh-login-expect-script-624047/
. It shows using expect to provide password for ssh, ftp should be very similar.
Redirect the ftp command inside your shell script
ftp ..... > ftp.log
Do you mean you want to get the result that the command (say, ftp) wrote to the terminal as a value inside the script?
Use backticks. For example:
x=`ftp ...`
will run the ftp command, and take the text of its stdout and store it in the variable x.