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

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
 
 
第1行: 第1行:
[[category:shell]]
+
=c=
=Question=
 
有个项目要迁移 新来一批机器 要做初始化 于是抽空练了一下expect
 
=利用expect批量添加pubkey=
 
==多台添加pubkey ==
 
 
<pre>
 
<pre>
cat main
 
#!/bin/bash
 
for ip  in `cat list`
 
  
do
+
/*                                                                                                                                                                                         
#echo $ip
+
20答案是 Tuesday  因为列数定了是10                                                                                                                                                           
./addkey  $ip
+
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>
 +
==eg ==
 +
=== 石头剪刀布===
 +
<pre>
 +
#include <stdio.h>
 +
#include <stdlib.h>
 +
#include <time.h>
  
done
+
int main(void)
 +
{
 +
char gesture[3][10] = { "scissor", "stone", "cloth" };
 +
int man, computer, result, ret;
  
 +
srand(time(NULL));
 +
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;
 +
if (result > 0)
 +
printf("You win!\n");
 +
else if (result == 0)
 +
printf("Draw!\n");
 +
else
 +
printf("You lose!\n");
 +
}
 +
return 0;
 +
}
  
cat addkey
 
#!/usr/local/bin/expect
 
#define var
 
set timeout 17
 
# #<==接受第一个参数,赋值host
 
set host [lindex $argv 0]
 
set password "evan=="
 
  
 +
/*
 +
0、1、2三个整数分别是剪刀石头布在程序中的内部表示,用户也要求输入0、1或2,然后和计算机随机生成的0、1或2比胜负。这个程序的主体是一个死循环,需要按Ctrl-C退出程序。以往我们写的程序都只有打印输出,在这个程序中我们第一次碰到处理用户输入的情况。在这里只是简单解释一下,以后再细讲。scanf("%d", &man)这个调用的功能是等待用户输入一个整数并回车,这个整数会被scanf函数保存在man这个整型变量里。如果用户输入合法(输入的确实是整数而不是字符串),则scanf函数返回1,表示成功读入一个数据。但即使用户输入的是整数,我们还需要进一步检查是不是在0~2的范围内,写程序时对用户输入要格外小心,用户有可能输入任何数据,他才不管游戏规则是什么。
  
#spawn
+
和printf类似,scanf也可以用%c、%f、%s等转换说明。如果在传给scanf的第一个参数中用%d、%f或%c表示读入一个整数、浮点数或字符,则第二个参数的形式应该是&运算符加一个相应类型的变量名,表示读进来的数存到这个变量中;如果在第一个参数中用%s读入一个字符串,则第二个参数应该是数组名,数组名前面不加&,因为数组类型做右值时自动转换成指针类型,而scanf后面这个参数要的就是指针类型,在第 10 章 gdb有scanf读入字符串的例子。&运算符的作用也是得到一个指针类型,这个运算符以后再详细解释。
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
+
留给读者的思考问题是:(man - computer + 4) % 3 - 1这个神奇的表达式是如何比较出0、1、2这三个数字在“剪刀石头布”意义上的大小的
expect {                 
+
*/
"*yes/no" { send "yes\r"; exp_continue} 
 
"*password:" { send "$password\r" }     
 
 
#send  "exit\r" 
 
expect eof
 
  
cat list
+
</pre>
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)分析:
+
[https://wenku.baidu.com/view/55e520da5022aaea998f0f5e.html 石头剪刀布_C语言]
set可以设置超时,或者设置一个变量的值
 
spawn是执行一个命令
 
expect等待一个匹配的输出流中的内容
 
send是匹配到之后向输入流写入的内容
 
[lindex $argv 0]表示脚本的第0个参数
 
expect eof表示读取到文件结束符
 
  
</pre>
+
https://blog.csdn.net/guoqingchun/article/details/8104197
  
==多台添加ssh安全(远程执行命令和退出) ==
+
=排序与查找=
 +
==插入排序 ==
 
<pre>
 
<pre>
cat  run
+
#include <stdio.h>
#!/usr/local/bin/expect
+
#define LEN 5
#define var
+
int a[LEN] = { 10, 5, 2, 4, 7 };
set timeout 17
+
void insertion_sort(void)
set host [lindex $argv 0]
+
{
set password "evan=="
+
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 起初的值
 +
////采用顺序查找方式找到插入的位置,在查找的同时,将数组中的元素进行后移操作,给插入元素腾出空间 ?
  
#spawn
+
while (i >= 0 && a[i] > key) {
spawn ssh root@$host
+
a[i+1] = a[i];
 
+
--i; //跳出while
expect {
+
}
 
+
a[i+1] = key; ////插入到正确位置
    "*#*" { 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"
+
printf("%d, %d, %d, %d, %d\n",
send "systemctl restart sshd\r"
+
      a[0], a[1], a[2], a[3], a[4]);
#send "\r"
+
}
    }
+
int main(void)
 +
{
 +
insertion_sort();
 +
return 0;
 
}
 
}
send  "exit\r" 
+
/* 就是key 一直和前面排好的比,找到正确的位置
expect eof
+
10, 5, 2, 4, 7
 
+
5, 10, 2, 4, 7
cat main
+
2, 5, 10, 4, 7
#!/bin/bash
+
2, 4, 5, 10, 7
for ip  in `cat list`
+
2, 4, 5, 7, 10 */  
 
 
do
 
#echo $ip
 
#./addkey  $ip
 
./run  $ip
 
 
 
done
 
 
 
#run
 
bash main
 
 
</pre>
 
</pre>
  
==单个==
 
<pre>
 
  
vi addkey
 
  
#!/usr/local/bin/expect
+
[https://blog.csdn.net/qq_25775935/article/details/88724130 插入排序算法及C语言实现]
#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
+
=附录 A. 字符编码=
 +
20191112
 +
=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://wiki.ubuntu.com.cn/Gcchowto Gcc-howto]
./addkey  18.80.216.19 #这是你的IP
 
</pre>
 
  
=on dovo=
+
[[category:c]]
<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
 
 
 
 
 
#!/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
 
 
 
</pre>
 
=see also=
 
[https://www.cnblogs.com/zhaoyangjian724/p/3798006.html expect: spawn id exp4 not open]
 

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

c


/*                                                                                                                                                                                          
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                 

eg

石头剪刀布

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
	char gesture[3][10] = { "scissor", "stone", "cloth" };
	int man, computer, result, ret;

	srand(time(NULL));
	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;
		if (result > 0)
			printf("You win!\n");
		else if (result == 0)
			printf("Draw!\n");
		else
			printf("You lose!\n");
	}
	return 0;
}


/*
0、1、2三个整数分别是剪刀石头布在程序中的内部表示,用户也要求输入0、1或2,然后和计算机随机生成的0、1或2比胜负。这个程序的主体是一个死循环,需要按Ctrl-C退出程序。以往我们写的程序都只有打印输出,在这个程序中我们第一次碰到处理用户输入的情况。在这里只是简单解释一下,以后再细讲。scanf("%d", &man)这个调用的功能是等待用户输入一个整数并回车,这个整数会被scanf函数保存在man这个整型变量里。如果用户输入合法(输入的确实是整数而不是字符串),则scanf函数返回1,表示成功读入一个数据。但即使用户输入的是整数,我们还需要进一步检查是不是在0~2的范围内,写程序时对用户输入要格外小心,用户有可能输入任何数据,他才不管游戏规则是什么。

和printf类似,scanf也可以用%c、%f、%s等转换说明。如果在传给scanf的第一个参数中用%d、%f或%c表示读入一个整数、浮点数或字符,则第二个参数的形式应该是&运算符加一个相应类型的变量名,表示读进来的数存到这个变量中;如果在第一个参数中用%s读入一个字符串,则第二个参数应该是数组名,数组名前面不加&,因为数组类型做右值时自动转换成指针类型,而scanf后面这个参数要的就是指针类型,在第 10 章 gdb有scanf读入字符串的例子。&运算符的作用也是得到一个指针类型,这个运算符以后再详细解释。

留给读者的思考问题是:(man - computer + 4) % 3 - 1这个神奇的表达式是如何比较出0、1、2这三个数字在“剪刀石头布”意义上的大小的
*/

石头剪刀布_C语言

https://blog.csdn.net/guoqingchun/article/details/8104197

排序与查找

插入排序

#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];
			--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 */ 


插入排序算法及C语言实现

附录 A. 字符编码

20191112

see also

C语言简要语法指南

https://wiki.ubuntu.com.cn/Compiling_C

Gcc-howto