Reputation: 73
#!/usr/bin/expect -f
set login "root"
set addr "10.10.10.10"
set pw "123!"
set localDir "/home/user/www/*"
set servDir "/var/www/html/"
spawn scp -r $localDir $login@$addr:$servDir
expect "connecting (yes/no)?"
send "yes\r"
expect "$login@$addr\'s password:"
send "$pw\r"
interact
I need to copy all the files in /home/user/www/ to /var/www/html/ but when I run the SCP command in SPAWN using the directory string /home/user/www/* it gives an error.
Running it as /home/user/www/ doesn't but it copies the www folder straight off which is not what I need to happen.
Upvotes: 0
Views: 6886
Reputation: 486
OPTION A:
The best way is to set up password-less log-in. It is not a good idea to throw your password naked on the network. Once the auth keys are set up, you can use scp
without password prompts,or even in a crontab
.
First log in on Sys_A as user a and generate a pair of authentication keys. Do not enter a passphrase:
a@Sys_A:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa):
Created directory '/home/a/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/a/.ssh/id_rsa.
Your public key has been saved in /home/a/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 a@A
Now use ssh to create a directory ~/.ssh as user b on Sys_B. (The directory may already exist, which is fine):
a@Sys_A:~> ssh b@B mkdir -p .ssh
b@Sys_B's password:
Finally append a's new public key to b@Sys_B:.ssh/authorized_keys and enter b's password one last time:
a@Sys_A:~> cat .ssh/id_rsa.pub | ssh b@B 'cat >> .ssh/authorized_keys'
b@Sys_B's password:
From now on you can log into Sys_B as b from Sys_A as a without password:
a@Sys_A:~> ssh b@Sys_B
OPTION B
Use expect
.
#!/usr/bin/expect
eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com
#use correct prompt
set prompt ":|#|\\\$"
interact -o -nobuffer -re $prompt return
send "my_password\r"
interact -o -nobuffer -re $prompt return
send "my_command1\r"
interact -o -nobuffer -re $prompt return
send "my_command2\r"
interact
Sample solution for bash could be:
#!/bin/bash
/usr/bin/expect -c 'expect "\n" { eval spawn ssh -oStrictHostKeyChecking=no -oCheckHostIP=no usr@$myhost.example.com; interact }'
This will wait for enter and than return (for a moment) interactive session.
Please see this SO question
Upvotes: 1