I have this basic script file.sh
that connects to a server
name=username
routerip=172.21.200.37
echo "$name""@""$routerip"
ssh "$name""@""$routerip"
sample output:
$ ./file.sh
[email protected]
[email protected]'s password:
What I am wondering is how to best handle the password that is requested in my script. Should I use expect
? Or is there another way. Also if the password is in the script should it be encrypted for security?
And maybe a silly question but, is there a way to connect to the server with haveing a username?
If you care about security, you definitely don't want to embed the password in the script. And you can't really "encrypt" the password, either, because the script would have to know how to decrypt it, and anyone reading the script would see how you did it (and what the decryption key was, etc.).
If you have a script running on machine A that is authorized to log in to machine B, the way I've always done it is to set up a private ssh key on machine A, and put the public key in the user's
authorized_keys
file on machine B.(In answer to your last question, no, there's no way to do this sort of login without a username.)