How to auto input the password when run vncserver :1? - bash

I'm writing a bash script that will connect to my remote machine then run some commands, one of them is vncserver :1, but this command need to input a password. How can I do it in my shell script? (I just need to run the script only, don't need to input the password)
This is my script:
ssh -i $pem -o StrictHostKeyChecking=no -o 'IdentitiesOnly yes' admin#$ip -f '
pkill vnc ;
vncserver :1 ;
'

Thanks all,
It's ok now:
ssh -i $pem -o StrictHostKeyChecking=no -o 'IdentitiesOnly yes' admin#$ip -f '
pkill vnc ;
expect -c "
spawn vncserver :1;
expect -nocase \"password:\" {
send \"$pass\r\";
expect -nocase \"Verify:\" {
send \"$pass\r\";
expect -nocase \"Would you like to enter a view-only password \(y\/n\)\?\" {
send \"n\r\";
expect eof }; }; interact } ;
"
'

Related

Shell/Bash + expect script based on input list

I'm trying to run a shell script that execute an expect script based on an input list (IP Addresses), but in parallel.
Unfortunately I didn't understood how to achieve it, below my working script that execute the expect script sequentially on each IP contained in the list.
#!/bin/bash
HOST="$1"
USER="user"
PASS="password"
OUT="checkversion.csv"
rm -rf $OUT
touch $OUT
NOW=$(date +"%Y-%m-%d_%H-%M-%S")
echo "Script started at $NOW"
for i in `cat $1`
do
SCRIPT=$(expect -c "
spawn ssh -o HostKeyAlgorithms=+ssh-dss -o KexAlgorithms=+diffie-hellman-group1-sha1 -o StrictHostKeyChecking=no -c 3des-cbc -c aes128-cbc -c aes192-cbc -c aes256-cbc -c aes128-ctr -c aes192-ctr -c aes256-ctr $USER#$i
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
expect \"*>*\"
send -- \"start shell\"
send -- \"\r\"
expect \"*%*\"
send -- \"exit\"
send -- \"\r\"
expect \"*>*\"
send -- \"exit\"
send -- \"\r\"
")
VERSION=`echo "$SCRIPT" | grep JUNOS | grep -v WARNING | grep -v possible | grep -v recover`
echo "$i;$VERSION" >> $OUT
done
NOW=$(date +"%Y-%m-%d_%H-%M-%S")
echo "script finished at $NOW - note any errors above"
Could you please help me in found a method to run it in parallel and speedup the process?
Thanks
Please try changing your script as follow:
#!/bin/bash
HOST="$1"
USER="user"
PASS="password"
OUT="checkversion.csv"
rm -rf $OUT
touch $OUT
NOW=$(date +"%Y-%m-%d_%H-%M-%S")
echo "Script started at $NOW"
function runSSH {
PARAM=$1
SCRIPT=$(expect -c "
spawn ssh -o HostKeyAlgorithms=+ssh-dss -o KexAlgorithms=+diffie-hellman-group1-sha1 -o StrictHostKeyChecking=no -c 3des-cbc -c aes128-cbc -c aes192-cbc -c aes256-cbc -c aes128-ctr -c aes192-ctr -c aes256-ctr $USER#$PARAM
match_max 100000
expect \"*?assword:*\"
send -- \"$PASS\r\"
expect \"*>*\"
send -- \"start shell\"
send -- \"\r\"
expect \"*%*\"
send -- \"exit\"
send -- \"\r\"
expect \"*>*\"
send -- \"exit\"
send -- \"\r\"
")
VERSION=`echo "$SCRIPT" | grep JUNOS | grep -v WARNING | grep -v possible | grep -v recover`
echo "$PARAM;$VERSION" >> $OUT
}
for i in `cat $1`; do
# Run SSH in background
runSSH $i &
done
# Wait for background processes to finish...
wait
NOW=$(date +"%Y-%m-%d_%H-%M-%S")
echo "script finished at $NOW - note any errors above"

sftp bash script doesnt downloads files

I have a problem with my bash script.
It connects to my sftp server.
Gets the list of files to download.
Should download the files. But it doesnt do this. I can see the commands. if I write the commands manually, it works.
You can see the script and the log files here:
#!/bin/bash
LOCALDIR="/data/IMPORT/$(date +%Y%m%d)"
REMOTEDIR="/EXPORT"
FILELIST="$LOCALDIR/filelist.txt"
FILELIST2="$LOCALDIR/filelist2.txt"
SFTP="sftp -P 1234 -i /var/xxxxxx.pem -oStrictHostKeyChecking=no user#xxxxx.xxxx"
PASSPHRASE="xxxxxxxxxxxxxxxx"
mkdir -p $LOCALDIR
rm $FILELIST
rm $FILELIST2
allfilenames=()
function readFileList {
expect -c "
spawn $SFTP
expect \"assphrase\"
send \"$PASSPHRASE\r\"
expect \"sftp>\"
send \"lcd $LOCALDIR\r\"
send \"ls -l $REMOTEDIR/*\r\"
expect \"sftp>\"
send \"exit\r\"
interact " > $FILELIST
}
function getFiles {
myfilenames=("$#")
expect -c "
spawn $SFTP
expect \"assphrase\"
send \"$PASSPHRASE\r\"
expect \"sftp>\"
send \"lcd $LOCALDIR\r\"
expect \"sftp>\"
send \"cd $REMOTEDIR\r\"
expect \"sftp>\"
" >> $FILELIST2
for filepath in "${myfilenames[#]}"
do
file="$(basename -- $filepath)"
expect -c "
send \"get -P $file\r\n\"
sleep 3
expect \"sftp>\"
" >> $FILELIST2
done
expect -c "
send \"exit\r\"
" >> $FILELIST2
}
readFileList
c=0
if [[ -f "$FILELIST" ]]; then
while read line; do
filename=$(echo $line | awk '{ print $9 }')
if [[ "$filename" =~ ^$REMOTEDIR ]] ; then
allfilenames+=($filename)
fi
done < $FILELIST
fi
getFiles "${allfilenames[#]}"
filelist.txt looks like:
spawn sftp -P 1234 -i /var/xxxxxx.pem -oStrictHostKeyChecking=no user#xxxxx.xxxx
Enter passphrase for key '/var/xxxxxx.pem':
Connected to xxxxx.xxxx.
sftp> lcd /data/IMPORT/20200401
sftp> ls -l /EXPORT/*
-rw-r--r-- 0 1000472 1000472 3681 Mar 31 22:31 /EXPORT/file1.txt
-rw-r--r-- 0 1000472 1000472 14537 Mar 31 22:34 /EXPORT/file2.txt
-rw-r--r-- 0 1000472 1000472 5932 Mar 31 22:34 /EXPORT/file3.txt
sftp> exit
filelist2.txt looks like:
spawn sftp -P 1234 -i /var/xxxxxx.pem -oStrictHostKeyChecking=no user#xxxxx.xxxx
Enter passphrase for key '/var/xxxxxx.pem':
Connected to xxxxx.xxxx.
sftp> lcd /data/IMPORT/20200401
sftp> cd /EXPORT
sftp> get -P file1.txt
get -P file2.txt
get -P file3.txt
exit
expect -c 'spawn sftp ...'
expect -c 'send "get file\r" '
Here when the first expect -c completes, the SFTP connection will be closed so the second expect -c would not work. You have to use one single expect -c for one SFTP session.
It's like when you manually sftp to the server, you cannot temporarily go back to Bash and come back to sftp later.

Expect deletes ^H after spawn bash -c

I have an expect script that grabs a bunch of configs and outputs them to separate files. The files though have a bunch of ^H's though I'd like to clean out. I used the CtrlV+CtrlH to put it in the expect script, but when it runs it just does it. The sed works fine in bash, but fails in expect. How can I delete the ^H from the files?
foreach host $ip {
set output [ open "$host" w ]
set timeout 2
spawn ssh -oKexAlgorithms=+diffie-hellman-group1-sha1 -o "StrictHostKeyChecking no" -oHostKeyAlgorithms=+ssh-dss $username#$host
expect {
eof {wait; spawn telnet $host;
expect "ame:";
send "$username\r"}
}
expect "word:"
send "$password\r"
expect "#"
send "$command\r"
sleep 2
expect {
"ore--" { send -- " "; puts $output $expect_out(buffer); exp_continue}
"#" {send -- "exit\r"}
}
puts $output $expect_out(buffer)
close $output
close
}
foreach host $ip {
spawn bash -c "sed -i 's/^H//gi; s/--More--//g' ./$host"
}
close
Here's what it looks like when it runs:
spawn bash -c sed -i 's//gi; s/--More--//g' ./192.168.50.1
spawn bash -c sed -i 's//gi; s/--More--//g' ./192.168.51.1
spawn bash -c sed -i 's//gi; s/--More--//g' ./192.168.52.1
Assuming an ASCII-based locale, this should work:
tr -d '\010' < file > file.new
\010 is octal 8, which is the ASCII equivalent of backspace.
For calling out to sed, since you don't need to interact with the process, you can use exec instead:
foreach host $ip {
exec sed -i {s/^H//gi; s/--More--//g} ./$host
}
Using Tcl's version of single quotes, which are {}

creating username and set password remotely

I am running script which generated strong password and takes input as user name and calls
remote script to create username and password .
script goes like this
USERNAME=$1
PASS=`cat /dev/urandom|tr -dc 'a-zA-Z0-9-!##%()+{}$|:?='|fold -w 10 | head -n 1| grep -i '[!##%()+{}|:$?=]'`
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh $USER $PASS "
It works fine ,if the password does not contain any extra characte like | , & and $ .
e.g.
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh testuser1 12345 "
it fails with strong password as follows .
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh testuser1 v|9q4TT8={ "
Is there any workaround for this .
Regards
Use enclosing " " to use strong password, bash will treat any character between " " as String.
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh \"$USER\" \"$PASS\" "
And don't forget to escape inner quotes. i.e. add like \"

set .htpasswd with bash expect

I'm trying to automate setting a user's password in .htpasswd for Apache, but this script doesn't seem to actually set the password. What am I missing? Thanks!
#!/bin/bash
PASSWORD=`tr -dc a-z0-9_ < /dev/urandom | head -c 10`
cat << EOF | /usr/bin/expect
spawn /usr/bin/htpasswd -c /var/.htpasswd testuser
expect "assword:"
send "$PASSWORD\r"
expect "assword:"
send "$PASSWORD\r"
EOF
echo -e "\nPassword set to: $PASSWORD\n"
You can specify a password on the command line using the -b flag.
#!/bin/bash
PASSWORD=`tr -dc a-z0-9_ < /dev/urandom | head -c 10`
htpasswd -cb /var/.htpasswd testuser $PASSWORD
echo Password set to: $PASSWORD
# output:
# Adding password for user testuser
# Password set to: gzu00n4lp8
For clarification, here's a version using expect:
#!/bin/bash
PASSWORD=`tr -dc a-z0-9_ < /dev/urandom | head -c 10`
expect << EOF
spawn htpasswd -c /var/.htpasswd testuser
expect {
"New password:" { send "$PASSWORD\r"; exp_continue }
"Re-type new password:" { send "$PASSWORD\r"; exp_continue }
}
EOF
echo Password set to: $PASSWORD

Resources