页面“C基础”与“Expect基础”之间的差异

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
 
 
第1行: 第1行:
=c=
+
[[category:shell]]
 +
=Question=
 +
有个项目要迁移 新来一批机器 要做初始化 于是抽空练了一下expect
 +
  有可能有这个  警告 expect: spawn id exp4 not open 不过效果是没问题的
 +
 
 +
=利用expect批量添加pubkey=
 +
==多台添加pubkey ==
 
<pre>
 
<pre>
 +
cat main
 +
#!/bin/bash
 +
for ip  in `cat list`
 +
 +
do
 +
#echo $ip
 +
./addkey  $ip
 +
 +
done
 +
 +
 +
 +
cat addkey
 +
#!/usr/local/bin/expect
 +
#define var
 +
set timeout 17
 +
# #<==接受第一个参数,赋值host
 +
set host [lindex $argv 0]
 +
set password "evan=="
 +
 +
 +
#spawn
 +
spawn ssh-copy-id  -i /home/evan/lx/ssh/opspub root@$host
 +
#spawn ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub root@$host
 +
#ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub [email protected]
 +
 +
#expect
 +
expect {                 
 +
"*yes/no" { send "yes\r"; exp_continue} 
 +
"*password:" { send "$password\r" }     
 +
 +
#send  "exit\r" 
 +
expect eof
 +
 +
cat list
 +
192.168.7.4
 +
192.168.7.46
 +
 +
##上面用了copy-id  如果想用scp  etc
 +
 +
    #!/usr/bin/expect   
 +
    set timeout 5   
 +
    set hostno [lindex $argv 0]   
 +
    spawn scp ~/.ssh/id_dsa.pub impala$hostno:~/.ssh/pub_key   
 +
    expect "*password*"   
 +
    send "111111\r"   
 +
    spawn ssh impala$hostno "cat ~/.ssh/pub_key/ >> ~/.ssh/authorized_keys"   
 +
    expect "*password*"   
 +
    send "111111\r"   
 +
    spawn ssh impala$hostno "chmod 600 ~/.ssh/authorized_keys"   
 +
    expect "*password*"   
 +
    send "111111\r"   
 +
    expect eof   
 +
 +
(3)分析:
 +
set可以设置超时,或者设置一个变量的值
 +
spawn是执行一个命令
 +
expect等待一个匹配的输出流中的内容
 +
send是匹配到之后向输入流写入的内容
 +
[lindex $argv 0]表示脚本的第0个参数
 +
expect eof表示读取到文件结束符
  
/*                                                                                                                                                                                         
 
20答案是 Tuesday  因为列数定了是10                                                                                                                                                           
 
21days[2] === days[2][10]                                                                                                                                                                   
 
22                                                                                                                                                                                           
 
23解说                                                                                                                                                                                       
 
24days[2][0]=T                                                                                                                                                                               
 
25days[2][1]=u                                                                                                                                                                               
 
26days[2][2]=e                                                                                                                                                                               
 
27days[2][3]=s                                                                                                                                                                               
 
28days[2][4]=d                                                                                                                                                                               
 
29days[2][5]=a                                                                                                                                                                               
 
30days[2][6]=y                                                                                                                                                                               
 
31days[2][7]=                                                                                                                                                                               
 
32days[2][8]=                                                                                                                                                                               
 
33days[2][9]=                                                                                                                                                                               
 
34                                                                                                                                                                                           
 
35               
 
 
</pre>
 
</pre>
==eg ==
+
 
=== 石头剪刀布===
+
==多台添加ssh安全(远程执行命令和退出) ==
 
<pre>
 
<pre>
#include <stdio.h>
+
cat  run
#include <stdlib.h>
+
#!/usr/local/bin/expect
#include <time.h>
+
#define var
 +
set timeout 17
 +
set host [lindex $argv 0]
 +
set password "evan=="
  
int main(void)
+
#spawn
{
+
spawn ssh root@$host
char gesture[3][10] = { "scissor", "stone", "cloth" };
 
int man, computer, result, ret;
 
  
srand(time(NULL));
+
expect {
while (1) {
 
computer = rand() % 3;
 
  printf("\nInput your gesture (0-scissor 1-stone 2-cloth):\n");
 
ret = scanf("%d", &man);
 
  if (ret != 1 || man < 0 || man > 2) {
 
printf("Invalid input! Please input 0, 1 or 2.\n");
 
continue;
 
}
 
printf("Your gesture: %s\tComputer's gesture: %s\n",
 
gesture[man], gesture[computer]);
 
  
result = (man - computer + 4) % 3 - 1;
+
    "*#*" { send "sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config\r";
if (result > 0)
+
send "sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config\r"
printf("You win!\n");
+
send "systemctl restart sshd\r"
else if (result == 0)
+
#send "\r"
printf("Draw!\n");
+
    }
else
 
printf("You lose!\n");
 
}
 
return 0;
 
 
}
 
}
 +
send  "exit\r" 
 +
expect eof
  
 +
cat main
 +
#!/bin/bash
 +
for ip  in `cat list`
  
/*
+
do
0、1、2三个整数分别是剪刀石头布在程序中的内部表示,用户也要求输入0、1或2,然后和计算机随机生成的0、1或2比胜负。这个程序的主体是一个死循环,需要按Ctrl-C退出程序。以往我们写的程序都只有打印输出,在这个程序中我们第一次碰到处理用户输入的情况。在这里只是简单解释一下,以后再细讲。scanf("%d", &man)这个调用的功能是等待用户输入一个整数并回车,这个整数会被scanf函数保存在man这个整型变量里。如果用户输入合法(输入的确实是整数而不是字符串),则scanf函数返回1,表示成功读入一个数据。但即使用户输入的是整数,我们还需要进一步检查是不是在0~2的范围内,写程序时对用户输入要格外小心,用户有可能输入任何数据,他才不管游戏规则是什么。
+
#echo $ip
 +
#./addkey  $ip
 +
./run  $ip
  
和printf类似,scanf也可以用%c、%f、%s等转换说明。如果在传给scanf的第一个参数中用%d、%f或%c表示读入一个整数、浮点数或字符,则第二个参数的形式应该是&运算符加一个相应类型的变量名,表示读进来的数存到这个变量中;如果在第一个参数中用%s读入一个字符串,则第二个参数应该是数组名,数组名前面不加&,因为数组类型做右值时自动转换成指针类型,而scanf后面这个参数要的就是指针类型,在第 10 章 gdb有scanf读入字符串的例子。&运算符的作用也是得到一个指针类型,这个运算符以后再详细解释。
+
done
 +
 
 +
#run
 +
bash main
 +
</pre>
  
留给读者的思考问题是:(man - computer + 4) % 3 - 1这个神奇的表达式是如何比较出0、1、2这三个数字在“剪刀石头布”意义上的大小的
+
==单个==
*/
+
<pre>
  
</pre>
+
vi addkey
  
[https://wenku.baidu.com/view/55e520da5022aaea998f0f5e.html 石头剪刀布_C语言]
+
#!/usr/local/bin/expect
 +
#SERVERS="101.00.208.197 120.40.043.52 "
 +
set timeout 5 
 +
set host [lindex $argv 0]
 +
spawn ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub root@$host
 +
#ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub [email protected]
 +
expect {                 
 +
"*yes/no" { send "yes\r"; exp_continue} 
 +
"*password:" { send "P2xMGipLpSG7dA==\r" }     
 +
  
https://blog.csdn.net/guoqingchun/article/details/8104197
+
expect eof
  
=排序与查找=
 
==插入排序 ==
 
<pre>
 
#include <stdio.h>
 
#define LEN 5
 
int a[LEN] = { 10, 5, 2, 4, 7 };
 
void insertion_sort(void)
 
{
 
int i, j, key;
 
for (j = 1; j < LEN; ++j) {
 
printf("%d, %d, %d, %d, %d\n",
 
      a[0], a[1], a[2], a[3], a[4]);
 
key = a[j];  //key 标记为未排序的第一个元素
 
i = j - 1;
 
                // key  与已排序元素从大到小比较,寻找key应插入的元素 a[i+1]一般就是key 起初的值
 
////采用顺序查找方式找到插入的位置,在查找的同时,将数组中的元素进行后移操作,给插入元素腾出空间 ?
 
  
while (i >= 0 && a[i] > key) {
+
用法
a[i+1] = a[i];
+
./addkey  18.80.216.19 #这是你的IP
--i; //跳出while
 
}
 
a[i+1] = key; ////插入到正确位置
 
}
 
printf("%d, %d, %d, %d, %d\n",
 
      a[0], a[1], a[2], a[3], a[4]);
 
}
 
int main(void)
 
{
 
insertion_sort();
 
return 0;
 
}
 
/* 就是key 一直和前面排好的比,找到正确的位置
 
10, 5, 2, 4, 7
 
5, 10, 2, 4, 7
 
2, 5, 10, 4, 7
 
2, 4, 5, 10, 7
 
2, 4, 5, 7, 10 */  
 
 
</pre>
 
</pre>
  
 +
=on dovo=
 +
<pre>
 +
#!/usr/bin/expect
 +
#good on new kali  这个在新的kali 上是ok的
 +
set timeout 30
 +
set sshIP "4.88.1.2"
 +
set keypassword "ZdvV"
 +
set rootPassword "5D"
 +
#spawn ssh -o StrictHostKeyChecking=no -i /root/key  evan@$sshIP
 +
expect "Enter passphrase"
 +
send "$keypassword\r"
 +
expect "]$"
 +
send "sudo -i\r"
 +
expect "xxxxx"
 +
send "$rootPassword\r"
 +
expect "]#"
 +
## run command
 +
#send "cat /root/1  && echo 'test was ok .';exit\r"
 +
#send "bash /data/tmp/dbins  && echo 'dbins  was ok.';exit\r"
 +
#expect "52wan"
 +
#send "exit\r"
 +
#expect eof {exit 0}
 +
interact
  
  
[https://blog.csdn.net/qq_25775935/article/details/88724130 插入排序算法及C语言实现]
+
#!/usr/bin/expect
 +
# on old kali  这个在老的kali
 +
set timeout 30
 +
set sshIP "4.88.1.2"
 +
set keypassword "Zd"
 +
set rootPassword "5D"
 +
spawn ssh -o StrictHostKeyChecking=no -i /home/key  evan@$sshIP
 +
expect "Enter passphrase"
 +
send "$keypassword\r"
 +
expect "52wan"
 +
send "sudo -i\r"
 +
expect "password for 52wan:"
 +
send "$rootPassword\r"
 +
expect "]#"
 +
## run command
 +
#send "cat /root/1  && echo 'test was ok .';exit\r"
 +
#send "bash /data/tmp/dbins  && echo 'dbins  was ok.';exit\r"
 +
#expect "52wan"
 +
#send "exit\r"
 +
#expect eof {exit 0}
 +
interact
  
=附录 A. 字符编码=
+
</pre>
20191112
+
=see also=
字符编码的高位 左边是也
+
[https://www.cnblogs.com/jpinsz/p/10772750.html expect 分发ssh key脚本写得非常规范]
  
=see also=
 
[https://wiki.ubuntu.com.cn/C%E8%AF%AD%E8%A8%80%E7%AE%80%E8%A6%81%E8%AF%AD%E6%B3%95%E6%8C%87%E5%8D%97 C语言简要语法指南]
 
  
https://wiki.ubuntu.com.cn/Compiling_C
+
[https://www.cnblogs.com/changyaoguo/p/5764008.html ssh+expect批量分发]
  
[https://wiki.ubuntu.com.cn/Gcchowto Gcc-howto]
 
  
[[category:c]]
+
[https://www.cnblogs.com/zhaoyangjian724/p/3798006.html expect: spawn id exp4 not open]

2019年11月12日 (二) 10:33的版本

Question

有个项目要迁移 新来一批机器 要做初始化 于是抽空练了一下expect
 有可能有这个  警告 expect: spawn id exp4 not open 不过效果是没问题的

利用expect批量添加pubkey

多台添加pubkey

cat main 
#!/bin/bash
for ip   in `cat list` 

do 
#echo $ip
./addkey  $ip

done 



cat addkey 
#!/usr/local/bin/expect
 #define var
set timeout 17
# #<==接受第一个参数,赋值host
set host [lindex $argv 0]
set password "evan=="


#spawn
spawn ssh-copy-id  -i /home/evan/lx/ssh/opspub root@$host
#spawn ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub root@$host
#ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub [email protected]

#expect
expect {                  
 "*yes/no" { send "yes\r"; exp_continue}   
 "*password:" { send "$password\r" }       
 }  
#send  "exit\r"  
expect eof

cat list 
192.168.7.4
192.168.7.46

##上面用了copy-id  如果想用scp  etc 

    #!/usr/bin/expect    
    set timeout 5    
    set hostno [lindex $argv 0]    
    spawn scp ~/.ssh/id_dsa.pub impala$hostno:~/.ssh/pub_key    
    expect "*password*"    
    send "111111\r"    
    spawn ssh impala$hostno "cat ~/.ssh/pub_key/ >> ~/.ssh/authorized_keys"    
    expect "*password*"    
    send "111111\r"    
    spawn ssh impala$hostno "chmod 600 ~/.ssh/authorized_keys"    
    expect "*password*"    
    send "111111\r"    
    expect eof    

(3)分析:
set可以设置超时,或者设置一个变量的值
spawn是执行一个命令
expect等待一个匹配的输出流中的内容
send是匹配到之后向输入流写入的内容
[lindex $argv 0]表示脚本的第0个参数
expect eof表示读取到文件结束符

多台添加ssh安全(远程执行命令和退出)

 cat   run
#!/usr/local/bin/expect
 #define var
set timeout 17
set host [lindex $argv 0]
set password "evan=="

#spawn
spawn ssh root@$host

expect {

    "*#*" { send "sed -i 's/#PubkeyAuthentication yes/PubkeyAuthentication yes/g' /etc/ssh/sshd_config\r";
send "sed -i 's/^PasswordAuthentication yes/PasswordAuthentication no/g' /etc/ssh/sshd_config\r"
send "systemctl restart sshd\r"
	#send "\r"
    }
}
send  "exit\r"  
expect eof

cat main
#!/bin/bash
for ip   in `cat list` 

do 
#echo $ip
#./addkey  $ip
./run  $ip

done 

#run 
bash main 

单个


vi addkey

#!/usr/local/bin/expect
#SERVERS="101.00.208.197 120.40.043.52 " 
set timeout 5   
set host [lindex $argv 0]
spawn ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub root@$host
#ssh-copy-id  -i /home/evan/.ssh/id_rsa.pub [email protected]
expect {                  
 "*yes/no" { send "yes\r"; exp_continue}   
 "*password:" { send "P2xMGipLpSG7dA==\r" }       
 }  

expect eof


用法 
	./addkey  18.80.216.19 #这是你的IP 

on dovo

#!/usr/bin/expect
#good on new kali  这个在新的kali 上是ok的
set timeout 30
set sshIP "4.88.1.2"
set keypassword "ZdvV"
set rootPassword "5D"
#spawn ssh -o StrictHostKeyChecking=no -i /root/key  evan@$sshIP
expect "Enter passphrase"
send "$keypassword\r"
expect "]$"
send "sudo -i\r"
expect "xxxxx"
send "$rootPassword\r"
expect "]#"
## run command
#send "cat /root/1  && echo 'test was ok .';exit\r"
#send "bash /data/tmp/dbins   && echo 'dbins  was ok.';exit\r"
#expect "52wan"
#send "exit\r"
#expect eof {exit 0}
interact


#!/usr/bin/expect
# on old kali  这个在老的kali
set timeout 30
set sshIP "4.88.1.2"
set keypassword "Zd"
set rootPassword "5D"
spawn ssh -o StrictHostKeyChecking=no -i /home/key  evan@$sshIP
expect "Enter passphrase"
send "$keypassword\r"
expect "52wan"
send "sudo -i\r"
expect "password for 52wan:"
send "$rootPassword\r"
expect "]#"
## run command
#send "cat /root/1  && echo 'test was ok .';exit\r"
#send "bash /data/tmp/dbins   && echo 'dbins  was ok.';exit\r"
#expect "52wan"
#send "exit\r"
#expect eof {exit 0}
interact

see also

expect 分发ssh key脚本写得非常规范


ssh+expect批量分发


expect: spawn id exp4 not open