how to read username & password from file in shell script? - linux

I want to login to remote computer using shell script through ssh, i want to pass username and password from text file. how to do that?
here is code what i have tried.
#!/bin/sh
#
username=user
ip=192.168.2.21
ssh $username#$ip < user.txt

You can't SSH in the method that you're referring to. You will want to use SSH keys which are created to help with what you're doing.
Also, avoid storing your password in a text file.

Related

Passing Password for scp through shell script

I have a shell script which contains following command to copy test.dat file from remote server to my current directory :
scp remote_user_id#remote_server_name:<filepath>/test.dat .
upon executing the script , It asks for password which I have to enter manually. I do not have access to install or use sshpass or expect or even setting up ssh. How can I pass this password in the shell script ?
Use public key authentication
-i identity_file
Selects the file from which the identity (private key) for public key authentication is read. This option is directly passed to ssh(1).
You could also use rsync. rsync has options like --password-file

Using a shell script to interact with programs

I need to upload a file via FTP with shell script.
Can I interact with FTP from the script? My Script is at http://pastebin.com/A76fsaM3
cd MyLocalUploadDir
ftp
open ftp.server.com
myusername
mypassword
put LocalIteam.TXT
Will this work? Is there another way to interact with FTP from within a shell script? Any help is appreciated!
See http://www.tldp.org/HOWTO/FTP-3.html for information regarding interaction with FTP.
You need to use a here-doc. Otherwise, the lines after ftp will not be used as input to the program.
cd MyLocalUploadDir
ftp <<EOF
open ftp.server.com
myusername
mypassword
put LocalIteam.TXT
quit
EOF
You can also avoid putting your username and password in the script by using a .netrc file.
And instead of using the ftp program, you could use curl to do it in one line.
curl --upload-file MyLocalUploadDir/LocalIteam.TXT 'ftp://myusername:mypassword#ftp.server.com/'
You can also use the --netrc option to curl to tell it to get the username and password from the .netrc file.

Shell script - SSH

I am using shell script to add some file to server. Is there any way to write a shell script that will execute one part on local computer and the other part when you're logged into that server?
For example, I want to log in, do something, add some file, and then I want to list everything on that server.
ssh something#something
I enter password
Then list files from server.
You can just add a command to end of the ssh command; for example:
ssh username#host ls
will run ls on the server, instead of giving you a login shell.

Enter Password through bourne shell script when asked by the command without expect

I am writing a bourne shell script for the openwrt firware where I want to copy my files from router to linux machine. when i do
scp /etc/clients.txt shah#192.168.1.2:/home/shah/
from inside the router's openwrt firmware, it asks me to give the password. I want to give this password from within the script because this file needs to be copied after every 2 seconds. How can I do this without using expect?
You can try to use sshpass tool but you will need to provide a password in clear text in your script. If that is not a problem you should use it.
sshpass -p 'password' scp /etc/clients.txt shah#192.168.1.2:/home/shah/

How to login to an ftp server using shell scripting in linux?

I want to login to my FTP server using shell scripting. I made a .sh file having contents as
ftp
open 172.31.1.45
but it's not working. The second command is not executing. Please help. I am new to linux so please forgive if this is a stupid question
try this:
#!/bin/sh
HOST="yourhost"
USER="user"
PASSWD="pass"
FILE="file.txt"
ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
If it's just a matter of simple file transfers, you might want to look at using a tool like ncFTP, wget, or cURL, which can do file transfers in a single command. Using these tools, you can simply pass the username, password, host address, source file, and destination file in one command.

Resources