免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5457 | 回复: 0
打印 上一主题 下一主题

perl写入以及读取文件 [复制链接]

论坛徽章:
0
1 [报告]
发表于 2012-11-06 17:28 |显示全部楼层
楼主你提问的能力真的很强大。
这是写文件的代码:
  1. my $log_output = "config.log";
  2. unlink $log_output;

  3. open(FILE,">$log_output");
  4. while (1) {
  5.               print "Enter your host ip:\n";
  6.               $inputip = <STDIN>;           
  7.               chomp($inputip);
  8.               exit if($inputip=~/\bexit\b/);
  9.               print(FILE "$inputip\t\t");

  10.               print "Enter your host username:\n";
  11.               $inputusername = <STDIN>;           
  12.               chomp($inputusername);
  13.               exit if($inputusername=~/\bexit\b/);
  14.               print(FILE "$inputusername\t\t");
  15.               
  16.               system "stty -echo";
  17.               print "Enter your host password:\n";
  18.               $inputpasswd = <STDIN>;
  19.               chomp($inputpasswd);
  20.               exit if($inputpasswd=~/\bexit\b/);
  21.               $encryppasswd = encode_base64($inputpasswd);
  22.               system "stty echo";
  23.               print(FILE $encryppasswd);
  24. }
  25. close(FILE);
复制代码
这是读文件的代码:
  1. my ($hostlist) =  @ARGV;
  2.   my $HANDLE;
  3.   print $hostlist;
  4.   open (HANDLE, $hostlist) or die("ERROR: Can not locate \"$hostlist\" input file!\n");
  5.   my @lines = <HANDLE>;
  6.   my @errorArray;
  7.   my $line_no = 0;

  8.   close(HANDLE);
  9.   foreach my $line (@lines) {
  10.           $line_no++;
  11.           &TrimSpaces($line);

  12.           if($line) {
  13.                   if($line =~ /^\s*:\s*$/){
  14.                           print "Error in Parsing File at line: $line_no\n";
  15.                           print "Continuing to the next line\n";
  16.                           next;
  17.                   }
  18.                   (my $host, my $user, my $passwd) = split($line);
  19.                   &TrimSpaces($host);
  20.                   push @hostlist,$host;
  21.                   push @userlist,$host;
  22.                   push @passwdlist,$host;
  23.           }
  24.   }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP