To read the output of a command in a shell script - shell

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.

Related

Is there a way to include the input in the bash file so that I will not type the input required in the command line every time I run the bash script

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.

What is best way to store passwords for shell script?

I have shell script (example.sh) in Ubuntu 16.04, what I will excecute with cronjob daily to backup databases and archive it in 7z arhive with password.
So I need somehow pass passwords to this script.
Example:
#!/bin/sh
mysql_user="root"
mysql_pass="example"
zip_pass="example2"
#there goes logic for backup
...
Issues
As far as I know, just store passwords in shell script is not correct and unsecure.
I tried to pass this as arguments to shell script, for example sh example.sh myStrongMySQLPassword 7zPassword, but thoose all users can see just using command ps -ef in terminal.
So, what is the most secure and best option to run shell scripts with passwords?
You can use a separate config file and store credentials in this. Read this config file in your shell script to fetch the credentials.
There are some very good articles on how to do this on unix stack exchange. Refer to it for detailed info:
hiding-password-in-shell-scripts

How to extract information from a command prompt using shell script?

I need a shell script using which I can fetch data from command prompt. I need to fetch data from the command prompt of a router. When I write commands in a shell script it goes the prompt but not executing the next command. So running the script just stuck in the prompt. Bellow is my script file
#!/bin/sh
ccli
rsc
where ccli is the command to enter the prompt and rsc is the command to fetch some infomation.
So please suggest some method.
If ccli reads commands from stdin (which I don't know), you might get further with
printf 'rsc\n' | ccli
For more complicated tasks I suggest you look into expect which was invented for the sole reason of driving interactive programs in a scripted way.

how to ftp remote server through running a shell script

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

While Using PLink Only First Command Is Executing

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 :)

Resources