[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 754
  • Last Modified:

Silmulate keystrokes in bash script

Hi experts,

I was wondering if someone could help me out with a bash script.

I have script that simply does an scp

scp *.js root@192.168.0.229:/

Is there a way to add the password info and enter key to the bash script so I don't have enter it everytime I run the script?

Thanks!
0
DanRaposo
Asked:
DanRaposo
1 Solution
 
ravenplCommented:
First of all try http://www.google.com/search?q=ssh+without+password to omit password
openssh tries as hard as it can to avoid reading password from non terminal input.
So You could try expect script instead ...
0
 
slyongCommented:
just providing the link for expect http://expect.nist.gov/
0
 
root_startCommented:
Hi DanRaposo,

I was checking in my linux box and as "scp" does not provide an option to put the password, I can suggest you to use: "expect".

I created a very simple script that you can use, check below:
===========================================================================
#!/usr/bin/expect -f

set timeout -1
spawn scp test_file.txt root@localhost:/
match_max 100000
expect -exact "root@localhost's password: "
send -- "passxxword\r"
expect eof
===========================================================================

Above I put my script, and for yours would be something like:
===========================================================================
#!/usr/bin/expect -f

set timeout -1
spawn scp *.js root@192.168.0.229:/
match_max 100000
expect -exact "root@192.168.0.229's password: "
send -- "passxxword\r"
expect eof
===========================================================================

The scripts above are really simple and will not handle if you have a problem during the execution of "scp" like the "problem" below:
===========================================================================
The autenticity of host 'Hostname or Ip(ip address)' can't be established.
RSA Key fingerprint is xxxxxxxxxxxxxx.
Are you sure you want to continue connecting (yes/no)?
===========================================================================

You can change the script to whatever you need and if you are not familiar with "expect", the "autoexpect" command can help you, just: "man autoexpect".

I hope it helps you. =0)
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now